Base solution for your next web application
Open Closed

When to create custom repositories ? #455


User avatar
0
nobruds created

Hello,

I think I am doing something wrong about custom repositories.

If I need some logic or filters to be made, I create a custom repository I put that in there, not right ?

I saw on ModuleZeroSampleProject that if you need to perform filters on DB you put that on the ApplicationService

So, What is the best practice here: NHibernate Layer (custom repository)

var result = Session.QueryOver<ConfiguracaoProduto>()
                        .Where(c => c.ProdutoEstruturaID == produtoEstruturaID)
                        .SingleOrDefault();

or

Application layer (service)

private readonly IRepository<ConfiguracaoProduto> _produtoRepository;
var result = _produtoRepository.Single(p => p.ProdutoEstruturaID == produtoEstruturaID);

4 Answer(s)
  • User Avatar
    0
    moretl created

    I think the better place is in domain service !

    domain services return entity

    application services ask to domain service, get entity and return a dto with automapper...

    here, <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Domain-Services">http://aspnetboilerplate.com/Pages/Docu ... n-Services</a> you can read this : "Unlike Application Services which gets/returns Data Transfer Objects, a Domain Service gets/returns domain objects (like entities or value types)."

  • User Avatar
    0
    nobruds created

    Oh, i missed the domain services. I will check it, thanks.

    Edit: But... As you can see on [http://aspnetboilerplate.com/Pages/Documents/Application-Services]) CreatePerson method has:

    var person = _personRepository.FirstOrDefault(p => p.EmailAddress == input.EmailAddress);
    

    SearchPeople method has:

    var query = _personRepository.GetAll();
    

    so my point is, those filters (rules) should be on Application Services not in a Custom Repository right ?

    thanks

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    If you need to filter/sort entities and get a list of entities, you have two options:

    1. In application service, just call Repository.GetAll().Where(...).OrderBy(...).ToList(); I prefer it normally. Because App services can use repository. And this filters is UI and case specific, not related to your domain logic at all. They should not be in domain service.

    2. Add a custom repository method for each such query. This has just one advantage: If you need to replace query by a stored procedure or change EF to a different ORM which does not supports IQueryable, your application services does not effected, you just change the repository method implementation.

    So, it's your choice. If you don't think to change to an non queryable ORM, then prefer 1st one.

  • User Avatar
    0
    nobruds created

    Great

    thanks hikalkan

    <cite>hikalkan: </cite> Hi,

    If you need to filter/sort entities and get a list of entities, you have two options:

    1. In application service, just call Repository.GetAll().Where(...).OrderBy(...).ToList(); I prefer it normally. Because App services can use repository. And this filters is UI and case specific, not related to your domain logic at all. They should not be in domain service.

    2. Add a custom repository method for each such query. This has just one advantage: If you need to replace query by a stored procedure or change EF to a different ORM which does not supports IQueryable, your application services does not effected, you just change the repository method implementation.

    So, it's your choice. If you don't think to change to an non queryable ORM, then prefer 1st one.