Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices | Where to put search logic if we have different repository for each entity [closed]

I am working on an application in which have different repositories for different entities and now i have to put search logic, hence i am confused where should i put my search logic, should i create a new repository for search or should i put the logic in the existing repository, and if i should put in some existing repository the which one is it.

the repositories are listed below

public class VendorRepository
{

}

public class ProductRepository
{

}

Public Class ProductBatchRepository
{

}
like image 948
rajansoft1 Avatar asked Jun 04 '13 10:06

rajansoft1


People also ask

Do you need a repository for each entity?

It depends on your logic and how "important" are every entity. For example, if you had the entities User and Address you could have UserRepository and AddressRepository. But only UserService, with methods like addAddress(User user, Address address)...

How should I be grouping my repositories when using repository pattern?

Repository should be per Aggregate root and not table. It means - if entity shouldn't live alone (i.e. - if you have a Registrant that participates in particular Registration ) - it's just an entity, it doesn't need a repository, it should be updated/created/retrieved through repository of aggregate root it belongs.

What repository pattern should return?

As the repository is an abstraction, it should always return whatever the layer above wants to work with, which in most cases are domain entities, i.e., the objects which will encapsulate the logic in your business code.

When should you use repository pattern?

The Repository pattern allows you to easily test your application with unit tests. Remember that unit tests only test your code, not infrastructure, so the repository abstractions make it easier to achieve that goal.


3 Answers

Why not a base repository? Something like this:

public class GenericRepository<T>
{
    protected IDbSet<T> Query<T>()
    {
        return myContext.GetDbSet<T>();
    }

    public IEnumerable<T> Where<T>(Expression<Func<T, bool>> predicate)
    {
        return Query<T>().Where(predicate).ToList();
    }

   ...
}

and then the repositories:

public class ProductRepository : GenericRepository<Product>
{

}

public class VendorRepository : GenericRepository<Vendor>
{

}
like image 135
Bidou Avatar answered Oct 22 '22 05:10

Bidou


Alternative approach that I like to use...

...you could seperate commands from queries. Your commands would use the underlying domain model and repositories (as you have it now?); while your queries could use another technology; for example; query the database directly using entity framework code first.

This way you have all the flexibility of querying (using LINQ) and bypassing layers that don't add value to query purposes, and design the return types specifically for your clients. You can even use database views if that would make it easier to query.

like image 45
L-Four Avatar answered Oct 22 '22 05:10

L-Four


I would create methods for searching every entity in respective repository and create another class that would call all of them to search everything and transform the results in the desirable shape ...

like image 44
Senad Uka Avatar answered Oct 22 '22 06:10

Senad Uka