我正在开发一个基于 .NET 4.5、EF5 和 GenericRepository.EntityFramework 的项目。我正在尝试创建一个发布服务,该服务将对专门用于创作和移动的数据库中的记录执行一些操作
第一个问题是我的类型转换(IBaseRepository<BaseEntity>)
失败。通过使用直接转换...
object myRepository = p.GetValue(entity, null);
IBaseRepository<MRClassification> risk = myRepository;
因为 myRepository 采用“MRClassification”对象类型
问题是每个存储库的签名不同,是否可以返回一个列表,IBaseRepository<p.PropertyType.GetInterfaces()[0].GenericTypeArguments[0]>
该列表将返回每个存储库作为类型的实体?
我考虑过使用协方差,但我在 BaseRepository 中有将 T 作为参数的方法,例如
public virtual new void Add(T entity) { do something; }
发布应用代码
public static void DeactivateNonTransversableExpiredRecords()
{
IEnumerable<Tuple<IBaseRepository<BaseEntity>, Type>> nonTransversableList =
GetNonTransversableRepositories(Databucket);
foreach (var repository in nonTransversableList)
{
////IQueryable recordsToExpire =
////((IBaseRepository<BaseEntity>)repository).FindBy(
//// p => Convert.ToDateTime(p.ExpirationDate).Date <= DateTime.Now.Date && p.IsActive);
IQueryable recordsToExpire = repository.Item1.FindBy(p => p.IsActive);
foreach (var row in recordsToExpire)
{
((BaseEntity)row).IsActive = false;
((IBaseRepository<BaseEntity>)repository).Edit((BaseEntity)row);
}
}
}
private static IEnumerable<Tuple<IBaseRepository<BaseEntity>, Type>> GetNonTransversableRepositories(object entity)
{
PropertyInfo[] properties =
entity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance);
List<Tuple<IBaseRepository<BaseEntity>, Type>> list = new List<Tuple<IBaseRepository<BaseEntity>, Type>>();
foreach (PropertyInfo p in properties)
{
if (!typeof(ITransversableRepository).IsAssignableFrom(p.PropertyType))
{
if (OpenGenericIsAssignableFrom(typeof(IBaseRepository<>), p.PropertyType))
{
list.Add(
new Tuple<IBaseRepository<BaseEntity>, Type>(
(IBaseRepository<BaseEntity>)Activator.CreateInstance(p.PropertyType),
p.GetValue(entity, null)));
}
}
}
return list;
}
DataModel 解决方案中的 POCO 实体和存储库
--- DataBucket
public class DataBucket : Audit, IDataBucket
{
private IMRClassificationRepository mrClassCodeRepository;
public IMRClassificationRepository MRClassificationRepository
{
get { return this.mrClassCodeRepository ?? (this.mrClassCodeRepository = new MRClassificationRepository(this.dataContext)); }
}
...
}
--- Repository Class Headers
public class MRClassificationRepository : BaseRepository<MRClassification>, IMRClassificationRepository
{
...
}
public interface IMRClassificationRepository : IBaseRepository<MRClassification>
{
...
}
public abstract class BaseRepository<T> : CoreRepository<T>, IBaseRepository<T> where T : BaseEntity, IBaseEntity
{
...
}
public interface IBaseRepository<T> : ICoreRepository<T> where T : BaseEntity
{
...
}
--- Entity Class Headers
public class MRClassification : RiskClassification
{
...
}
public abstract class RiskClassification : Revisionable
{
...
}
public abstract class Revisionable : BaseEntity, IRevisionableEntity
{
...
}
public class BaseEntity : IBaseEntity
{
...
}
public interface IBaseEntity : IEntity<Guid>
{
...
}