Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

How we’d normally represent entities outside of Azure

3/10/2011 9:01:48 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
To keep things simple, we’ll return to the Hawaiian Shirt Shop website. Over the next few sections, we’ll look at how we’d typically store shirt data in a noncloud database. We’ll focus on the following:
  • How would we represent a shirt in C#?

  • How would we store shirt data in SQL Server?

  • How would we map and transfer data between the two platforms?

By understanding how we’d represent our shirt data in typical solutions, we can then see how this translates to the Table service.

1. How we’d normally represent an entity in C#

We defined a Hawaiian shirt using the following data transfer object (DTO) entity.

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}

The Product entity class that we’re using to represent Hawaiian shirts contains three properties that we’re interested in (Id, Name, and Description).

We chose to keep the example simple by hardcoding the list of products rather than retrieving it from a data store such as the Table service or SQL Server. The following code is a hardcoded list of the three shirt entities displayed in figure 1 (Red Shirt, Blue Shirt, and Blue Frilly Shirt).

var products =
new List<Product>
{
new Product
{
Id = 1,
Name = "Red Shirt",
Description = "Red"
},
new Product
{
Id = 2,
Name = "Blue Shirt",
Description = "A Blue Shirt"
},
new Product
{
Id = 3,
Name = "Blue Frilly Shirt",
Description = "A Frilly Blue Shirt"
},
};


In the preceding code, we simply defined the list of products as a hardcoded list. Obviously this isn’t a very scalable pattern—you don’t want to redeploy the application every time your shop offers a new product—so let’s look at how you can store that data using a non-Windows Azure environment, such as SQL Server.

.2. How we’d normally store an entity in SQL Server

To store an entity in SQL Server, you first need to define a table where you can store the entity data. Figure 1 shows how the Products table could be structured in SQL Server.

Figure 1. A representation of how you could store the Hawaiian shirt data in SQL Server


Figure 1 shows a table called Products with three columns (ProductId, ProductName, and Description). In this table, ProductId would be the primary key and would uniquely identify shirts in the table. Table 1 shows how the shirt data would be represented in SQL Server.

Table 1. Logical representation of the Products table in SQL Server
ProductIdProductNameDescription
1Red ShirtRed
2Blue ShirtA Blue Shirt
3Blue Frilly ShirtA Frilly Blue Shirt

In table 1 we’ve enforced a fixed schema in our SQL Server representation of the Hawaiian shirts. If you wanted to store extra information about the product (a thumbnail URI, for example) you’d need to add an extra column to the Products table and a new property to the Product entity.

Now that we can represent the Hawaiian shirt product as both an entity and as a table in SQL Server, we’ll need to map the entity to the table.

3. Mapping an entity to a SQL Server database

Although you can manually map entities to SQL Server data, you’d typically use a data-access layer framework that provides mapping capabilities. Typical frameworks include the following:

  • ADO.NET Entity Framework

  • LINQ’s many varieties, like LINQ to SQL and LINQ to DataSet

  • NHibernate

The following code maps the Products table returned from SQL Server as a dataset to the Product entity class using LINQ to DataSet.

var products = ds.Tables["Products"].AsEnumerable().Select
(
row => new Product
{
Id = row.Field<int>("ProductId"),
Name = row.Field<string>("ProductName"),
Description = row.Field<string>("Description")
}
);

In this example, we convert the dataset to an enumerable list of data rows and then reshape the data to return a list of Product entities. For each property in the Product entity (Id, Name, and Description) we map the corresponding columns (ProductId, ProductName, and Description) from the returned data row.

We’ve now seen how we’d normally define entities in C#, how we’d represent entities in SQL Server, and how we could map the entity layer to the database. Let’s look at what the differences are when using the Table service.

Other -----------------
- A brief overview of the Table service
- BLOBs : Setting shared access permissions
- Enterprise Service Bus with BizTalk Server and Windows Azure : Distributed and Scalable ESB Architecture
- Enterprise Service Bus with BizTalk Server and Windows Azure : The ESB Toolkit
- Enterprise Service Bus with BizTalk Server and Windows Azure : Integration with BizTalk
- Copying BLOBs - Copying files via the StorageClient library
- Using local storage with BLOB storage (part 3) - Improving your handler to check the last modified time
- Using local storage with BLOB storage (part 2) - Updating your HTTP handler to use local storage & Checking properties of a BLOB without downloading it
- Using local storage with BLOB storage (part 1) - Using a local cache & Defining and accessing local storage
- Integrating BLOBs with your ASP.NET websites
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server