Well yes, using the explicit constructor I completely agree with you, but I'm sure that on the best practices of the repository pattern there is also a factory who decouple the the business layer from the constructor.
I will rephrase my question:
I'm trying to decouple the Bll from the Dal using some interfaces and a Factory patter. Data.Contracts containing the interfaces will be referenced on my Bll.
I can not use the concrete Implementations of the Repository since they are inheriting from some classes that I do not want on my Bll.
DataRepositoryBase in particular leverage LLBLGen Pro, and will map the LLBL Entities on POCO models.
following my code:
I got a DataRepositoryBase:
public abstract class DataRepositoryBase<LlblEnt, Model> where LlblEnt : EntityBase, new() where Model : new()
{
public IEnumerable<Model> Get() { // Other code here... }
}
I got a concrete implementation of the abstract generic:
public class UserRepository : DataRepositoryBase<UserEntity, User>, IUserRepository
{
public UserRepository() { }
}
I got a Factory to build any Repository:
public class DataRepositoryFactory : IDataRepositoryFactory
{
public T GetDataRepository<T>() where T : IDataRepository, new()
{
return new T();
}
}
I got the interfaces:
public interface IDataRepository<T> : IDataRepository where T : class, new()
{
IEnumerable<T> Get();
}
public interface IUserRepository : IDataRepository { }
public interface IDataRepository { }
And finally I'm trying to build the repository using the factory:
static void Main(string[] args)
{
IDataRepositoryFactory _DataRepositoryFactory;
IUserRepository AptRep = _DataRepositoryFactory.GetDataRepository<IUserRepository>();
}
An finally I got my error:
'Data.Contracts.IUserRepository' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'Data.Contracts.IDataRepositoryFactory.GetDataRepository()'
It seems quite obvious that the "return new T()" will try to create and Interface, and so will not work.
My question is: Which is the correct implementation to Decouple the code on the Business layer using the factory pattern? I can not use the concrete implementation UserRepository since it inherits from classes that I do not want in the BLL.
Any help would be appreciated.