Architecting Your Data Access Layer with the Entity Framework

2009 June 26
by Wade

I had the pleasure to co-present with one of my fellow evangelists, Dave Bost, on architecting and developing with the ADO.NET Entity Framework this week.  I focused on application architecture topics while Dave focused on developing applications.

I think we can all agree – and if not, please let me know why – that the Entity Framework provides some awesome capabilities – mapping your conceptual schema to your data schema, isolation from the relational database and database schema, change tracking and identity resolution, full query comprehension and optimization, and more.  Yet, despite these features, it is difficult to figure out how and where to include the Entity Framework in your application architecture.

In preparation for this talk, I spent a lot of time looking at different strategies and architectures for using your data access layer (DAL) and the Entity Framework – or any O/RM tool, for that matter.  In the end, I settled on three approaches – certainly there are other patterns and approaches, but I found these to be the most relevant:

  1. Entity Framework as the DAL
  2. Full encapsulation of the Entity Framework
  3. Partial encapsulation of the Entity Framework

Let me spell it out in a little more detail, as well as highlight what I feel are the different pro’s and con’s.

Entity Framework as the Data Access Layer

With this approach, you opt to use the Entity Framework as your data access layer.  This is a perfectly legitimate approach, and comes with a host of benefits in many situations.  Here’s a picture of the architecture:

Entity Framework as the DAL

As you can see, in this architecture aspects of the Entity Framework – like entities and the entity framework object context – are scattered throughout the various tiers.  This approach has many benefits, but it also introduces some challenges.

- Pro’s:

  • Excellent isolation from the DB schema
  • Independence from the relational database
  • Object services, namely identity and change tracking
  • Full query comprehension and optimization

- Con’s:

  • In .NET 3.5 SP1 the “object first” approach is not well supported (updated in EF 4.0)
  • Difficult to switch your O/RM, as you are tightly coupled to the Entity Framework
  • Limited support for data validation

If you aren’t offended by the scattering of your entities and entity contexts throughout your application tiers, then this is a perfectly valid architecture.  That said, most people probably recognize that technology changes, and an application that works perfectly well with Entity Framework today may need to move to a newer technology in the future.  Consequently, you may want to look at an approach that encapsulates the Entity Framework, so that you separate your concerns and have high cohesion in your tiers without tight coupling.

Full Encapsulation of the Entity Framework

There are many benefits to encapsulation.  We have talked and read about ensuring a separation of concerns – e.g. high cohesion, loose coupling – for years now, and for good reason.  Typically we want to reduce the tight coupling throughout application so that tools like Entity Framework don’t proliferate through all the tiers of our application.  This helps make our code more testable, and allows us to utilize techniques such as Inversion of Control and Dependency Injection.

At the same time, fully encapsulating a tool like Entity Framework also comes with some costs.  If the real value of this tool is the ability to empower developers to quickly build powerful applications that are fully optimized against your database, then full encapsulation may inhibit some of the capabilities.

- Pro’s:

  • Simplified queries
  • POCO objects
  • ORM & database agnostic
  • No references to Entity Framework
  • Isolation of SQL queries

- Con’s:

  • Loss of change tracking and identity management with Entity Framework
  • Additional object materialization of business objects
  • No query composition; simple LINQ enumeration queries

Again, this is not a full laundry list, but hopefully it provides some of the different benefits and challenges.  Fortunately, there’s a third path you can take, which is a hybrid of the two – I call this partial encapsulation.

Partial Encapsulation of the Entity Framework

When you decide to partially encapsulate the Entity Framework, you acknowledge the benefits of the separation of concerns and do what you can to loosely couple your tiers.  But you also try to preserve the advantages of a tool like the Entity Framework.  In this scenario, you might expose your entities to the different tiers of your application, but encapsulate the rest of the Entity Framework in your DAL.  When you return data from the DAL, you would simply use one of the three techniques: IEnumerable, IQueryable, or ObjectQuery.  Other than the last technique, you aren’t exposing aspects of the Entity Framework – other than the entities themselves – to any other tiers.  This preserves the loose coupling, but allows you to leverage the real value of the Entity Framework.

