Parameter in constructor, and constructor itself, should only do one and only one job to do: that is registering dependency. Sometimes, it is needed to "inject" the dependency to factory, as described at abstract factory pattern by Mark Seeman in this answer.
public class ProfileRepositoryFactory : IProfileRepositoryFactory
{
private readonly IProfileRepository aRepository;
private readonly IProfileRepository bRepository;
public ProfileRepositoryFactory(IProfileRepository aRepository,
IProfileRepository bRepository)
{
if(aRepository == null)
{
throw new ArgumentNullException("aRepository");
}
if(bRepository == null)
{
throw new ArgumentNullException("bRepository");
}
this.aRepository = aRepository;
this.bRepository = bRepository;
}
public IProfileRepository Create(string profileType)
{
if(profileType == "A")
{
return this.aRepository;
}
if(profileType == "B")
{
return this.bRepository;
}
// and so on...
}
}
It is valid in that case, but not in your case because:
- It makes your factory have state
It makes your factory more flexible if the parameter (stream) injected as method parameter
public class StringsListFromFile : IStringsListFactory{ public List<string> Create(StreamReader sr){ ///recover the strings using my stream reader... } }
If your interface should be flexible for the input, use generic instead
- additionally, it is better to return
IEnumerable<string>
instead ofList<string>