工廠應該堅持他們構建的實體嗎?或者這是來電者的工作?僞例傳入:工廠是否應該堅持實體?
public class OrderFactory
{
public Order Build()
{
var order = new Order();
....
return order;
}
}
public class OrderController : Controller
{
public OrderController(IRepository repository)
{
this.repository = repository;
}
public ActionResult MyAction()
{
var order = factory.Build();
repository.Insert(order);
...
}
}
或
public class OrderFactory
{
public OrderFactory(IRepository repository)
{
this.repository = repository;
}
public Order Build()
{
var order = new Order();
...
repository.Insert(order);
return order;
}
}
public class OrderController : Controller
{
public ActionResult MyAction()
{
var order = factory.Build();
...
}
}
是否有一個推薦的做法在這裏?
我沒有創建這些類所需的所有私有字段,但您應該明白我的想法。 – mxmissile 2010-04-20 21:00:54
謝謝,所有好的答案! – mxmissile 2010-04-20 21:27:30