A Layered ASP.NET MVC Application – Part I

Posted: April 12, 2009 in .NET, ASP.NET
Tags: , ,

Normally while developing ASP.NET applications we follow a layered architecture with the Web/Presentation layer decoupled from the core business logic via the Service Interface.Whereas most of the examples we come across in ASP.NET MVC, the Model (O/R layer) seems to be very tightly integrated with the web application.I just started how I can seperate out the O/R layer and expose it via a WCF service and make the ASP.NET MVC application talk to the this service only.While doing so I found out that to seperate the different components into individual layer it is extremely important to understand the underlying application design patterns, how and where they fit in.In this post I will discuss about the Object Relational Mapping patterns and how it can help us to isolate the O/R.The three object relational mapping patterns discussed in this post are from Martin Fowler’s Patterns of Enterprise Application Architecture.

1) Metadata Mapping 

In an object relational mapping layer one of the key tasks is to implement a mapping between the database tables and columns with objects and fields.This pattern provides design solution to this problem.There are two ways to approach this problem:

a) Using Reflection – Mapping information and method/field names are maintained in configuration files.At runtime methods are invoked on the fly using reflection.This method is very dynamic and flexible from maintenance perspective, for any changes in database schema only mapping configuration needs to be changed.The major drawback is the performance overhead of reflection.

b) Code Generation – Here based on the database tables, code for classes are auto generated by some tool.The configuration files contains map between database table/columns and classes/fields newly generated.This method is much better from performance standpoint.But there is an overhead from maintenance perspective as any change database schema leads to code generation and compilation.

The Entity Data Framework that is shipped with .NET 3.5 SP1 takes the code generation route for O/R Mapping.When we create ADO.NET Entity Data Model it creates a .edmx file and generates a set of classes.The edmx file is basically an XML file which contains three sections:

  • StorageModel - Information database storage like tables/columns etc.
  • Conceptual Model – Information about the auto generated classes/fields
  • Mapping – Mapping between the conceptual and storage model.

2) Query Object

We use SQL,a language which database understands to perform all the CRUD operations.So while working with O/R mapping we need to have objects using which we can express all our query operations.This object should internally generate the SQL and developers will only deal with objects.The Query Object patterns addresses this problem area.

.NET 3.0 onwards we have querying features built into C# and VB through LINQ.But this is not a Query Object.Because LINQ is a pure language feature and nothing to do with database.However LINQ to SQL or Entity Data Framework adds O/R mapping capabilities where LINQ is used to query the object collections.Among the auto generated classes with Entity Framework there is a class which inherits from System.Data.Objects.ObjectContext.This object internally generates and fires the SQLs.ObjectContext along with LINQ gives us facility to fire queries on databases and object collections but none of this is an exact solution to the problem Query Object pattern tries to address.

3) Repository

Along with Metadata Mapping and Query Object we need something more which will provide a clean interface, modelling the CRUD operations in terms of objects and object collections.This is what Repository tries to address.It provides another layer of abstraction between the domain objects and meta data mapping providing collection like interfaces.Client code interacts only with Repository interface and Repository internally manages the interaction with Meta Data Mapping and Query Objects.For example we can have a Contact Repository interface like

public interface  IContactRepository {
    Contact GetContact(int ContactId);
    IEnumerable<Contact> GetContactList();
    void AddBookMark(Contact Contact);
    void UpdateContact(Contact Contact);
    void DeleteContact(Contact Contact);

}

The implementation of this Repository interface will determine what to use, LINQ to SQL or EF and how to use it.Client code is only bothered about the interface definition.

In the next post I will describe with a code sample how to implement a repository library with Entity Framework.

About these ads
Comments
  1. [...] my last post I had discussed about the various O/R mapping patterns and Repository is one of them.In this [...]

  2. [...] Originally posted here: A Layered ASP.NET MVC Application – Part I « Sankarsan’s Journal [...]

  3. [...] to VoteA Layered ASP.NET MVC Application – Part I (4/12/2009)Sunday, April 12, 2009 from sankarsan.wordpress.comNormally while developing ASP.NET applications we [...]

  4. [...] has a nice tutorial about writing a layered ASP.NET MVC application with Entity Framework. In the first part, he presents the options for the O/R layer isolation. In the second part, he creates the [...]

  5. fysicusje says:

    I’m a novice when it comes to MVC, just started working on my 1st project. 3 years have passed however since your article and me starting on my project.

    I’m using Visual Studio 2010 (.NET 4) and ASP .NET MVC 3. Are there some things I should do differently in this technology?

    I do have a question about the repository pattern, don’t really know what it’s for. Could you provide me with some more information on the subject?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s