Category Archives: Entity Framework

Entity Framework POCO, Repository and Specification Pattern [Upgraded to EF 5]

[Updated on Nov 18 2012]: The source code now moved to github @ https://github.com/huyrua/efprs. Let start making pull requests¬†yourself ūüôā

I am happy to announce that the framework has been upgraded to target .NET 4.5, Entity Framework 5 and Visual Studio 2012. This post is also a response to someone asking me when should the framework being upgraded to Entity Framework 5.

Some notes regarding this upgrade:

– The source code is now restructured to two versions: one for .Net Framework 4.0, the other one for .Net Framework 4.5. You can access to the source code at http://ef4prs.googlecode.com/svn/trunk/ef4prs-read-only. However, the download page only shows specific versions for you to choose which version that best fits your need.

– Removed repository implementation against ObjectContext, this means the repository is now interfacing mainly with DbContext (not a Lab version anymore). So, if you prefer to work with DbContext, you can get the framework up and running quickly. On the other hand, if you want to work with ObjectContext, this is also easy by upgrading/migrating code from .NET 4.0 version.

That’s it! Not much changes from this upgrade, but that’s a good sign to me since EF 5 does not break some low level code.

Download links http://code.google.com/p/ef4prs/downloads/list

Cheers.

Separate Binary Data Out Of An Entity For Effective Loading

I received an email this morning asking about loading binary data related to an entity, here is the excerpt:

Hello, 

I was reading the following post on your blog and checking the code in Google Code. Entity Framework 4 POCO, Repository and Specification Pattern [Upgraded to EF 4.1]

I found it really interesting and realized that the performance issues where solved.

But how do you handle binary data?

Consider you have an entity with 2 Byte[] fields (varbinary or filestream in the database).

public class Product
{
    public Int32 Id { get; set; }

    public Boolean Available { get; set; }

    public String Name { get; set; }

    public Byte[] Image { get; set; }

    public Byte[] Brochure { get; set; }
}

When N records are loaded you will get N * 2 files being loaded even if you want to display only the names.

One solution to this problem is to apply a projection. For example:

context.Products.Where(x => x.Available).Select(x => new ProductView { Available = x.Available, Name = x.Name, Image = x.Image });

In this case I am leaving out the brochure but including the image ‚Ķ How do you address this with your code?”

—————————–

Here is my answer:

In my experience regarding the binary data, I would create another entity, says, BinaryData which is somewhat like below (of course there will be a corresponding BinaryData table in the database):
public class BinaryData
{
    public Int32 Id { get; set; }
    public Byte[] Data { get; set; }
}
Then within an entity that has binary data, like your Product with Image or Brochure properties, I just add dependencies (or relationship) to the BinaryData entity:
public class Product
{
    // ... other properties
    public BinaryData Image { get; set; }
    public BinaryData Brochure { get; set; }
}

With lazy-loading enabled, you won’t worry about loading binary data (Image, Brochure) when loading Product, but it will be loaded when you access to it like: product.Image”

I myself think it is a good design practice when working with ORM framework, like Entity Framework or NHibernate.

What do you think? If there is a better solution for this, please drop a comment.

Entity Framework 4 POCO, Repository and Specification Pattern [Upgraded to EF 4.1]

I am excited to announce that the framework has been upgraded to the new version to follow up with the final release of Microsoft ADO.NET Entity Framework 4.1 yesterday. The upgrade includes some changes from Entity Framework API itself and also incorporates bug fixed as well many great suggestion/comments for improvement from the readers. You can download the latest version of the framework (1.4 at this writing) at http://code.google.com/p/ef4prs/downloads/list and play around with it. Note that to be able to compile the code, you first need to download and install EF 4.1 using standalone installer or by adding nuget package to your project.

If you are new to this framework and the design idea behind it, take some time to read these posts first:

Happy coding!

Entity Framework 4 POCO, Repository and Specification Pattern [Upgraded to CTP5]

Before reading this, you might want to read these two posts first:

You might have been aware that MS just released Entity Framework 4 CTP5 some days ago. There have been some comments asking me when the data access layer would be upgraded to that version. Since I had been quite busy these days,  the answers for these questions was just a promise that I would do the upgrade when I had time after I finished investigating the new release carefully. However, some readers seem to be anxious waiting for the code updated so they keep asking me for the progress (and I feel good because I know there are some ones who are interested in the framework I built :)).

So, today I decided to fulfill my promise: the source code has been updated in google code. You can download the latest version (1.3 at this  writing) here http://code.google.com/p/ef4prs/downloads/list

Note: to be able to compile the code, you must first download and install EF4 CTP5.

Some implementation notes:

РThe assembly is now changed to EntityFramework.dll and located at C:\Program Files (x86)\Microsoft ADO.NET Entity Framework Feature CTP5\Binaries\EntityFramework.dll

