I have put this kind of pattern in my code for instantiating business objects:
// UI or Business level code
public class SomeClass
{
IOrderFactory orderFactory;
public SomeClass(IOrderFactory orderFactory)
{
this.orderFactory = orderFactory
}
public void SomeMethod()
{
var newOrder = orderFactory.CreateOrder();
// Do stuff with new object
}
}
// In an interfaces only project
public interface IOrderFactory
{
Order CreateOrder();
}
// In an implementation project (not seen by most other modules)
public class OrderFactory
{
public Order CreateOrder()
{
return new Order();
}
}
We currently use Unity to create the IOrderFactory
object, but i am wondering if Unity (IOC) can be used to to generate the Order
object itself.
Something like unityContainer.Resolve<IOrder>()
and have it make a new one each time?
Would that work? We use unity for our services and functional (ie ViewModels, Helpers) classes. But we have never used it to to create business objects (like Customer or Order.)