- Pro’s:

  • Query composition
  • Identity resolution & changing tracking
  • Some independence from O/RM tool

- Con’s:

  • Use of Entity Framework entities has implications on business layer
  • Lack of isolation of SQL queries – spread throughout the various tiers

Partial encapsulation is a pragmatic approach to this problem, and it’s a technique the I have seen many customers and partners utilize very successfully.  In the end, the approach you choose depends on your situation.  All three of these approaches are perfectly valid, but aren’t necessarily valid in every situation.

 

Below are the slides I used during the presentation.  I am sure that this is a presentation I’ll give again in the future, so expect to see updates and/or changes.

I’d love to hear your feedback, so please leave me a comment or message.

I hope this helps!

Stratus: Tamer of Clouds

2009 June 19
by Wade

Many of you probably recall that I used to work for Statera, a Microsoft partner headquartered back in Colorado.  Great company, and great people.  It was a hard decision to leave them.

I recently learned that Statera has been investing a lot of time and energy in developing some cool applications using our Azure Services Platform.  In particular, Statera built an engine running in Windows Azure that integrations Microsoft CRM Online and SharePoint Online (both of which are part of Microsoft’s Business Productivity Online Suite, or BPOS).  It’s a great example of how you can use Microsoft’s cloud to extend existing applications.

This engine is called Stratus – love the name! – and you can find out more about it at http://stratus.statera.com/.

Here’s a quick laundry list of some of the features/capabilities:

  • CRM users can provision SharePoint sites for Account, Opportunity and Case records, allowing them to store documents in a location where they are easier to collaborate on and in an environment where non-sales employees may be more familiar with.
  • CRM users can upload documents while in CRM and they are stored directly in SharePoint.
  • CRM users can view associated SharePoint document libraries and their contents and pull up documents without ever leaving CRM.
  • SharePoint users can navigate to SharePoint sites that are associated with CRM Account, Opportunity or Case records and access uploaded documents, CRM contacts, CRM activities as well as some other general CRM information.
  • SharePoint users can create new contacts or activities via the associated SharePoint site that are in turn stored in CRM.
  • SharePoint users can save CRM contacts directly into their Outlook contacts folder with the click of the mouse.

Evidently, you can also use the engine to link your on-premises versions of SharePoint and CRM to BPOS, and any combination thereof.  Currently, it doesn’t appear that the on-premise activation is available.

Watch these guys, as I’m sure they’ll have other cool things out in the future.  For more information, check out their webcast http://stratus.statera.com/.

.NET Architecture for Enterprises

2009 June 16
by Wade

Yesterday I gave a talk on .NET architecture to a very large customer here in Illinois (in case you’re interested, I had to drive down to Bloomington, IL. to visit them).  The description of the talk was pretty bland – “Overview of application architecture for medium to large scale applications.” – so I tried to spice it up a bit.  Here’s the presentation:

Ultimately, my goal in this talk was to address the following:

  • Provide an introduction to software architecture – i.e. the practice of architecture, the role of an architect, architecture patterns & anti-patterns, methodologies, and the like – to developers that typically live in a world of software implementation.
  • Explore the practical aspects of applying architectural practices in .NET.

I had a lot of fun preparing for this talk, and I sincerely hope that the audience found it valuable.  I hope to eventually record this as a webcast and then break off into more specific topics that dig into different areas.

The Commerce Server book is now available!

2009 May 28
by Wade

