MVC DDD: Is it OK to use repositories together with services in the controller?

前端 未结 5 538
情歌与酒
情歌与酒 2020-12-31 15:51

most of the time in the service code I would have something like this:

public SomeService : ISomeService
{
    ISomeRepository someRepository;
    public Do(         


        
5条回答
  •  情话喂你
    2020-12-31 16:19

    My own rough practices for DDD/MVC:

    • controllers are application-specific, hence they should only contain application-specific methods, and call Services methods.
    • all public Service methods are usually atomic transactions or queries
    • only Services instantiate & call Repositories
    • my Domain defines an IContextFactory and an IContext (massive leaky abstraction as IContext members are IDBSet)
    • each application has a sort-of Composition Root, which is mainly instantiating a Context Factory to pass to the Services (you could use DI container but not a big deal)

    This forces me to keep my business code, and data-access out of my controllers. I find it a good discipline, given how loose I am when I don't follow the above!

提交回复
热议问题