Archive for the ‘Architecture’ Category.

Architecting Your Data Access Layer with the Entity Framework

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:

EF

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.

ADO.NET Entity Framework & Your Data Access Layer ame="allowFullScreen" value="true" />
View more documents from Wade Wegner.

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

I hope this helps!

.NET Architecture for Enterprises

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.

Building Multi-Enterprise Business Applications on the Azure Services Platform

I had the honor and privilege to present with Jack Greenfield at PDC2008 this past week.  We discussed our use of the Azure Services Platform – the new Microsoft cloud services layer recently announced at PDC2008 – to build Multi-Enterprise Business Applications (MEBAs).  As Jack says in the presentation, the Azure Services Platform has the potential to bring about a resurgence on business-to-business (B2B) in a way that hasn’t been possible before.  Our mission was to figure out how to use Azure to build out the MEBAs of tomorrow.

It was an incredible journey to PDC, and I plan to post a number of blog entries over the next couple weeks highlighting our day one keynote with Bob Muglia and RedPrairie (skip to minute 59:00), our use of the Azure Services Platform to build a supply chain application, and lots of code and patterns that show how we did it.  For now, I’d like to share with your our breakout session at PDC2008.  To watch our presentation, please click the picture below:

Behind The Scenes: How We Built a Multi-Enterprise Supply Chain Application

Our session abstract:

Learn how we built a multi-enterprise supply chain application using the cloud services platform. Today, most organizations are deeply interconnected, and business applications span multiple enterprises. See how we built an application that satisfies requirements around connectivity, identity, orchestration, and storage, providing a scalable, pervasive, highly available, general-purpose platform that replaces custom software and infrastructure. Hear about the core requirements for multi-enterprise business applications, their architecture, and the cloud-based framework we built to support them. Learn about the issues we encountered, lessons learned, and useful guidelines and patterns.

If you are interested in building out higher-level services on top of the Azure Services Platform, please let me know!  We are looking for great customers that want to explore the many possibilities that the cloud has to offer.

Enjoy!