Thursday, November 20, 2008

Entity Framework, first impressions...

The Entity Relational framework for .Net is a way to relate normalized data from a database store to the objects that represent it within an application. Currently in development I have to create a mapper class that knows the SQL statements to store and retrieve data. This class takes and returns domain or business objects that represent the data and interacts with the database.

The Enitity framework creates an XML mapping file that is used at runtime to represent the database. It allows the developer to hide the specific database by using the Entity provider that will hide the underlying communication with the database. This introduces a new SQL type language that the user of the framework must understand. The following is and example of the eSQL language:

SELECT sp.FirstName, sp.LastName, sp.HireDateFROM AdventureWorks.AdventureWorksDB.SalesPeople AS spWHERE sp.HireDate > @date

The framwork allows the developer to navigate the relationships within data without the more complicated SQL such as INNER JOINS. The following is an example of the inner join syntax and the related eSQL:
SELECT SalesPersonID, FirstName, LastName, HireDateFROM SalesPerson spINNER JOIN Employee e ON sp.SalesPersonID = e.EmployeeIDINNER JOIN Contact c ON e.EmployeeID = c.ContactIDINNER JOIN SalesOrder o ON sp.SalesPersonID = o.SalesPersonIDWHERE e.SalariedFlag = 1 AND o.TotalDue > 200000
Same Query in eSQL:
SELECT VALUE sp
FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp
WHERE EXISTS(
SELECT VALUE o
FROM NAVIGATE(p, AdventureWorks.SalesPerson_Order) AS o
WHERE o.TotalDue > 200000)
A valuable key word here is the eSQL word "NAVIGATE" which represent inheritance within the entity framework. This is another helpful concept introduced, that foreign key relationships are now represented through inheritance. This will allow me to see a table as the sum of all of its relationships.
Object Representation
Notice in the following snippet that the query is returning a List of type SalesPerson:


Query newSalesPeople = ctx.GetQuery(
"SELECT VALUE sp "

+ "FROM AdventureWorks.AdventureWorksDB.SalesPeople AS sp "
+ "WHERE sp.HireDate > @date",

new QueryParameter("@date", hireDate));

foreach(SalesPerson p in newSalesPeople)
{ Console.WriteLine("{0}\t{1}", p.FirstName, p.LastName);




The entity framework knows how to represent partial class objects of the data that it is representing. This allows qualified objects to be used from the framework and give the developer a way to get rid of database specific code such as connection, command and adapter objects.
My first impression of the framework is apprehensive excitement. I have tried to write code that is database independent and acts as if the database is only a place to persist data. This has been hard and time consuming. The framework allows this to happen more quickly and with less time trying to relate data to the application.
My worries come from dealing with datasets in the past and the numerous issues related to their bulkiness and non-persistence in a web environment. So if the entity framework comes with little to no speed issues and allows for it's use in a disconnected environment then I think it will be useful. This remains to be seen.