Update (5/31/09) - as Glen notes in the below comment, you can order the book through an English language site.  Please seen the instructions in on his blog (http://www.software-smith.com/csbook.html) or go directly here for the site (still a little German, but it’s easier to follow in English).

Commerce Server 2007The first – and only – book on Commerce Server 2007 is now available! (Here’s the translated version, thanks to Microsoft Translator - http://bit.ly/Gfm7R; note that you cannot purchase the book when using translator.)

Unfortunately, the only location you can currently purchase the book is from Amazon.de – not sure why it’s not available at Amazon.com, but hopefully this gets resolved soon.  I can assure you that this is an English version, and not German.

That aside, I really hope you find this a useful book.  It’s been a longtime coming, but it’s here now!  Go buy it!  If you have any issues purchasing this book, please let me know ASAP.

Running a PHP application on Windows Azure

2009 March 23
by Wade

I recently returned from MIX09 – what a fantastic event! So many great things were announced; so many that there’s no way I can list them all here. Please visit http://live.visitmix.com/ for access to the keynote videos and all the breakout sessions. I would also recommend reading the day one recap and the day two recap.

One of the major announcements was new capabilities for the Windows Azure CTP. This includes the support for FastCGI, Full Trust, and Geo-location. Please take a look at the Windows Azure team’s blog for all the announcements.

In this post, I want to walk you through the step you need to take to run a PHP application on Windows Azure. As you’ll see, it’s quite simple and straightforward – after a few configuration updates and additional steps, you can run nearly any PHP application. This walkthrough assumes that you have installed the latest bits for Windows Azure. If you are just getting started with Azure, please visit http://www.azure.com/ – this is the primary landing page for the Azure Services Platform. If you want to get the updated bits that allows for Full Trust and FastCGI, you can grab the bits at the Windows Azure Tools for Microsoft Visual Studio March 2009 CTP page.

  1. Create a new project. Select File –> New –> Project.
  2. Select Cloud Service project type and select the Blank Cloud Service template. Specify the name/location, and click OK.
    New Windows Azure project
  3. Add a Cgi Web Role project. Right-click the project (e.g. CloudService1) and select New Web Role Project.
    Add a web role project
  4. Specify the Name and click OK.
    Add a CGI web role
  5. By default the Web.roleconfig file is opened in the new project. Remove the comments and update the application fullPath to “%RoleRoot%\php\php-cgi.exe”.
    image
  6. Now we need to actually grab the PHP executable that will bet invoked. Browse to http://www.php.net/downloads.php and grab the non-thread-safe Windows binary (at the time of writing, it is “PHP 5.2.9-1 Non-thread-safe zip package”).
  7. You might as well configure PHP to run on your Windows environment. Rather than specifying all these steps, please see Simon Guest’s post on hosting PHP applications in 5 easy steps. Follow the steps in his post, and at the end you should be able to confirm that PHP works in IIS 7.
  8. Copy all the files in the \PHP folder you downloaded into your Cgi Web Role project. XCopy works really well. For example, open a command prompt in the root of your Cgi Web Role project and type:
    xcopy /s \php php
    When asked, specify “D for directory”. This will copy everything from c:\php into a folder called php in your Cgi Web Role project. Note that this is where we pointed Windows Azure to in step 5.
  9. Select your Cgi Web Role project, show all files, and add the new php folder into your project. This way it will get packaged up with the rest of your application and deployed to Windows Azure.
  10. Open up the Web.config file and specify a new default document. This will tell Windows Azure to open up our PHP page by default. Under the <system.webServer> element, add a <defaultDocument>:
    defaultDocument
  11. Additionally, we need to setup a handler in the Web.config file. Add the following code to the <handlers> element in the web.Config:
    image
  12. In order for this to function, we have to enable native code execution in Windows Azure. To do so, open the ServiceDefinition.csdef file and set the enableNativeCodeExecutiion flag to true:
    enableNativeCodeExecution
  13. Now, let’s create a new PHP file. Right-click on your Cgi Web Role and add a new item. Select the Text File template and change the name to index.php. Click Add.
    index.php
  14. To test, create some PHP code. A quick, but effective, test is to simply echo “Hello World”. In the index.php file, write the following code:
    hello world
  15. Press F5, and your browser should open and show you the following page:
    Hello World!

And that’s all it takes!

While this is a simple demonstration, I hope it impresses upon you the impact that allowing native code execution has in Windows Azure. With native code execution, and fast CGI support, you can now leverage all the rich content created in the PHP community on Windows Azure. The story even gets better with the new relational capabilities of SQL Data Services, but I’ll save that for another post.

I hope this helps!