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

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

问题描述

我已经读了很多asp.net的MVC分层应用的,我可以在互联网上找到。现在,时间申请我学到的东西。请告诉我,如果我这样做是正确与否。一个例子code将是巨大的: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)

我想知道,在&LT;公司方式&gt;。服务层(这应该是我应该把我的业务逻辑的地方)例如:

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
  }
}

然后在我的控制器

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.

请注意:我删除了一些code为简洁

Note: I removed some code for brevity.

真的AP preciated对您有所帮助。谢谢!

Really appreciated for your help. Thanks!

推荐答案

我会改变你的 ProductService ,以便它包括你的版本库使用的组成而不是继承的。还是......因为它看起来非常像一个仓库,无论如何,也许你的意思是把它叫做 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层 - > 应用程序/服务层 - > 数据层。你的控制器将在 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天全站免登陆