.NET
- 1 ответ
- 0 вопросов
1
Вклад в тег
RepositoryProvider provider = new RepositoryProvider();
dynamic repo = provider.GetRepository<Entity<int>>();
List<Entity<int>> list = repo.GetAll();
Entity<int> entry = repo.Get(10);
interface Entity { }
class Entity<TId> : Entity
where TId : struct
{
public virtual TId Id { get; set; }
}
interface IRepository<T, in TId>
where T : Entity<TId>
where TId : struct
{
T Get(TId id);
IList<T> GetAll();
}
class Repo<T, TId> : IRepository<T, TId>
where T : Entity<TId>
where TId : struct
{
public T Get(TId id)
{
return default(T);
}
public IList<T> GetAll()
{
return new List<T>();
}
}
class RepositoryProvider
{
public dynamic GetRepository<TEntity>()
where TEntity : Entity
{
Type genericParam = typeof(TEntity).GetGenericArguments()[0];
return Activator.CreateInstance(typeof(Repo<,>).MakeGenericType(typeof(TEntity), genericParam));
}
}