– Base mapping class has changed from EntityConfiguration<T> to EntityTypeConfiguration<T>

– Entity Framework team seems to concentrate on implementing DbContext much more than ObjectContext. But if you want to access to ObjectContext from DbContext, here is the way:

ObjectContext ctx = ((IObjectContextAdapter)_dbContext).ObjectContext;

– We do not need to register entity any more, just add mapping class(es) to the ModelBuilder then we are all set. Well, this is for your information only, since this framework will do this automatically in the XContextBuilder class.

– The most and important thing, I think, is EF now will automatically map entity relationships without explicitly writing the mapping code. For example:

I have an entity Customer that has many Orders. These two classes will be defined as below:

public class Customer : Entity
{
    public Customer()
    {
        Orders = new List<Order>();
    }

    public virtual string Firstname
    {
        get; set;
    }

    public virtual string Lastname
    {
        get; set;
    }

    public virtual IList<Order> Orders
    {
        get;
        set;
    }

    public virtual DateTime Inserted
    {
        get; set;
    }
}

public class Order : Entity
{
    public Order()
    {
        OrderLines = new List<OrderLine>();
    }

    public virtual IList<OrderLine> OrderLines
    {
        get;
        set;
    }

    public virtual DateTime OrderDate
    {
        get; set;
    }

    public virtual Customer Customer
    {
        get; set;
    }
}

Here is the mapping classes:

public class CustomerMapping : EntityTypeConfiguration<Customer>
{
    public CustomerMapping()
    {
        HasKey(x => x.Id);

        Property(x => x.Firstname).IsRequired().HasMaxLength(25);
        Property(x => x.Lastname).IsRequired().HasMaxLength(25);
        Property(x => x.Inserted);

        //we dont need to write this code to specify the relationship, this will autolatically done by EF
        //HasMany(x => x.Orders).WithRequired(y => y.Customer).HasForeignKey(c => c.CustomerId);

        ToTable("Customer");
    }
}

public class OrderMapping : EntityTypeConfiguration<Order>
{
    public OrderMapping()
    {
        HasKey(x => x.Id);

        Property(x => x.OrderDate);

        // No need to write mapping code to Customer
        //HasRequired(c => c.Customer);

        ToTable("Order");
    }
}

With these mappings, EF will generate the Order table with a foreign key to Customer table as below:

You can download and check out the test code to see other mapping scenarios like many-to-many or how to change the table name in the mapping code.

Happy coding!

Specification Pattern In Entity Framework 4 Revisited

After the post Entity Framework 4 POCO, Repository and Specification Pattern was published for a while, there have been quite a few of positive comments from readers. At first, I thought that this piece of code should have been used as a prototype to demonstrate the implementation of EF POCO, the Repository and Specification pattern. I known it is not the optimized piece of code to everyone and that if there was suggestion for improvement, I would leave to reader as I thought once they understood the design idea, they could extend/change/use the API in anyway they want.

However, there are also some comments concerning about the way the Specification pattern is applied which might cause the BIG performance problem when used that I was not aware of. Even I am using this API in my current work but it’s a shame that I rarely use Specification to query the data but the other methods are enough for me.

For your information, here are the extracted comments from Jon & Buu (many thanks :)) which pointed out the problem:

Jon: Linq to Entities uses expressions to build the SQL that will be executed on the database server. If you use the specification pattern as designed above, i.e., without expressions, the generated SQL is never impacted. Instead, EF will generate ‚ÄúSELECT * FROM Table,‚ÄĚ returning all rows. From there the specification pattern kicks in and filters the data in memory. Your test passed because you got the right result, but not in the right way. it would never work in a real would scenario.”

Buu: ¬†…the cause of the issue as Jon observed is because the method (IsSatisfiedBy) of Specification is fed into the Where method of the query object. That results in LINQ-2-EF loading all records to memory objects and then filter those objects using the passed in method. That‚Äôs a huge performance hit.

To fix the issue, you should feed into the Where method an instance of Expression so that the Where overload in IQueryable is invoked (instead of the one in IEnumerable). The Specification already stores an instance of Expression, so the code change should be straightforward. However, since not all expression operations are supported by LINQ-2-EF, you might end up breaking some existing code. Be warned…”

I verified the problem with Entity Framework Profiler and I have to say the problem is there. Here are some tests I made to retrieve a product by name, and also the results from the profiler:

Use standard lambda expression:

private void FindProductByName()
{
    IEnumerable<Product> products = productRepository.Find<Product>(p => p.Name == "Windows XP Professional");
    Assert.AreEqual(1, products.Count());
}

The output from the profiler

EF generated the correct sql and returned the expected result, no thing wrong with this.

Use specification

private void FindBySpecification()
{
    Specification<Product> specification = new Specification<Product>(p => p.Name == "Windows XP Professional");
    IEnumerable<Product> products = productRepository.Find<Product>(specification);
    Assert.AreEqual(1, products.Count());
}

