barcodelib c# Serializing Self-Tracking Entities in the ViewState in Font

Printer QR Code JIS X 0510 in Font Serializing Self-Tracking Entities in the ViewState

9-9. Serializing Self-Tracking Entities in the ViewState
Making QR-Code In None
Using Barcode generator for Font Control to generate, create QR-Code image in Font applications.
www.OnBarcode.com
UPC Symbol Encoder In None
Using Barcode printer for Font Control to generate, create UPC Symbol image in Font applications.
www.OnBarcode.com
Problem
Make European Article Number 13 In None
Using Barcode drawer for Font Control to generate, create EAN13 image in Font applications.
www.OnBarcode.com
Printing EAN / UCC - 13 In None
Using Barcode generator for Font Control to generate, create EAN / UCC - 13 image in Font applications.
www.OnBarcode.com
You want to save a self-tracking entity in the ViewState of an ASP.NET application.
Draw ECC200 In None
Using Barcode generator for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com
QR Maker In None
Using Barcode creation for Font Control to generate, create QR image in Font applications.
www.OnBarcode.com
Solution
PDF417 Printer In None
Using Barcode generator for Font Control to generate, create PDF 417 image in Font applications.
www.OnBarcode.com
ISSN - 10 Generator In None
Using Barcode printer for Font Control to generate, create ISSN - 10 image in Font applications.
www.OnBarcode.com
Let s suppose you have a model like the one in Figure 9-13.
Denso QR Bar Code Decoder In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Denso QR Bar Code Drawer In Java
Using Barcode generation for BIRT Control to generate, create QR Code ISO/IEC18004 image in BIRT reports applications.
www.OnBarcode.com
Figure 9-13. A model with a Job entity
PDF 417 Generation In None
Using Barcode drawer for Office Word Control to generate, create PDF 417 image in Office Word applications.
www.OnBarcode.com
Create USS-128 In Java
Using Barcode maker for Java Control to generate, create USS-128 image in Java applications.
www.OnBarcode.com
USING THE ENTITY FRAMEWORK IN N-TIER APPLICATIONS
PDF417 Creator In None
Using Barcode maker for Software Control to generate, create PDF-417 2d barcode image in Software applications.
www.OnBarcode.com
Print Denso QR Bar Code In None
Using Barcode creation for Office Excel Control to generate, create QR-Code image in Excel applications.
www.OnBarcode.com
If you try to assign a job entity to the ViewState, the BinaryFormatter would fail. To get around this, we can create our own serializer using the DataContractSerializer to serialize the self-tracking entity. To create your own serializer and a test page, do the following: 1. 2. Create a new empty Web Application. Add an ADO.NET Entity Data Model with the Job table. Right-click the design surface and view the Properties. Right-click the design surface and select Add Code Generation Item. Select the ADO.NET Self-Tracking Entity Generator from the Code Installed Template. Click Add. Add the code in Listing 9-31 to the project. We ll use this class to serialize the object graph. Add a Default.aspx page to the project. Use the code in Listing 9-32 for the page and the code in Listing 9-33 for the code behind.
Making Code-128 In .NET
Using Barcode drawer for Reporting Service Control to generate, create Code-128 image in Reporting Service applications.
www.OnBarcode.com
Barcode Generator In Java
Using Barcode printer for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
3. 4.
Printing Barcode In Java
Using Barcode maker for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Generate Barcode In .NET
Using Barcode generation for Reporting Service Control to generate, create Barcode image in Reporting Service applications.
www.OnBarcode.com
Listing 9-31. The ByteArraySerializer class is used to serialize the object graph. using System.IO; using System.Runtime.Serialization; namespace Recipe9 { public class ByteArraySerializer { public static byte[] ToByteArray<T>(T graph) { var stream = new MemoryStream(); var serializer = new DataContractSerializer(typeof(T)); serializer.WriteObject(stream, graph); return stream.ToArray(); } public static T ToObject<T>(byte[] bytes) { var stream = new MemoryStream(bytes); stream.Position = 0; var serializer = new DataContractSerializer(typeof(T)); return (T)serializer.ReadObject(stream); } } } Listing 9-32. The ASP.NET page that tests our ViewState serialization <body> <form id="form1" runat="server"> <div> <table> <tr> <td>New Job Title:</td> <td><asp:TextBox ID="JobTitle" runat="server" /></td>
Drawing ANSI/AIM Code 128 In Objective-C
Using Barcode printer for iPhone Control to generate, create Code 128A image in iPhone applications.
www.OnBarcode.com
Code39 Maker In None
Using Barcode creator for Microsoft Excel Control to generate, create Code-39 image in Excel applications.
www.OnBarcode.com
USING THE ENTITY FRAMEWORK IN N-TIER APPLICATIONS
</tr> <tr> <td>New Salary:</td> <td><asp:TextBox ID="Salary" runat="server" /></td> </tr> </table> <br /> <asp:Button ID="create" runat="server" OnClick="Create_Click" Text="Create Job" />   <asp:Button ID="update" runat="server" OnClick="Update_Click" Text="Update Job" /> <table> <tr> <td>Job Title:</td> <td><asp:Label ID="JobTitleLabel" runat="server" /></td> </tr> <tr> <td>Salary:</td> <td><asp:Label ID="SalaryLabel" runat="server" /></td> </tr> </table> </div> </form> </body> Listing 9-33. The code behind for the page public partial class Default : System.Web.UI.Page { public Job Job { get { var bytes = ViewState["job"] as byte[]; return ByteArraySerializer.ToObject<Job>(bytes); } set { var bytes = ByteArraySerializer.ToByteArray<Job>(value); ViewState["job"] = bytes; } } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { // create the default job this.Job = CreateJob("Plumber", 82000M);
USING THE ENTITY FRAMEWORK IN N-TIER APPLICATIONS
InitializeControls(); } } private void InitializeControls() { this.JobTitleLabel.Text = Job.Title; this.SalaryLabel.Text = Job.Salary.ToString(); } private Job CreateJob(string title, decimal salary) { using (var context = new EFRecipesEntities()) { return new Job { Title = title, Salary = salary }; } } protected void Create_Click(object sender, EventArgs e) { decimal salary = 0; decimal.TryParse(this.Salary.Text, out salary); this.Job = CreateJob(this.JobTitle.Text, salary); InitializeControls(); } protected void Update_Click(object sender, EventArgs e) { decimal salary = 0; decimal.TryParse(this.Salary.Text, out salary); this.Job = CreateJob(this.JobTitle.Text, salary); InitializeControls(); } } After filling in a job title, salary, and clicking Create Job, the page is rendered in the browser, as shown in Figure 9-14.
USING THE ENTITY FRAMEWORK IN N-TIER APPLICATIONS
Figure 9-14. The page rendered in a browser after the new job has been created
How It Works
We used the DataContractSerializer to serialize the self-tracking entity into a byte array. Once in the byte array, we can assign it to the ViewState. To get the self-tracking entity from the ViewState, we read the byte array and deserialize the entity. We needed to use the DataContractSerializer because the current version of self-tracking entities does not support the binary serialization. A future version would likely support it. Binary serialization works for POCO entities as long as they are marked with the Serializable attribute. Dynamic proxies can also be serialized and deserialized when using binary serialization. However, when deserializing the proxy, if the proxy is not already loaded in the AppDomain, you will get an exception. To avoid the exception, you can force the proxy to be loaded in the AppDomain before deserialization by calling CreateProxyTypes(). You don t need to call CreateProxyTypes() if the serialization and de-serialization occur in the same AppDomain because the initial retrieval of the entity would have created the proxy in the AppDomain.
Copyright © OnBarcode.com . All rights reserved.