我在实施通用存储库时遇到问题
I have problem in implementing generic repository
我想构建通用存储库以使其易于实施..现在我想制作接口以在我的域服务中的依赖注入中使用它,但我不能
我想构建通用存储库以便在 implementing.I 创建通用抽象存储库以获取实体及其 context.now 我想创建接口以在我的域服务的依赖注入中使用它
我的通用存储库:
public abstract class Repository<T,K>:IRepository<T,K>
{
private Type t;
private K _Context;
private bool disposed = false;
public Repository(K Context)
{
_Context = Context;
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_Context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Delete(object id)
{
T t = _Context.Set<T>().Find(id);
_Context.Set<T>().Remove(t);
}
public T Get(object id)
{
return _Context.Set<T>().Find(id);
}
public IEnumerable<T> getList()
{
return _Context.Set<T>().ToList();
}
public void insert(T t)
{
_Context.Set<T>().Add(t);
}
public void Save()
{
_Context.SaveChanges();
}
public void Update(T t)
{
_Context.Entry(t).State = EntityState.Modified;
}
}
}
我的存储库界面:
public interface IRepository<T,K> where T : BaseEntity where K : BaseContext<K>
{
T Get(object id);
IEnumerable<T> getList();
void insert(T t);
void Delete(object id);
void Update(T t);
void Save();
}
我的错误是 "the Type 'T' can not be used as type parameter 'T' in the generic type....The type 'T' cannot be used as type parameter 'T' in the generic type or method 'IRepository'. There is no boxing conversion or type parameter conversion from 'T' to 'DomainModel.BaseEntity"
,我想知道如何解决这个问题
您还必须在 class Repository 上放置 where 约束,即:
public abstract class Repository<T,K>:IRepository<T,K> where T : BaseEntity where K : BaseContext<K>
这是因为C#对Repository 中的T一无所知,但它需要满足where条件在 IRepository
我想构建通用存储库以使其易于实施..现在我想制作接口以在我的域服务中的依赖注入中使用它,但我不能
我想构建通用存储库以便在 implementing.I 创建通用抽象存储库以获取实体及其 context.now 我想创建接口以在我的域服务的依赖注入中使用它
我的通用存储库:
public abstract class Repository<T,K>:IRepository<T,K>
{
private Type t;
private K _Context;
private bool disposed = false;
public Repository(K Context)
{
_Context = Context;
}
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_Context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Delete(object id)
{
T t = _Context.Set<T>().Find(id);
_Context.Set<T>().Remove(t);
}
public T Get(object id)
{
return _Context.Set<T>().Find(id);
}
public IEnumerable<T> getList()
{
return _Context.Set<T>().ToList();
}
public void insert(T t)
{
_Context.Set<T>().Add(t);
}
public void Save()
{
_Context.SaveChanges();
}
public void Update(T t)
{
_Context.Entry(t).State = EntityState.Modified;
}
}
}
我的存储库界面:
public interface IRepository<T,K> where T : BaseEntity where K : BaseContext<K>
{
T Get(object id);
IEnumerable<T> getList();
void insert(T t);
void Delete(object id);
void Update(T t);
void Save();
}
我的错误是 "the Type 'T' can not be used as type parameter 'T' in the generic type....The type 'T' cannot be used as type parameter 'T' in the generic type or method 'IRepository'. There is no boxing conversion or type parameter conversion from 'T' to 'DomainModel.BaseEntity"
,我想知道如何解决这个问题
您还必须在 class Repository
public abstract class Repository<T,K>:IRepository<T,K> where T : BaseEntity where K : BaseContext<K>
这是因为C#对Repository