简体   繁体   English

分层体系结构中的存储库模式

[英]Repository Pattern in Layered Architecture

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. 我正在尝试在ASP.NET 4.0应用程序中实现它。 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 来自这篇文章, http://www.primaryobjects.com/CMS/Article108.aspx

I have created the MYSQLRepository (DataLayer) 我创建了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>");
    }

}

In order to do this, I have to reference my DataLayer in the presentation layer. 为此,我必须在表示层中引用我的DataLayer。 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. 我见过许多使用ASP.NET MVC的例子,它似乎在那里运行良好。

Also, Do i really need dependency injection here? 另外,我真的需要依赖注入吗?

Thanks for the help Soni 感谢Soni的帮助

最好将IoC统一到构造函数注入,这将删除不必要的层的引用。

I think you're missing the point and the reason why the repository pattern is so powerful. 我认为你忽略了存储库模式如此强大的重点和原因。 It may seem strange to call it in your presentation layer, but imagine that page needed to use a different datasource. 在您的表示层中调用它可能看起来很奇怪,但想象该页面需要使用不同的数据源。 You can just as easily swap it out with a call like: orderBL orderBL = new OrderBL(new OracleRepository()); 您可以通过以下调用轻松地将其交换出来: orderBL orderBL = new OrderBL(new OracleRepository());

Another alternative here is you don't pass repository references around. 另一种替代方法是您不要传递存储库引用。

Your BL can instantiate the DL when it needs to. 您的BL可以在需要时实例化DL。 If you have cross method calls where several operations need to happen in data then use a facade layer to sit on top of your business objects. 如果您有交叉方法调用,其中需要在数据中执行多个操作,那么请使用外观层来放置业务对象。 Either way then you don't need a direct reference to a data layer repository from your presentation layer. 无论哪种方式,您都不需要直接引用表示层中的数据层存储库。 If it makes you feel even better, pull any of your Model classes (for example Order) out into a separate "Models" project. 如果它让您感觉更好,请将任何Model类(例如Order)拉出到单独的“Models”项目中。

To avoid calling the data layer directly, you could call a function in your business layer to instatiate your repository. 为避免直接调用数据层,您可以调用业务层中的函数来实例化存储库。 Your business layer could take care of the rest. 您的业​​务层可以处理其余部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM