Reputation: 10123
How do I handle the situation when my concrete object that my factory object will return relies on anther object that is already created.
In order for my repository to work, I need to have an instance of a connection object. I do not want to create a new connection object each time a repository is called. How do I handle this situation? Do I pass the connection object into the RepositoryFactory?
IRepository<User> userRepository = RepositoryFactory.GetRepository("user");
public class UserRepository : IRepository<User>
{
public DbConnection Connection { get; set; }
public UserRepository(DbConnection connection)
{
this.Connection = connection;
}
}
Upvotes: 2
Views: 150
Reputation: 36092
In MEF, you would put a property on your factory object which imports a connection object. The factory object could then provide that connection object as a constructor parameter to instances it creates.
The connection object could be constructed by MEF composition, or it could be supplied by the composition container (passed as a parameter into container.ComposeParts).
Upvotes: 0
Reputation: 61599
In this particular example, it might be better to spin up a new connection each time, close and dispose when your finished using a unit-of-work approach to resource usage.
Upvotes: 2