Should Factories Persist Entities?

Posted by mxmissile on Stack Overflow See other posts from Stack Overflow or by mxmissile
Published on 2010-04-20T20:59:44Z Indexed on 2010/04/20 21:03 UTC
Read the original article Hit count: 262

Should factories persist entities they build? Or is that the job of the caller? Pseudo Example Incoming:

    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);
       ...
    }
}

or

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();
     ...
  }

}

Is there a recommended practice here?

© Stack Overflow or respective owner

Related posts about c#

Related posts about best-practices