これが私のドメインモデルと汎用リポジトリの設計です
public interface IEntity
{
long Id { get; }
}
public interface IRepository<T> where T : class, IEntity, new()
{
void Save(T entity);
void Delete(long id);
T Get(long id);
IEnumerable<T> GetAll();
}
public interface IUserRepository : IRepository<User>
{
User Login(string username, string password);
}
public class User : IEntity
{
// Implementation of User
}
public abstract class BaseRepository<T> : IRepository<T> where T : class, IEntity, new()
{
// Implementation of IRepository
}
public class UserRepository : BaseRepository<User>, IUserRepository
{
// Implementation of IUserRepository
// Override BaseRepository if required
}
リポジトリ インスタンスをインスタンス化する場合は、次のインターフェイスを実装するファクトリを使用します
public interface IRepositoryFactory
{
R CreateRepository<R, T>()
where R : IRepository<T>
where T : class, IEntity, new();
}
そして、以下のようにファクトリオブジェクトを使用します
1. IRepositoryFactory factory = CreateFactory();
2. IUserRepository repository = factory.CreateRepository<IUserRepository, User>();
3. User user = repository.Login("user", "1234");
私の問題は2行目にあります。私は自分の工場を次のように使用したいと思います。
// Without specifying the User type parameter
factory.CreateRepository<IUserRepository>()
私の IRepository インターフェイスにはエンティティのタイプに関する制約があるため、私のファクトリは同じ制約を使用して IRepository の要件を満たします。
このパラメータをクライアントから分離する方法はありますか?