Architecting ASP.net MVC App to use repositories and services

僤鯓⒐⒋嵵緔 提交于 2019-12-06 03:47:15

This is the side effect of following the Single Responsibility Pattern. If each class is designed for only one purpose, then you're going to end up with a lot of classes. This is a good thing. Don't be afraid of it. It will make your life a lot easier in the long run when it comes to swapping out components as well as debugging which components of your system aren't working.

Personally, I prefer putting more of my domain logic in the actual domain entities (e.g. article.AddComment(comment) instead of articleCommentService.AddComment(article, comment)), but your approach is perfectly fine as well.

I think you are headed in the right direction. The question is how to instantiate your services? I'm no MVC.NET guru, but have done plenty of service oriented Java projects and exactly the pattern you are discussing.

In Java land we would usually use Spring to inject singleton beans.

1) You can do the same thing in .NET, using dependency injection frameworks.

2) You can instantiate services as needed in the method, if they are lightweight enough.

3) You can create static service members in each controller as long as you write them to be threadsafe, to reduce object churn. This is the approach I use in many cases.

4) You can even create a simple, global service factory that all controllers access, which could simply be a class of singletons.

Do some Googling on .NET dependency injection as well.

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