Should controllers in an ASP.NET MVC web app call repositories, services, or both?

后端 未结 7 1425
生来不讨喜
生来不讨喜 2021-01-30 09:22

The controllers in my ASP.NET MVC web app are starting to get a bit bloated with business logic. The examples on the web all show simple controller actions that simply pull dat

7条回答
  •  野的像风
    2021-01-30 10:03

    Your controllers (in the MVC project) should be calling your objects in the Service project. The services project is where all the business logic is handled.

    A good example is this:

    public ActionResult Index()
    {
        ProductServices productServices = new ProductServices();
    
        // top 10 products, for example.
        IList productList = productServices.GetProducts(10); 
    
        // Set this data into the custom viewdata.
        ViewData.Model = new ProductViewData
                             {
                                 ProductList = productList;
                             };
    
        return View();
    }  
    

    or with Dependency Injection (my fav)

    // Field with the reference to all product services (aka. business logic)
    private readonly ProductServices _productServices;
    
    // 'Greedy' constructor, which Dependency Injection auto finds and therefore
    // will use.
    public ProductController(ProductServices productServices)
    {
        _productServices = productServices;
    }
    
    public ActionResult Index()
    {
        // top 10 products, for example.
        // NOTE: The services instance was automagically created by the DI
        //       so i din't have to worry about it NOT being instansiated.
        IList productList = _productServices.GetProducts(10); 
    
        // Set this data into the custom viewdata.
        ViewData.Model = new ProductViewData
                             {
                                 ProductList = productList;
                             };
    
        return View();
    }
    

    Now .. what's the Service project (or what is ProductServices)? that's a class library with your business logic. For example.

    public class ProductServices : IProductServices
    {
        private readonly ProductRepository _productRepository;
        public ProductServices(ProductRepository productRepository)
        {
            _productRepository = productRepository;
        }
    
        public IList GetProducts(int numberOfProducts)
        {
            // GetProducts() and OrderByMostRecent() are custom linq helpers...
            return _productRepository.GetProducts()
                .OrderByMostRecent()
                .Take(numberOfProducts)
                .ToList();
        }
    }
    

    but that might be all so hardcore and confusing... so a simple version of the ServiceProduct class could be (but i wouldn't really recommend) ...

    public class ProductServices
    {
        public IList GetProducts(int numberOfProducts)
        {
            using (DB db = new Linq2SqlDb() )
            {
                return (from p in db.Products
                        orderby p.DateCreated ascending
                        select p).Take(10).ToList();
            }
        }
    }
    

    So there you go. You can see that all the logic is in the Service projects, which means u can reuse that code in other places.

    Where did i learn this?

    From Rob Conery's MVC StoreFront media and tutorials. Best thing since sliced bread. His tutorials explain (what i did) in good detail with full solution code examples. He uses Dependency Injection which is SOO kewl now that i've seen how he uses it, in MVC.

    HTH.

提交回复
热议问题