The output from the profiler

As you can see there is a problem with using specification to retrieve a product in which EF loads all the products from the database, then perform filtering a product against the whole set of products in-memory. If the number of products is huge, it really is an issue since the query is not efficient at all.

This is because the specification pattern is implemented incorrectly. To fix it, I change the specification contract as the following:

public interface ISpecification<TEntity>
{
    TEntity SatisfyingEntityFrom(IQueryable<TEntity> query);

    IQueryable<TEntity> SatisfyingEntitiesFrom(IQueryable<TEntity> query);
}

And the implementation of some generic repository’s methods which accepts a specification are as following:

public class GenericRepository : IRepository
{
    // other code...

    public IQueryable<TEntity> GetQuery<TEntity>() where TEntity : class
    {
        var entityName = GetEntityName<TEntity>();
        return ObjectContext.CreateQuery<TEntity>(entityName);
    }

    public TEntity FindOne<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
    {
        return criteria.SatisfyingEntityFrom(GetQuery<TEntity>());
    }

    public IEnumerable<TEntity> Find<TEntity>(ISpecification<TEntity> criteria) where TEntity : class
    {
        return criteria.SatisfyingEntitiesFrom(GetQuery<TEntity>());
    }

    // other code...
}

The implementation of a simple specification is straightforward.

public class Specification<TEntity> : ISpecification<TEntity>
{
    public Specification(Expression<Func<TEntity, bool>> predicate)
    {
        Predicate = predicate;
    }

    public TEntity SatisfyingEntityFrom(IQueryable<TEntity> query)
    {
        return query.Where(Predicate).SingleOrDefault();
    }

    public IQueryable<TEntity> SatisfyingEntitiesFrom(IQueryable<TEntity> query)
    {
        return query.Where(Predicate);
    }

    public Expression<Func<TEntity, bool>> Predicate;
}

Here is the output of the profiler with the new specification implementation:

As you can see EF now generates the expected sql and returns expected result as well, very efficient.

What about the composite specification?

If you already read the previous post, you should have known that the composite specification was being used to chain the specifications. With the new implementation of specification, the composite specification implementation also needs to change. The technique being applied here is to combine the lambda expression (or predicate) of each specification (left and right side of a composite specification) to create a new lambda expression then this new lambda expression is fed to the IQuerable object for querying.

Here is the code of the extension method of lambda expression which is mostly inspired from here:

public static class ExpressionExtension
{
    public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
    {
        // build parameter map (from parameters of second to parameters of first)
        var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);

        // replace parameters in the second lambda expression with parameters from the first
        var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);

        // apply composition of lambda expression bodies to parameters from the first expression
        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
    }

    public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.And);
    }

    public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Compose(second, Expression.Or);
    }
}

And here is the implementation of the composite specification:

public class AndSpecification<TEntity> : CompositeSpecification<TEntity>
{
    public AndSpecification(Specification<TEntity> leftSide, Specification<TEntity> rightSide)
        : base(leftSide, rightSide)
    {
    }

    public override TEntity SatisfyingEntityFrom(IQueryable<TEntity> query)
    {
        return SatisfyingEntitiesFrom(query).FirstOrDefault();
    }

    public override IQueryable<TEntity> SatisfyingEntitiesFrom(IQueryable<TEntity> query)
    {
        return query.Where(_leftSide.Predicate.And(_rightSide.Predicate));
    }
}

public class OrSpecification<TEntity> : CompositeSpecification<TEntity>
{
    public OrSpecification(Specification<TEntity> leftSide, Specification<TEntity> rightSide)
        : base(leftSide, rightSide)
    {
    }

    public override TEntity SatisfyingEntityFrom(IQueryable<TEntity> query)
    {
        return SatisfyingEntitiesFrom(query).FirstOrDefault();
    }

    public override IQueryable<TEntity> SatisfyingEntitiesFrom(IQueryable<TEntity> query)
    {
        return query.Where(_leftSide.Predicate.Or(_rightSide.Predicate));
    }
}

The test code to find product by name and price which uses the AndSpecification

private void FindByAndCompositeSpecification()
{
    IEnumerable<Product> products = productRepository.Find<Product>(
        new Specification<Product>(p => p.Price < 100).And(new Specification<Product>(p => p.Name == "Windows XP Professional")));
    Assert.AreEqual(1, products.Count());
}

The output from the profiler:

The test code to find product which applies the OrSpecification:

private void FindByOrCompositeSpecification()
{
    IEnumerable<Product> products = productRepository.Find<Product>(
        new Specification<Product>(p => p.Price < 100).Or(new Specification<Product>(p => p.Name == "Windows XP Professional")));
    Assert.AreEqual(2, products.Count());
}

The output from the profiler:

The problem is solved!

The updated source code can be downloaded here.

Once again, comments are welcome.