分层架构中的存储库模式 [英] Repository Pattern in Layered Architecture

查看:20
本文介绍了分层架构中的存储库模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于存储库模式的文章并且有一些问题.我正在尝试在 ASP.NET 4.0 应用程序中实现这一点.该架构是具有表示层、业务层和数据层的分层架构.从这篇文章中,http://www.primaryobjects.com/CMS/Article108.aspx

I've gone through quite a few articles on the repository pattern and had some questions. I'm trying to implement this in a ASP.NET 4.0 application. The architecture is a layered architecture with a Presentation Layer, Business Layer and Data Layer. From this article, http://www.primaryobjects.com/CMS/Article108.aspx

我已经创建了 MYSQLRepository (DataLayer)

I have created the MYSQLRepository (DataLayer)

public class MySQLRepository:IOrderRepository
{
    public List<Order> GetOrders()
    {

        List<Order> orders = new List<Order>();
        orders.Add(new Order(1,"A"));
        orders.Add(new Order(2,"B"));

        return orders;
    }
}

我的业务层看起来像这样

My Business Layer looks like this

public class OrderBL
{
    IOrderRepository orderrep;
    public OrderBL(IOrderRepository repository)
    {
       orderrep = repository;

    }

    public List<Order> GetOrders()
    {
        return orderrep.GetOrders();

    }
}

现在我的问题是在表示层,我应该这样做

Now my question is that in the presentation layer, I'm expected to do this

protected void Page_Load(object sender, EventArgs e)
{
    OrderBL orderBL = new OrderBL(new MySQLRepository());
    List<Order> orders = orderBL.GetOrders();

    foreach (Order order in orders)
    {
        Response.Write(order.OrderId.ToString() + ". " + order.OrderNo + "<br>");
    }

}

为了做到这一点,我必须在表示层中引用我的 DataLayer.是不是错了?理想情况下,我只想引用我的业务层.我的实现有什么问题,或者它不是实现模式的正确位置.我看过很多使用 ASP.NET MVC 的例子,它似乎在那里工作得很好.

In order to do this, I have to reference my DataLayer in the presentation layer. Isn't that wrong? Ideally I would only want to reference my Business Layer. IS something wrong in my implementation or is it not the right place to implement the pattern. I have seen many examples using ASP.NET MVC and it seems to work well there.

另外,我真的需要依赖注入吗?

Also, Do i really need dependency injection here?

感谢您的帮助索尼

推荐答案

最好统一 IoC 以获取构造函数注入,这将删除不必要的层引用.

it's better to unilize IoC to get constructor injection, this will remove unnecessary layers' references.

这篇关于分层架构中的存储库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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