C# 在存储库中实现泛型类型
我不确定我想做什么是可能的,因为我还没有在谷歌上找到任何东西,经过大约30分钟的密集搜索,我决定直接问 我已经为我的存储库定义了一个简单的界面C# 在存储库中实现泛型类型,c#,generics,interface,implementation,C#,Generics,Interface,Implementation,我不确定我想做什么是可能的,因为我还没有在谷歌上找到任何东西,经过大约30分钟的密集搜索,我决定直接问 我已经为我的存储库定义了一个简单的界面 public interface IRepository<TEntity> : IDisposable { TEntity GetById(object id); List<TEntity> GetAll(); } 当我这样做时,我不知道如何在实现中定义我的泛型类型tenty 总之,我希望使用接口时不指定类型,这
public interface IRepository<TEntity> : IDisposable
{
TEntity GetById(object id);
List<TEntity> GetAll();
}
当我这样做时,我不知道如何在实现中定义我的泛型类型tenty
总之,我希望使用接口时不指定类型,这样它就可以从实际对象中获取类型,如下所示
public class ContentRepository : IRepository<ContentPages>
{
private readonly Context _db = new Context();
public ContentPages GetById(object id)
{
var result = _db.ContentPages.Find(id);
return result;
}
public List<ContentPages> GetAll()
{
return _db.ContentPages.ToList();
}
public void Dispose()
{
_db.Dispose();
}
}
public interface IRepository : IDisposable
{
TEntity GetById<TEntity>(object id);
List<TEntity> GetAll<TEntity>();
}
}
public constructor1(IRepository ContentRepository){}
下一个控制器获取此构造函数
public constructor2(IRepository BlogRepository){}
等等
我希望我能详细描述我的问题,让大家都能理解:)在
IRepository
类的具体实现中,您可以定义tenty的类型,如下所示
public TEntity GetById<TEntity>(object id) where TEntity:class
{
// Implimetation
}
public interface IRepository<TEntity>: IDisposable where TEntity : class
public tenty GetById(对象id),其中tenty:class
{
//恳求
}
但在这里根据存储库模式更好地使用如下
public TEntity GetById<TEntity>(object id) where TEntity:class
{
// Implimetation
}
public interface IRepository<TEntity>: IDisposable where TEntity : class
公共接口IRepository:IDisposable其中tenty:class
试试这种变体:
public interface IRepository<TEntity> where TEntity : class
{
TEntity Find(params object[] keyValues);
// ...
}
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly IDbSet<TEntity> _dbSet;
public Repository(IDbContext context)
{
_dbSet = context.Set<TEntity>();
}
public virtual TEntity Find(params object[] keyValues)
{
return _dbSet.Find(keyValues);
}
// ...
}
公共接口假定,其中tenty:class
{
TEntity Find(参数对象[]键值);
// ...
}
公共类存储库:IRepository,其中tenty:class
{
专用只读IDbSet _dbSet;
公共存储库(IDbContext上下文)
{
_dbSet=context.Set();
}
公共虚拟内存查找(参数对象[]键值)
{
返回_dbSet.Find(keyValues);
}
// ...
}
用法示例:
IRepository<ApplicationUser> repository = new Repository<ApplicationUser>(new ApplicationDbContext());
ApplicationUser applicationUser = repository.Find("key");
i假设。真的很酷
例如:
public class DatabasesController : Controller
{
private UnitOfWork _unitOfWork;
private WebContext _context;
public DatabasesController()
{
_context = new WebContext();
_unitOfWork = new UnitOfWork(_context);
}
//
// GET: /Databases/
public ViewResult Index()
{
List<Database> databases =
_unitOfWork
.Repository<Database>()
.Query()
.Include(database => database.FileEntitiesInfo)
.Get()
.ToList();
_unitOfWork.Save();
return View(databases);
}
}
公共类数据库控制器:控制器
{
私人工作单位(Unitof Work);;
私人网络上下文(WebContext);;
公共数据库控制器()
{
_context=新的WebContext();
_unitOfWork=新的unitOfWork(_上下文);
}
//
//获取数据库/
公共视图结果索引()
{
列出数据库=
_工作单元
.Repository()
.Query()
.Include(数据库=>database.FileEntitiesInfo)
.Get()
.ToList();
_unitOfWork.Save();
返回视图(数据库);
}
}
当我使用您的第一个示例并实现如下内容时:return _db.ContentPages.Find(id);我无法将ContentPages转换为TEntity我不能说TEntity:ContentPages,因为它违反了接口