Can I access a repository from presentation layer?

十年热恋 提交于 2019-12-07 23:29:21

问题


I am starting with DDD. I am a bit confused with the interaction between the several layers involved in a DDD application.

Can I call my repositories from my presentation layer? If not do I have to replicate the CRUD functionality provided by the repositories in my service layer (which ofcourse will in turn use the repository for these functions)? What would be the best way to do this?


回答1:


What exactly do you mean by presentation layer? If you mean the Controller/Presenter, then it's perfectly fine. The rule of thumb that I've used is if the controller action is 4 lines of code or more I should look at refactoring to an application service class but still - both are at the application level and you can certainly use repositories there.

The way you phrased your question - and I could certainly be misinterpreting this - sounded suspiciously like you were thinking of referencing the repository from your view or codebehind. In that case I would say no! no! no!




回答2:


Absolutely, you can call a repository from the presentation layer. My one piece of advice is to have your presentation layer depend on an abstraction of a repository as opposed to the implementation, i.e. UserSession depends on an IPersonRepository interface instead of a PersonRepository class. Not only is it a good separation of concerns, but it can make testing easier.

Go for it!




回答3:


Well I usually use a service layer but there is no harm in using your repository




回答4:


As Kevin says, going with a controller or presenter gives you many benefits.

These includes clear separation of concerns as well as testability.

Personally, in an ASP.NET setting, I'd go for a «passive view» Fowler.

You simply design your page contracts as interfaces which have concrete implementations as web forms, and then you just stub/mock those when testing your presenters.

The only drawback that comes to mind is that you end up with more code than in a "traditional" web form solution.



来源:https://stackoverflow.com/questions/1394967/can-i-access-a-repository-from-presentation-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!