Published: 28 Jul 2010
By: Brian Mains

Out of the .NET framework 4.0 comes many great enhancements, the ADO.NET entity framework (ADO.NET EF) is among the top. The framework has improved upon some of the previous bugs (in version 1), as well as enhanced the API, including many new LINQ-to-SQL-like improvements. We're going to examine using these API features to create a generic version of of a data repository.

Contents [hide]

The ASP.NET MVC 2 Series

  • Introduction to ASP.NET MVC 2.0 In this article, we'll begin examining the new features of ASP.NET MVC 2.0 by comparing what ASP.NET MVC offers against its predecessor, ASP.NET web forms.
  • ASP.NET MVC 2.0 User Interfaces The next part to this article series on MVC 2.0 is the user interface. We saw in the last article some basics on the changes of the user interface, which we'll delve into more in this article series. Here we will begin to look at how developers can construct the view user interface.
  • ASP.NET MVC 2.0 Using Multiple Actions Brian Mains explains how to use multiple actions in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Templating Templating is now in the ASP.NET MVC 2.0 framework, for .NET framework 3.5 and 4.0. We are going to take a look at these features.
  • ASP.NET MVC 2.0 Attributes An overview of ASP.NET MVC 2.0 Attributes.
  • ASP.NET MVC 2.0 Validation An overview of validation in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Areas A look at ASP.NET MVC 2.0 Areas.
  • ASP.NET MVC 2.0 and AJAX Part 1 ASP.NET MVC makes working with AJAX really easy; it's quite impressive how powerful the framework can be. JQuery adds extra features and makes AJAX calls really easy; we'll see that all here soon.
  • ASP.NET MVC 2.0 and AJAX Part 2 In this article, we are going to look at how you can utilize AJAX in your views, and utilize a concept called unobtrusive JavaScript to take advantage of a variety of situations. ASP.NET MVC provides AJAX features very easily as we previously saw, but this comes with a caveat, and it has mostly to do with the planning of the implementation. We’re going to examine these concerns in this article.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 1 In this article, we'll examine using the ADO.NET Entity Framework (ADO.NET EF). The 4.0 version has received some major upgrades, with some major API enhancements and fixes for most of the pain points within the designer.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 2 Out of the .NET framework 4.0 comes many great enhancements, the ADO.NET entity framework (ADO.NET EF) is among the top. The framework has improved upon some of the previous bugs (in version 1), as well as enhanced the API, including many new LINQ-to-SQL-like improvements. We're going to examine using these API features to create a generic version of of a data repository.
  • Complexity in ASP.NET MVC, Part 1: Dealing With Large Models At times, every developer has a web forms page or MVC view that ends up getting pretty complex, for various reasons. Whether it pulls in and integrates a lot of data (such as a portal page) or allows users to edit a lot of information, applications require data, and the amount of data grows over time. It’s a simple reality. We’re going to examine what a larger ASP.NET MVC view looks like, and some actions we can take to keep the model instantiation code as slim as possible.
  • Complexity in ASP.NET MVC, Part 2: Plan your AJAX Carefully Brian Mains discusses management of Ajax requests in ASP.NET MVC.
  • Using DI Containers in ASP.NET MVC How to use Dependency Injection containers in ASP.NET MVC.
  • Using Microsoft Unity in ASP.NET MVC Brian Mains continues his asp.net series with the unity application block.
  • Entity Framework Overview

    The entity framework has some handy methods for working against its data model generically. The designer, by default, generates a model representing the database (though it's doesn't have to be a 1 to 1 mapping). Each table is represented by an ObjectSet, which in turn has methods to create, update, or reflect on the entity and its relationships. The framework uses an entity key, a value that looks like EntitySet=Customers;CustomerID=4, to uniquely identify an entity and its identifier within the model. Using the entity key provides us a way to update objects, query an object from the database, and more.

    Creating And Updating

    Let's begin by looking at a sample repository base class implementation. I'm going to examine the base CRUD operations separately, starting with create and update options.

    Listing 1: Create/Update Operations

    Our BaseRepository class uses the ObjectContext (created on each request with the CreateContext method) to add new objects with the AddObject method or update existing objects by using the AttachTo method. For creating an object, we need to know which type of method to update. Using our helper GetFullEntitySetName method handles this magnificently. This returns the identifier of the object (a value like DotNetSamplesObjectContext.Customers) to identify the entity to add to.

    For updating an object, we run into issues with the context. Each object queried from the database is tracked by the ObjectContext in the ObjectStateManager. The MVC binding process actually constructs its own copy of the object and injects the form values into it via reflection. This means we have a new object, not attached to the ObjectContext.

    Not really a big problem, we first need to query the old record. This generates an ObjectStateEntry for our entity, and we can successfully perform the update (since it needs to know what the old record was). The entity requires an EntityKey instance too, with the appropriate primary key information (remember the EntityKey is a unique way to identify the existing entity).

    Lastly, calling ApplyCurrentValues applies the new values of the entity created by the MVC framework to the old entity. Here again, we need the entity set name to uniquely identify it.

    Metadata

    We saw uses of the entity set name, used to identify the type of entity in an ADO.NET EF fashion. For instance, it could represent a mapping between the Products table and the Product entity. For instance, for our product repository, it would implement the following to get the entity set.

    Listing 2: Returning the Product entity set name

    We'll see the GetDefaultSortingExpression used very soon. Note that GetFullEntitySetName appends the object context name to the name of the entity set, to get the correct name for adding, updating, etc. to objects.

    Data Retrieval

    We can generically work with some read operations as well, as shown below.

    Listing 3: Reading From the Database

    Every entity class generated by the default designer adds a set of attributes to each of its property. The EdmScalarPropertyAttribute has an EntityKeyProperty setting is set to true for the key field of the entity. This provides a flexible way to identify the primary key column without the need to manually specify it using a lambda.

    Getting into the ObjectContext methods, getting all of the results of an entity from the data can be done by constructing an object set, using the entity set name. The object set can use LINQ extension methods to filter by page index and size, to say only get a result of 20 objects. Unfortunately, calling Skip and Take requires an object sorting, and as such, a custom Lambda expression performs that ordering.

    The GetObjectByKey method actually retrieves the object from its key by its database, and we can use our new GetKeyProperty reflective method to get the name of the primary key property. As you can see, we can't use the key directly, but need an EntityKey object to retrieve it.

    Implementation

    This approach means that I can utilize a repository like the following and already have the Create, Delete, Update, Get, and GetAll methods implemented in the base class. This leaves us only to have to worry about our additional query operations.

    Listing 4: Final Implementation

    In most scenarios, code generation would be the best option to help reduce repetitive code; however, Entity Framework does a lot of the internal plumbing and expose helpful methods to do these features for us, without us having to write code.

    Conclusions

    ADO.NET Entity Framework provides a lot of plumbing to work in generic form and save yourself time writing a lot of code. We examined the various methods of the ObjectContext useful in getting data out and in of the backend database.

    The ASP.NET MVC 2 Series

  • Introduction to ASP.NET MVC 2.0 In this article, we'll begin examining the new features of ASP.NET MVC 2.0 by comparing what ASP.NET MVC offers against its predecessor, ASP.NET web forms.
  • ASP.NET MVC 2.0 User Interfaces The next part to this article series on MVC 2.0 is the user interface. We saw in the last article some basics on the changes of the user interface, which we'll delve into more in this article series. Here we will begin to look at how developers can construct the view user interface.
  • ASP.NET MVC 2.0 Using Multiple Actions Brian Mains explains how to use multiple actions in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Templating Templating is now in the ASP.NET MVC 2.0 framework, for .NET framework 3.5 and 4.0. We are going to take a look at these features.
  • ASP.NET MVC 2.0 Attributes An overview of ASP.NET MVC 2.0 Attributes.
  • ASP.NET MVC 2.0 Validation An overview of validation in ASP.NET MVC 2.0.
  • ASP.NET MVC 2.0 Areas A look at ASP.NET MVC 2.0 Areas.
  • ASP.NET MVC 2.0 and AJAX Part 1 ASP.NET MVC makes working with AJAX really easy; it's quite impressive how powerful the framework can be. JQuery adds extra features and makes AJAX calls really easy; we'll see that all here soon.
  • ASP.NET MVC 2.0 and AJAX Part 2 In this article, we are going to look at how you can utilize AJAX in your views, and utilize a concept called unobtrusive JavaScript to take advantage of a variety of situations. ASP.NET MVC provides AJAX features very easily as we previously saw, but this comes with a caveat, and it has mostly to do with the planning of the implementation. We’re going to examine these concerns in this article.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 1 In this article, we'll examine using the ADO.NET Entity Framework (ADO.NET EF). The 4.0 version has received some major upgrades, with some major API enhancements and fixes for most of the pain points within the designer.
  • ASP.NET MVC 2.0 and ADO.NET Entity Framework - Part 2 Out of the .NET framework 4.0 comes many great enhancements, the ADO.NET entity framework (ADO.NET EF) is among the top. The framework has improved upon some of the previous bugs (in version 1), as well as enhanced the API, including many new LINQ-to-SQL-like improvements. We're going to examine using these API features to create a generic version of of a data repository.
  • Complexity in ASP.NET MVC, Part 1: Dealing With Large Models At times, every developer has a web forms page or MVC view that ends up getting pretty complex, for various reasons. Whether it pulls in and integrates a lot of data (such as a portal page) or allows users to edit a lot of information, applications require data, and the amount of data grows over time. It’s a simple reality. We’re going to examine what a larger ASP.NET MVC view looks like, and some actions we can take to keep the model instantiation code as slim as possible.
  • Complexity in ASP.NET MVC, Part 2: Plan your AJAX Carefully Brian Mains discusses management of Ajax requests in ASP.NET MVC.
  • Using DI Containers in ASP.NET MVC How to use Dependency Injection containers in ASP.NET MVC.
  • Using Microsoft Unity in ASP.NET MVC Brian Mains continues his asp.net series with the unity application block.
  • <<  Previous Article Continue reading and see our next or previous articles Next Article >>

    About Brian Mains

    Brian Mains is an application developer consultant with Computer Aid Inc. He formerly worked with the Department of Public Welfare. In both places of business, he developed both windows and web applications, small and large, using the latest .NET technologies. In addition, he had spent many hou...

    This author has published 73 articles on DotNetSlackers. View other articles or the complete profile here.

    Other articles in this category


    Code First Approach using Entity Framework 4.1, Inversion of Control, Unity Framework, Repository and Unit of Work Patterns, and MVC3 Razor View
    A detailed introduction about the code first approach using Entity Framework 4.1, Inversion of Contr...
    jQuery Mobile ListView
    In this article, we're going to look at what JQuery Mobile uses to represent lists, and how capable ...
    Exception Handling and .Net (A practical approach)
    Error Handling has always been crucial for an application in a number of ways. It may affect the exe...
    JQuery Mobile Widgets Overview
    An overview of widgets in jQuery Mobile.
    Book Review: SignalR: Real-time Application Development
    A book review of SignalR by Simone.

    You might also be interested in the following related blog posts


    Learning MVC - Part 5 Repository Pattern in MVC3 Application with Entity Framework read more
    Learning MVC Part 6: Generic Repository Pattern in MVC3 Application with Entity Framework read more
    Oredev Wrap-Up read more
    Why not Classic (Legacy) ASP? read more
    MvcContrib working on Portable Areas read more
    Scenarios for WS-Passive and OpenID read more
    More On The CodePlex Foundation read more
    Lots of buzz about MvcContrib! read more
    Data Access Guidance read more
    Checking out one of the new stored procedure features in EF4 read more
    Top
     
     
     

    Please login to rate or to leave a comment.