2015-02-23 205 views
0

我已經得到了更新我的EF的DataContext如下通用庫(簡稱爲簡潔起見):C#泛型類V5接口

public interface IRepository<T> where T : class 
{ 
    void Add(T entity); 
    void Update(T entity); 
    void Delete(T entity); 
    IQueryable<T> Find(Expression<Func<T, bool>> filter = null); 
} 

public class Repository<TEntity, TContext> : IRepository<TEntity> 
    where TEntity : class 
    where TContext : DbContext 
{ 
    public IUnitOfWork UnitOfWork { get; set; } 
    public TContext DataContext { get; set; } 

    public Repository(IUnitOfWork unitOfWork, TContext dataContext) 
    { 
     UnitOfWork = unitOfWork; 
     DataContext = dataContext; 
    } 

    public virtual void Add(TEntity entity) 
    { 
     DataContext.Set<TEntity>().Add(entity); 
    } 

    public virtual void Update(TEntity entity) 
    { 
     DataContext.Set<TEntity>().Attach(entity); 
     DataContext.Entry(entity).State = EntityState.Modified; 
    } 

    public virtual void Delete(TEntity entity) 
    { 
     if (DataContext.Entry(entity).State == EntityState.Detached) 
      DataContext.Set<TEntity>().Attach(entity); 
     DataContext.Set<TEntity>().Remove(entity); 
    } 

    public IQueryable<TEntity> Find(System.Linq.Expressions.Expression<Func<TEntity, bool>> filter = null) 
    { 
     IQueryable<TEntity> query = DataContext.Set<TEntity>(); 

     if (filter != null) 
      query = query.Where(filter); 

     return query; 
    } 
} 

我想創建一個IArchiveOnlyRepository使用與實現我的IArchiveable接口實體。我希望這會工作:

public interface IArchiveable 
{ 
    bool IsArchived { get; set; } 
    DateTime? ArchivedOn { get; set; } 
    string ArchivedBy { get; set; } 
} 

public interface IArchiveOnlyRepository<T> where T : IArchiveable 
{ 
    void Add(T entity); 
    void Update(T entity); 
    void Archive(T entity); 
    IQueryable<T> Find(Expression<Func<T, bool>> filter = null); 
} 

public class ArchiveOnlyRepository<TEntity, TContext> : IArchiveOnlyRepository<TEntity> 
    where TEntity : IArchiveable 
    where TContext : DbContext 
{ 
    public IUnitOfWork UnitOfWork { get; set; } 
    public TContext DataContext { get; set; } 

    public ArchiveOnlyRepository(IUnitOfWork unitOfWork, TContext dataContext) 
    { 
     UnitOfWork = unitOfWork; 
     DataContext = dataContext; 
    } 

    public virtual void Add(TEntity entity) 
    { 
     DataContext.Set<TEntity>().Add(entity); 
    } 

    public virtual void Update(TEntity entity) 
    { 
     DataContext.Set<TEntity>().Attach(entity); 
     DataContext.Entry(entity).State = EntityState.Modified; 
    } 

    public virtual void Archive(TEntity entity) 
    { 
     if (DataContext.Entry(entity).State == EntityState.Detached) 
      DataContext.Set<TEntity>().Attach(entity); 
     entity.IsArchived = true; 
     entity.ArchivedOn = DateTime.UtcNow; 
     entity.ArchivedBy = UnitOfWork.UserName; 
    } 

    public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>) 
    { 
     IQueryable<TEntity> query = DataContext.Set<TEntity>(); 

     if (filter != null) 
      query = query.Where(filter); 

     return query; 
    } 
} 

不幸的是,下面幾行:

DataContext.Set<TEntity>()... 

生成以下錯誤:

The Type 'TEntity' must be a reference type in order to use it as parameter 'TEntity' 

我如何定義這個通用的存儲庫,以確保T是一個實現IArchiveable的類?

回答

3

你只需把你的class類型約束回:

where TEntity : class, IArchiveable 
+0

感謝的人,我知道這很簡單....星期一 – 2015-02-23 16:43:39

2

你只需要限制TEntityclass(引用類型)

public interface IArchiveOnlyRepository<T> where T : IArchiveable, class 
... 

public class ArchiveOnlyRepository<TEntity, TContext> : IArchiveOnlyRepository<TEntity> 
    where TEntity : IArchiveable, class 
    where TContext : DbContext 
.... 
1

只需更改

where TEntity : IArchiveable 

where TEntity : class, IArchiveable