构建ASP.net MVC应用程序以使用存储库和服务 [英] Architecting ASP.net MVC App to use repositories and services

查看:142
本文介绍了构建ASP.net MVC应用程序以使用存储库和服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始阅读ASP.net MVC,在对这个概念感到兴奋之后,我开始将我的所有webform项目迁移到MVC,但是我有一个很难保持我的控制器瘦,即使以后所有的好建议(或者我只是不能得到它...)。
我处理的网站有文章,视频,报价...,每个这些实体有类别,评论,可以与它相关联的图像。我使用Linq到sql进行数据库操作,对于每个实体,我有一个Repository,对于每个存储库,我创建一个服务在控制器中使用。



<



  • li> ArticleCommentRepository


    和相应的服务




    • ArticleService

    • ArticleCategoryService ...



    我有的问题是我有一个控制器的文章,类别和评论,因为我认为有ArticleController处理所有这一切可能有意义,但现在我必须通过所有的服务所需的Controller构造函数。所以我想知道这是什么,我做错了。我的服务设计不正确吗?应该创建Bigger服务来封装较小的服务,并在我的控制器中使用它们?或者应该有一个articleCategory控制器和一个articleComment控制器?



    用户查看的页面是所有的,要查看的文章,



    谢谢你,我想告诉你,为什么? !
    我希望我的问题不会太长,无法阅读...

    解决方案

    单一责任模式。如果每个类都只设计一个目的,那么你最终会得到很多类。这是一个好的东西。不要害怕它。这将使你的生活很容易在长期来换换组件,以及调试哪些组件的系统不工作。



    我更喜欢把更多的我的域逻辑在实际的域实体(例如article.AddComment(评论),而不是articleCommentService.AddComment(文章,评论)),但你的方法也很好。


    I recently started reading about ASP.net MVC and after getting excited about the concept, i started to migrate all my webform project to MVC but i am having a hard time keeping my controller skinny even after following all the good advices out there (or maybe i just don't get it ... ). The website i deal with has Articles, Videos, Quotes ... and each of these entities have categories, comments, images that can be associated with it. I am using Linq to sql for database operations and for each of these Entities, i have a Repository, and for each repository, i create a service to be used in the controller.

    so i have -

    • ArticleRepository
    • ArticleCategoryRepository
    • ArticleCommentRepository

    and the corresponding service

    • ArticleService
    • ArticleCategoryService ...

    you see the picture.

    The problem i have is that i have one controller for article,category and comment because i thought that having ArticleController handle all of that might make sense, but now i have to pass all of the services needed to the Controller constructor. So i would like to know what it is that i am doing wrong. Are my services not designed properly? should i create Bigger service to encapsulate smaller services and use them in my controller? or should i have an articleCategory Controller and an articleComment Controller?

    A page viewed by the user is made of all of that, thee article to be viewed,the comments associated with it, a listing of the categories to witch it applies ... how can i efficiently break down the controller to keep it "skinny" and solve my headache?

    Thank you! I hope my question is not too long to be read ...

    解决方案

    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.

    这篇关于构建ASP.net MVC应用程序以使用存储库和服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