locked
Repository; ServiceLayer, DI and constructor-over- injection RRS feed

  • Question

  • Hi there
    I´ve been reading alot about repositories, service layers, DI and containers.
    In most most of the cases the service layer that serves as an example is someting like ProductService that is injected with a ProductRepository and then the service as methods like Add, Delete,
    GetByID etc. The Add method applies business rules in the service layer that uses repository wich, in turn, intermediates between Model and database.
    Everyting is quite simple and the concept understood.
    Wanting to apply these concepts i started to design layers in this fashion.
    A doubt occurs; in my current context i have business entities such as the following:
    Play (main entity)
    Age(a play as an age)
    XPlay(possible association to a counterpart imported from other system)
    Block(a play always belongs to a block)

    To create a play i have to associate an Age, XPlay(or not) and a Block. If i where to follow the above example i have two options in my opinion:
    1 - Service layer applies rules, so PlayService has to have in it´s constructor PlayRepository, AgeRepository, BlockRepository and XPlayRepository. The Add could be implemented using all these
    dependencies and apply the business rules because it has all th others entities at hand. On the other hand Delete only needs PlayRepository and makes no use of other dependencies.

    2- Have a Service per entity
    PlayService
    AgeService
    BlockService
    XPlayService

    Combine them to enforce the rule

    PlayService.Add(play)
    AgeService.AssignToPlay(Play play)
    BlockService.AddPlay(Play play)
    XPlayService.AssociateToPlay(Play play)

    My question is this:wich approach is the more correct? First option leads to constructor-over injection the second leaves the question where(in the solution) to enforce these rules.
    Either way, each Service will have one more dependency like IValidationRules so constructor overinjection is a concern to me.

    I think that´s it
    Thanks


    Jorge Vinagre

    Wednesday, January 23, 2013 11:20 AM

All replies

  • Hi there

    Allow me to answer myself.

    There is another option that i think doesnt break the single responsability principle: If i have to insert a new entity with associated existing entities this can be enforced  at repository implementation level where i have access to DbContext or whatever abastraction.

    This way the service as only the PlayRepository that has access to the "extended" repository

    I remembered that the repository is per aggregate root.

    Am i right?

    Thanks


    Jorge Vinagre

    Thursday, January 24, 2013 11:07 AM