1

I am new to repositories. I just read about implementing predicates and a Unit of Work (Fowler). I have seen repository interfaces like the following:

public interface IRepository<ET> {
    ET        Add( ET entity);
    ET        Remove( int id);
    ET        Get( int id);
    IList<ET> Get(Expression<Func<T, bool>> predicate);
}

Of course the Unit of Work would inject a data context (Microsoft fan) to the new repository, where the Unit of Work would have a .Save() method, calling Save on all data contexts.

There's no Edit method, so I assume you can modify any Entity that pops out of the Repository then call save changes on the Unit of Work.

Is this correct? Leaky? What am I missing? Do methods of OrderBy need not ever be in a Repository? Should Paging (.Skip().Take()) somehow be implemented in the predicate?

Links to example code out there would be fantastic, especially how to implement the predicate in a repository.

2 Answers 2

5

if you are referring Entity Framework i would you suggest you read this: Link

Update:

I am not a expert in repository pattern, however i do using it in my project now. a part form performance, following is the benefits that i find from this design pattern:

1, Simplify CRUD operation implementations for all entities. with one interface:

public interface IDataRepository<T> where T : class

then you will be able to replicate others very easily and fast

public class EntityOneRepository : IDataRepository<EntityOne>
public class EntityTwoRepository : IDataRepository<EntityTwo>

2, Keeps my code dry. some entities may have their own method for data manipulation. (i.e. store procedure) you can extend it easily without touching other repositories.

public interface IDonationRepository : IDataRepository<Donation>
{
//method one
//method two
//....
}

for the Paging, it can be either done by Skip() and take(), or you can define your own SP in database then call it via EF4. in that case you will benefit from database sp caching as well.

Some time, keeping the code clean and logically readable is also important for a better app structure.

3
  • That's a good start as it mentions Unit of Work and POCOs. It starts adding Repository methods of FindByName(string name), but I couldn't imagine creating a method for every single "find by" you would need. Is there some walk through like that but on implementing predicates in the Repository, especially mentioning things like skip and take? (or the equivalents for paging through records)? Oct 22, 2010 at 3:45
  • Very cool. I added "public List<T> get( Func<T, Boolean> myFilter) { return myInternalProduct.Where( myFilter).ToList<T>(); } which worked pretty well for pre-compiled searches. Now just have to figure out how to make the Expressions work (moving on to the Specification pattern). Oct 22, 2010 at 20:46
  • for paging, personally i hate the way of data-gird binding in asp.net. check this link, that is much better doing it in store proc. tipsntracks.com/316/… you should be able to do it via EF4 as well
    – D.J
    Oct 24, 2010 at 23:10
2

The repository interface you've presented is a very easy-to-use CRUD interface that can work well in many types of applications. In general, I'd rather not have paging and sorting parameters or options on my repository, instead I'd rather return an IQueryable and let callers compose those types of operations into the query (as long as you are IQueryable, a technology like EF or nHibernate can translate those operators into SQL - if you fall back to IList or IEnumerable it's all in memory operations).

Although I avoid paging and sorting I might have more specific operations on a repository to shield business logic from some details. For example, I might extend IEmployeeRepository from IRepository and add a GetManagers method, or something similar to hide the Where expression needed in the query. It all depends on the application and complexity level.

One important note on this sentence in your post:

Of course the Unit of Work would inject a data context (Microsoft fan) to the new repository, where the Unit of Work would have a .Save() method, calling Save on all data contexts.

Make sure you are using a single data context/object context inside each unit of work, because a context is essentially the underlying unit of work. If you are using multiple contexts in the same logic transaction then you'd effectively have multiple units of work.

I have a couple sample implementations in this project: http://odetocode.com/downloads/employeetimecards.zip

The code might make more sense if you read this accompanying article: http://msdn.microsoft.com/en-us/library/ff714955.aspx

Hope that helps,

3
  • The problem with IQueryable is if someone gets a .Count() then processes the items. It queries the database twice. Oct 23, 2010 at 18:57
  • The filter Lambda injected to the Repository allows the Repo to filter data before it leaves the database. In this case, I somehow need to extend that filter to paging. Could I do something like Repo.Get(p => Row_Number() > 9 && Row_Number() < 21)? Oct 23, 2010 at 19:00
  • I haven't figured out how to get the Expressions working. However, the ability to flow pre-compiled Lamdas in the Where clause is extremely helpful. Expressions leading to a Specification pattern would be even more helpful. Oct 23, 2010 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.