• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTY2

Related Code
Generic Entity Framework 4.0 Base Repository with Paging
Keep Entity Framework ObjectContext in a WCF Service OperationContext
A Default Entity Framework 4.0 ObjectContext Factory
Loading Strategy for Entity Framework 4.0
Generic linq-to-sql repository
UnitOfWork Action filter for ASP.NET MVC and nHibernate
ForEach Extension method for IEnumerable<T>
Invoke Extension Method
Use of Extension methods to hide "infrastructure code"
Unity Service Locator for ASP.NET MVC 3.0 Beta 1
Null Dot "Operator" Extension Method
WCF RIA Services Unity 2.0 DomainService Factory
Make sure the web.config pages/namespace can be used together with ASP.NET MVC 3 P1 and Razor
Custom C# exception class
Email validation with Regular Expression
Reflection dynamically get private fields or properties
Url Validation with Regular Expression
Method to hash passwords
Number of sealed / unsealed types in the framework
Extension to the HttpClient

Generic Entity Framework 4.0 Base Repository

This is a generic base class you can use when creating a Repositories and EF 4.0

8
2.72k 1 0 0 0 9

public interface IRepository<T> where T : class
{
    IQueryable<T> GetQuery();

    IEnumerable<T> GetAll();

    IEnumerable<T> Find(Expression<Func<T, bool>> where);

    T Single(Expression<Func<T, bool>> where);

    void Delete(T entity);

    void Add(T entity);
}


public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : class
{
   readonly IObjectContext _objectContext = null;
   readonly IObjectSet<TEntity> _objectSet = null;

   public BaseRepository(IObjectContext objectContext)
   {
       if (objectContext == null)
          throw new ArgumentNullException("objectContext");

       _objectContext = objectContext;
       _objectSet = _objectContext.CreateObjectSet<TEntity>();
   }

   public IQueryable<TEntity> GetQuery()
   {
       return _objectSet;
   }

   public IEnumerable<TEntity> GetAll()
   {
      return _objectSet.ToList();
   }

   public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> where)
   {
      return _objectSet.Where(where);
   }

   public TEntity Single(Expression<Func<TEntity, bool>> where)
   {
      return _objectSet.SingleOrDefault(where);
   }

   public void Delete(TEntity entity)
   {
      _objectSet.DeleteObject(entity);
   }

   public void Add(TEntity entity)
   {
      _objectSet.AddObject(entity);
   }
}


To get more information about the IObjectContext check this code out: Keep Entity Framework ObjectContex in a WCF Service OperationContext


Share: twitter | facebook   Action: used | fork | flag

.net

Mark '.net' tag as 'like'

Mark '.net' tag as 'ignore'

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

ef4.0

Mark 'ef4.0' tag as 'like'

Mark 'ef4.0' tag as 'ignore'

objectcontext

Mark 'objectcontext' tag as 'like'

Mark 'objectcontext' tag as 'ignore'

repository

Mark 'repository' tag as 'like'

Mark 'repository' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 10, 2010 10:04 AM
edited July 15, 2010 11:19 PM

Fork

 Generic Entity Framework 4.0 Base Repository -  @fredrikn Saturday 10, 2010 10:04 AM
 Generic Entity Framework 4.0 Base Repository with Paging -  @CodingInsomnia Monday 12, 2010 10:43 PM


0 Feedback


You must log in before you can give any feedback


9 Discussion(s)

Newest Oldest
0

I really like this base class and I'm using a very similar one myself, but I'm calling it RepositoryBase<T> ;-).

I'm a bit undecided about the GetQuery method though. While I understand why it is useful, it seems to make the other Get/Find methods unnecessary. The Repository also looses a lot of control over when and how data is being accessed when you expose the IQueryable. This may not be a problem, but is something to have in mind.

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Sunday 11, 2010 5:07 PM

0

What would be the best implementation to get none-tracking entities in order to speed up materialization?

link | flag  | Reply

 @abratland "No. 1"
109
Sunday 11, 2010 7:44 PM

0

The reason of the IQueryable is for those who wants to use it in a subclass.

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Sunday 11, 2010 10:09 PM

1

Some points here:

I would avoid having "Add" and "Delete" on an interface or baseclass like that, simply since there might be entities/AR's that dont support add or delete..

I would also avoud having "Find(Expression ..." or IQueryable there. Every real world project I have been in lately have required us to fetch data from multiple data stores, thus I can only use Linq for the properties mapped to the DB and not for properties that are filled from other stores.

I'd rather go for methods like "FindByXyz" since that places the responsibillity to collect the correct data on the repository developer rather than the repository consumer..

Exposing IQueryable via an interface would make it public to the outside and thus give the repository responsibillity of a query provider rather than a dumb bucket of things..

link | flag  | Reply

 Roggan
15
Monday 12, 2010 1:53 PM

0

The Add and Delete methods are quite necessary to build a proper Repository. According to Fowler, a Repository should be "a collection-like interface" and "Objects can be added to and removed from the Repository".

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Monday 12, 2010 5:04 PM

1

So you would include a Delete method on an "OrderRepository" ? even though the domain rules would specify that Orders may not be deleted (they are cancelled not deleted)

What would you possibly gain by adding such method in that case?

Also note the wording "collection-like"...

link | flag  | Reply

 Roggan
15
Monday 12, 2010 8:01 PM

0

Well, that's a point. But I would argue that if your domain rules states that something cannot be treated as a simple collection, then the repository pattern is probably not right for that specific case.

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Monday 12, 2010 8:30 PM

0

Reply to: CodingInsomnia I would suggest then, that you create separate interfaces for each CRUD operation. That is IRespositoryAdd, IRepositoryUpdate, IRepositoryDelete. Leave the "fetch" interfaces in the base.

Then each aggregate respository, such as OrderRepository, can just compose whichever interfaces it wishes to provide:

public class OrderRepositor : IBaseRepository<>, IRepositoryAdd<>, IRepositoryUpdate<>

A "full" repository, would use all interfaces.

link | flag  | Reply

 @@SArchibald
10
Thursday 22, 2011 7:36 PM

0

Reply to: silverfox1948

Why not make a fork of the code and add your suggestion? :)

link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Wednesday 28, 2011 10:29 AM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010