Webscale architecture, Actors, CQRS, PowerShell, DevOps, Infrastructure as Code, Continuous Delivery
Sunday, January 15, 2012
Adding dynamic ‘where’ clauses to a linq query using Builder and Specification pattern
Saturday, November 21, 2009
Reactive Extensions - Much simpler asynchronous and event based programming
Friday, November 20, 2009
Reactive Extensions – First Look
Wednesday, May 28, 2008
ADO.NET Entity Framework - Part 3
ADO.NET Entity framework - update changes to the storage layer.
ADO.NET Entity framework helps you in creating, updating and deleting objects by using Object Services. Object Services keep track of the changes and then update them to the persisted storage by executing the corresponding T-SQL statements. The SaveChanges() method of the ObjectContext class is used for saving the changes to the persistence storage.
In this post I will explain how to apply changes on a detached object to the storage layer.
When a query is executed within an object context, the returned objects are automatically attached to the context. Object Services also lets you attach existing objects to an object context. Once you attach an object to the context. You can use the SaveChanges() method to update the changes in the database.
The ApplyPropertyChanges method can be used when the object already exists in the store and the detached object has property updates that you want to persist.
In the following example an updated Customer object is passed to the Save method. The Save method retrieves the original object by using the EntityKey and then updates the original object by the ApplyPropertyChanges() method. Later the changes are updated by using the SaveChanges() method of the ObjectContext class.
public int Save(Customer myCustomer)
{
EntityKey key;
object originalItem;
using (TwoWayDataBindingEntities entites = new TwoWayDataBindingEntities())
{
try
{
// Get the detached object's entity key.
if (myCustomer.EntityKey == null)
{
// Get the entity key of the updated object.
key = entites.GetEntityKey("Customers", myCustomer);
}
else
{
key = myCustomer.EntityKey;
}
// Get the original item based on the entity key from the context
// or from the database.
if (entites.TryGetObjectByKey(key, out originalItem))
{
// Call the ApplyPropertyChanges method to apply changes
// from the updated item to the original version.
entites.ApplyPropertyChanges(key.EntitySetName, myCustomer);
}
return entites.SaveChanges();
}
catch (InvalidOperationException ex)
{
//Exception handling logic
}
return 0;
}
}
Wednesday, May 21, 2008
ADO.NET Entity Framework - Part 2
Working with ADO.NET Entity framework
After looking into the architecture of the Entity Framework, this article I will show the how to create an EDM using the Entity Data Model Wizard.
The Entity Framework compiles a set of conceptual and storage schemas, together with the mappings between them, into bidirectional pairs of Entity SQL statements called client views. These views drive query and update processing in the runtime engine.
Generating an Entity Data model
To generate an EDM you need to first add an ADO.NET Entity Data Model item template to the project. Follow the below given steps to add and EDM to the project
- Right-click the project, point to Add, and then click New Item.
- Select ADO.NET Entity Data Model in the Templates pane.
- Click Add after you enter the name for EDM. This will present the Entity Data Model Wizard.
- Select Generate from database in the Choose Model Contents dialog box. Then click Next.
- Click the New Connection button. The Connection Properties dialog box is displayed.
- Enter your server name, select the authentication method.
- Select the database name, and then click OK.
- The Choose Your Data Connections dialog box is updated with your database connection settings.
- Select all the tables from the dialog box and click Finish.
The Entity Framework tools generate a class derived from ObjectContext that represents the entity container defined in the conceptual model. The ObjectContext class supports queries against an EDM that return entities as objects, as well as creating, updating, and deleting entity objects. The Entity Framework supports object queries against an EDM. Queries can be composed using Entity SQL, LINQ to entities and object query builder methods.
Once the entity data model is generated you can use any of the above given methods to query your ObjectContext class.
For example :
using (BlogEntities entities = new BlogEntities())
{
var query = from blogentry in entities.blogentries
.Skip(StartRow).Take(PageSize)
orderby blogentry.datepublished descending
select blogentry;
return query;
}
Tuesday, May 13, 2008
Accessing anonymous types from a different assembly
Anonymous types are one of the new features introduced in C# 3.0 which helps you to encapsulate a set of properties into an object without having to define a type. The type will not be available at the source code level (This is generated by the compiler).
In this article I will explain how to access anonymous types exposed from another assembly in the caller method. There are some very nice articles on the topic by Tom and Clark .
I have a GetContactList method which returns an anonymous type collection that is a result of the LINQ query.
{
public Int32 ContactID { get; set; }
public String ContactName { get; set; }
public Int32 CompanyID { get; set; }
}
public class Company
{
public Int32 CompanyID { get; set; }
public String CompanyName { get; set; }
public String CompanyURL { get; set; }
}
public Object[] GetContactList()
{
var contacts = from contact in GetContacts()
join company in GetCompanies()
on contact.CompanyID equals company.CompanyID
select new { contact.ContactName, company.CompanyName, company.CompanyURL };
return contacts.ToArray();
}
The properties of this return type cannot be accessed before casting it to an object.
The type returned from the above method looks something like this to the external caller.
<>f__AnonymousType0`3[System.String,System.String,System.String]
I will go through some interesting parts of the above type
- The 0 after the <>f__AnonymousType is a zero based index of each anonymous type definition. (If you have another anonymous type in the code that will have the index 1)
- The 3 after the 0 represents the number of properties in the anonymous type (We have 3 properties – ContactName, CompanyName, CompanyURL)
- The rest of the portions represent the data types and method name
I have created an Entity converter that will convert this anonymous type to the respective type in the caller function. The below given code will convert the anonymous type collection returned to the ContactDetails object.
public class ContactDetails
{
public String ContactName { get; set; }
public String CompanyName { get; set; }
public String CompanyURL { get; set; }
}
private ContactDetails EntityTranslator(Collection<KeyValuePair<String, Object>> propertyCollection)
{
ContactDetails cd = new ContactDetails();
foreach (KeyValuePair<String, Object> propertyInfo in propertyCollection)
{
switch (propertyInfo.Key)
{
case "ContactName":
cd.ContactName = propertyInfo.Value.ToString();
break;
case "CompanyName":
cd.CompanyName = propertyInfo.Value.ToString();
break;
case "CompanyURL":
cd.CompanyURL = propertyInfo.Value.ToString();
break;
}
}
return cd;
}
Now in the caller method I have to call the EntityTranslator like
Object[] contacts = Assembly2.GetContactList();
Collection<ContactDetails> details = new Collection<ContactDetails>();
foreach (Object contact in contacts)
{
ContactDetails cd = new ContactDetails();
Type t = contact.GetType();
PropertyInfo[] pInfo = t.GetProperties();
Collection<KeyValuePair<String, Object>> propertyCollection = new Collection<KeyValuePair<String, Object>>();
foreach(PropertyInfo pi in pInfo)
{
propertyCollection.Add(new KeyValuePair<String, Object>(pi.Name, pi.GetValue(contact, null)));
}
cd = ContactTranslator(propertyCollection);
details.Add(cd);
}
As you have seen by using Reflection we can access the properties of anonymous type returned from another assembly.
Friday, May 9, 2008
ADO.NET Entity Framework - Introduction
I have been using LINQ to SQL as an OR-mapper for my application since the release of Visual studio 2008. As a light weight OR-mapper and for RAD scenarios LINQ to SQL serves my purpose as a framework for persisting data. But when thinking about Enterprise Application development and real scenarios, I don’t find LINQ to SQL as powerful tool with all the features I require for Application development.
LINQ to SQL as present was not a competitor for NHibernate or LLBLGen which are the common OR-mappers available. If you are looking for a better OR-mapper from Microsoft, ADO.NET Entity framework is the answer. This one gives life to the conceptual modeling and has a lot of features compared to LINQ to SQL. The primary benefit of the ADO.NET Entity Framework is to elevate the level of abstraction at which developers can work when they deal with data and to decrease the amount of code that is required to create and maintain data-oriented applications.
Some of the advantages of using Entity framework:
- Entity framework maps relational tables, columns, and foreign key constraints to entities and relationships in conceptual models. Instances of these classes are populated with data and data changes are saved based on defined mappings between the conceptual and storage model.
- Supports Entity SQL and LINQ to Entities two powerful query languages to query the data.
- Provides a rich and intuitive view to the data model from the application perspective as opposed to the normalized view of data.
- Last but not least, comes with a code generation wizard that is integrated with Visual Studio that will create less and more quality code much faster.
In this article series I will be explaining the underlining technologies in the Entity Framework by some tutorials that help you to get hands on experience on the topic.
The architecture
The ADO.NET Entity Framework is a layered framework which abstracts the relational schema of a database and presents a conceptual model.
- Object Services: Object Services is a component of the Entity Framework that enables you to query, insert, update, and delete data, expressed as strongly-typed CLR objects that are instances of entity types. Object Services supports both Language-Integrated Query (LINQ) and Entity SQL queries against types defined in an Entity Data Model (EDM). Object Services materializes returned data as objects, and propagates object changes back to the persisted data store. It also provides facilities for tracking changes, binding objects to controls, and handling concurrency. Object Services is implemented by classes in the System.Data.Objects and System.Data.Objects.DataClasses namespaces.
- Entity SQL: Entity SQL is a SQL-like language provided by the ADO.NET Entity Framework to support the Entity Data Model (EDM). The EDM is an extended relational model that supports basic relational concepts, rich types with inheritance, and relationships. Entity SQL supports EDM constructs, enabling users to effectively query the data represented by an entity model.
- Entity Data Model: The Entity Data Model (EDM) is a specification for defining the data used by applications built on the Entity Framework. Applications using the EDM define entities and relationships in the domain of the application in a design schema. The design schema is used to build programmable classes used by application code. The entity data model contains of 3 parts
- Conceptual Schema Definition Language (CSDL): The conceptual schema is a design template for the object model that will be used by applications built on the Entity Data Model (EDM). Conceptual schema is used to declare and define entities, associations, inheritance etc. It is from this schema the Entity classes are generated.
- Store schema definition language (SSDL): The SSDL is a formal description of the database that persist data for an application built on the Entity Data Model (EDM). The entities and associations declared in this schema are the basis for mapping entities and associations in the conceptual schema to the corresponding entities in the storage model. This contains the metadata describing the database that persist data.
- Mapping specification language (MSL): In the Entity Data Model (EDM), the mapping specification uses mapping specification language (MSL) to connect the types declared in conceptual schema definition language (CSDL) to database metadata that persists data for applications that use the object model being defined.
- LINQ to entities: LINQ to Entities enables developers to write queries against the database from the same language used to build the business logic
In In the next part of this series we will look into more details of the Entity Framework and see how Entity Framework will assist in creating data centric applications more easily.
Thursday, April 17, 2008
Sorting and Grouping in LINQ to Objects
For the examples used in this article I will use the following data model.
public class Customer
{
public String Name { get; set; }
public Int32 Age { get; set; }
public Collection<Address> Addresses { get; set; }
public String Industry { get; set; }
}
public class Address
{
public String AddressLine { get; set; }
public String Country { get; set; }
}
And the method to return the data as
private static Collection<Customer> GetCustomers()
{
return new Collection<Customer> {
new Customer { Name = "John Samuels", Age = 40 , Addresses = new Collection<Address>
{ new Address { AddressLine = "New York", Country = "USA" },
new Address { AddressLine = "California, 21st Main Road", Country = "USA" },
new Address { AddressLine = "London", Country= "UK" }
}, Industry = "Computers"},
new Customer { Name = "Sreejith P.R", Age = 30 , Addresses = new Collection<Address>
{ new Address { AddressLine = "Bangalore", Country = "India" },
new Address { AddressLine = "Redmond", Country = "USA" }
}, Industry = "Telecom"},
new Customer { Name = "Kim David", Age = 35 , Addresses = new Collection<Address>
{ new Address { AddressLine = "Shangai", Country = "China" }
}, Industry = "Telecom"},
new Customer { Name = "Rahul Bose", Age = 50 , Addresses = new Collection<Address>
{ new Address { AddressLine = "New York", Country = "USA" },
new Address { AddressLine = "Bangalore", Country = "India" },
new Address { AddressLine = "Mysore", Country= "India" },
new Address { AddressLine = "Kolkatta", Country= "India" }
}, Industry = "Information Technology"},
new Customer { Name = "Micheal Johnson", Age = 37 , Addresses = new Collection<Address>
{ new Address { AddressLine = "New York", Country = "USA" },
new Address { AddressLine = "London", Country= "UK" }
}, Industry = "Computers"}
};
}
Now let’s say we want to see the customers in USA, sorted by their names in descending order.
The query I wrote to achieve this result is
var customers = (from customer in GetCustomers()
from address in customer.Addresses
where address.Country == "USA"
orderby customer.Name descending
select customer.Name).Distinct();
The operator that we are interested here is the orderby operator which sorts the collection in ascending order by default. You can also use the descending, ThenByDescending, ThenBy, OrderByDescending operators also for sorting.
The groupby operator helps you to group the query result based on any grouping condition.
For example to display all the customers grouped by industry we write the query like.
var industryCustomers =
from customer in GetCustomers()
group customer by customer.Industry into customerIndustries
select new { Industry = customerIndustries.Key, Customers = customerIndustries };
You can also have the advantage of using aggregation operators after grouping the results.
For example to find the minimum age of each customer by each industry we can write the query like.
var industryCustomers =
from customer in GetCustomers()
group customer by customer.Industry into customerIndustries
select new { Industry = customerIndustries.Key, Customers = customerIndustries, MinAge = customerIndustries.Min(cust => cust.Age)};