问题描述
我已经通过在存储库模式好几篇文章不见了,有一些问题。
我想在ASP.NET 4.0应用程序来实现这一点。
该架构是一个分层架构,一个presentation层,业务层和数据层。
从这篇文章,
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
(数据层)
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();
}
}
现在我的问题是,在presentation层,我希望做到这一点。
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>");
}
}
在为了做到这一点,我要引用我的数据层在presentation层。是不是错了?理想情况下,我只会想引用我的业务层。
什么不对我在执行还是不执行模式的正确的地方。我已经看到了使用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?
感谢您的帮助
瑞里
Thanks for the helpSoni
推荐答案
这是更好地unilize国际奥委会拿到构造器注入,这将消除不必要的层'的引用。
it's better to unilize IoC to get constructor injection, this will remove unnecessary layers' references.
这篇关于在分层架构库模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!