views:

68

answers:

2

Hi all, i'm writing an asp.net mvc 2 web application with Entity Framework 4.0. I have the views that should display items, views that are for login, etc. Should i structure the project with a repository pattern? If yes, how? Should i create an interface for basics methods like Add, Update, Delete, etc and another class that use the repository for the common methods like login validation, etc?

+1  A: 

I use EF with the repository pattern, it saves me hell a lot of work!

Small Respository Pattern Intro:

someRepository.Find.Where(something => something.IsRed && something.IsBig)

Create a generic interface called 'IRepository' of type containing all the methods for data access.

It could look like this:

interface IRepository<T> where T : class
{
    IEnumerable<T> FindAll(Func<T, bool> exp);

    T FindSingle(Func<T, bool> exp);
}   

Create an abstract 'Repository' class implementing this interface:

class Repository<T> : IRepository<T> where T : class
{
    TestDataContext _dataContext = TestDataContext(); // Would be your EF Context

    public IEnumerable<T> FindAll(Func<T, bool> exp)
    {
        _dataContext.GetTable<T>().Where<T>(exp);
    }

    public T FindSingle(Func<T, bool> exp)
    {
        _dataContext.GetTable<T>().Single(exp);
    }
}

We can now create an interface for the something table/objects which implements our 'IRepository' and a concrete class extending the abstract 'Repository' class and implementing the 'ISomethingInterface':

interface ISomethingRepository : IRepository<Banner>
{
}

And the matching repository to implement it:

class SeomthingRepository : Repository<Banner>, IBannerRepository
{
}

I would suggest using this approach as it gives you a lot of flexibility as well as enough power to control all the tiny entities you have.

Calling those methods will be super easy that way:

SomethingRepository _repo = new SomethingRepository ();

_repo.Find.Where(something => something.IsRed && something.IsBig)

Yes, it means that you have to do some work but it is hell easier for you to change the data source later on.

Hope it helps!

Shaharyar
how should i structure my application? I have actually 3 projects in 1 solution: the first is the web application, the second one a framework with my custom functions, etc and the latest one actually contains only the EDMX file with the POCO classes autogenerated by the entity framework designer
Stefano
with entity framework 4.0 i don't have a DataContext class, so i don't have available the GetTable method... How you fixed this?
Stefano
A: 

The most complete answer you can get is probably here:

http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/

Quite a lot to read and figure out, but if you really want to understand how to use the repository pattern with EF, that's probably the most complete source.

Some simpler sample code to get a quickstart can be found here:

http://www.forkcan.com/viewcode/166/Generic-Entity-Framework-40-Base-Repository

You can also find different variation + discussions (e.g. whether it's a good idea to return IQueryable's from the repository, etc.

Yakimych
the first link you gived me us EF4 CTP4 that is currently s preview... can i use it?
Stefano
The CTP comes in the updates to the post. Those parts are marked, so if you don't want to install the CTP, you can follow the older code (with the `ContextBuilder`). In case you just want to reuse the packaged source code (from the downloads), you can certainly install the CTP.
Yakimych
am I wrong or ctp isn't licensed for production apps?
Stefano
Not sure actually, they were planning a go-live licence, but I am not aware of the status on that. You can ask in the pre-release forum: http://social.msdn.microsoft.com/Forums/en-US/adonetefx/threads. Then again, you don't HAVE to use it if you don't want to.
Yakimych
should i use EF 4 with POCO classes or NHibernate for a production app?
Stefano
That's a completely different question and it isn't that related to the repository pattern - the implementations will be quite similar no matter which ORM you choose. Regarding ORM comparison, EF4 and nHibernate both have pros and cons, both are suitable for production applications, and it's only up to you to choose. http://dotnetslackers.com/articles/ado_net/A-Feature-driven-Comparison-of-Entity-Framework-and-NHibernate-Multiple-Databases.aspx
Yakimych
if i need to extract record from the repository and use them to make particular operations, should i create a class with functions that take the record from the repository, use them and give me the result?
Stefano