asp.net mvc 分层应用程序,我理解对吗? [英] asp.net mvc layered application, am I understanding it right?

查看:28
本文介绍了asp.net mvc 分层应用程序,我理解对吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多可以在互联网上找到的 asp.net mvc 分层应用程序.现在,是时候应用我所学到的了.请告诉我我做得对不对.示例代码会很棒:D

I've read a lot of asp.net mvc layered application I can find in the internet. Now, time to apply what I've learned. Please tell me if I'm doing it right or not. An example code would be great :D

这只是一个简单的分层应用程序.来了,

This is just a simple layered application. Here goes,

<Company>.Data
        -- DataAccess (contains repo, interfaces and dbcontext)
        -- Mappings (mapping to models using fluent api)
        -- Migrations (schema migration)
        -- Filters (pipe and filter)
        - Product.cs
        - Customer.cs
        - Order.cs
<Company>.Service (consumes the interfaces and implementation)
        - ProductService.cs
        - CustomerService.cs
        - OrderService.cs
<Company>.Tests
<Company>.Web (ASP.NET MVC, have reference to Ninject, including ViewModels)

我想知道,在 <Company>.Service 层(如果这应该是我应该放置业务逻辑的地方)例如.

I'm wondering, in the <Company>.Service layer (if this should be the place where I should put my business logic) For example.

public class ProductService : IProductRepository
{
  public IQueryable<Product> GetProductByCustomer(int id)
  {
    // Some logic; Get all the products bought by the customer
  }
}

然后在我的Controller

public class CustomerController : Controller
{
  private readonly ICustomerRepository _customerRepo;
  private readonly IProductRepository _productRepo;

  public CustomerControll(ICustomerRepository customerRepo,
           IProductRepository productRepo)
  {
    _customerRepo = customerRepo;
    _productRepo = productRepo;
  }

  public ActionResult Index(int id)
  {
    var customerProducts = _productRepo.GetProductByCustomer(id);
    return View(customerProducts.ToList());
  }
}

如果有什么需要改进或需要添加的地方,请告诉我.我只想从简单的开始,然后我会爬过去.

Please let me know if there is something I need to improve or some areas I need to add. I just want to start of simple then I'll climb my way through.

注意:为简洁起见,我删除了一些代码.

Note: I removed some code for brevity.

非常感谢您的帮助.谢谢!

Really appreciated for your help. Thanks!

推荐答案

我会更改您的 ProductService 以便它使用 composition 而不是 来包含您的存储库继承.或者...无论如何它看起来非常像一个存储库,也许您打算将其称为ProductRepository?在这种情况下,您可能仍然需要一个 ProductService 来保存您的业务逻辑.

I'd change your ProductService so that it includes your repository using composition rather than inheritance. Or... as it looks very much like a repository anyway, maybe you meant to call it ProductRepository? In which case you might still need a ProductService to hold your business logic.

编辑

例如:

public class ProductService : IProductRepository
{
    public IQueryable<Product> GetProductByCustomer(int id)
    {
        // Some logic; Get all the products bought by the customer
    }
}

会变成:

public class ProductRepository : IProductRepository
{
    public IQueryable<Product> GetProductByCustomer(int customerId)
    {
        // Some logic; Get all the products bought by the customer
    }
}

并且您的应用程序/服务层将具有某种面向业务的功能:

And you would have some sort of business oriented function in your application/services layer:

public class ProductService
{
    private readonly IProductRepository productRepository;

    public ProductService(IProductRepository productRepository)
    {
        this.productRepository = productRepository;
    }

    public IEnumerable<Product> GetCurrentProductsOnOrderForCustomer(int customerId)
    {
        // etc.
    }
}

进一步编辑

通常在分层架构中,您只会与您下方的直接层对话(尽管存在可以与您下方的任何层对话的架构,但它可能是您想要的那个).您典型的分层应用程序将布局为 UI/Presentation Layer -> Application/Service Layer -> Data Layer.您的 Controllers 将在您的 UI 层 中,通常引用应用程序/服务层,其中诸如您的 ProductService 会活着.

Usually in a layered architecture, you would only talk to the immediate layer below you (although architectures exist where you can talk to any layer below you, it may be the one you want). Your typical layered app would be laid out as UI/Presentation Layer -> Application/Service Layer -> Data Layer. Your Controllers would be in your UI Layer, usually with a reference to the Application/Service Layer, where things like your ProductService would live.

这篇关于asp.net mvc 分层应用程序,我理解对吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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