Can I access a repository from presentation layer?

风流意气都作罢 提交于 2019-12-06 10:36:13

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!

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!

Hannoun Yassir

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

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.

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