【问题标题】:Create repository based on entity type根据实体类型创建存储库
【发布时间】:2012-12-14 05:03:53
【问题描述】:

我想根据实体类型创建一个存储库。任何想法如何做到这一点?

  • 我们有很多实体,所以这需要是通用代码。
  • 我们需要为我们拥有的每个实体都有历史记录表。
  • 我们使用代码优先、DbContext、使用存储库和工作单元模式的 EF 5.0。

下面是我们必须在SaveChanges 方法中进行更改的代码。对于每个修改和删除的实体,我们希望将旧行保存到历史表中。下面是单个实体的硬编码:ServiceLocation

所有实体都有对应的历史实体。例如,对于ServiceLocation 实体,有一个ServiceLocationHistory 实体(和存储库)。

如果 modifiedEntity 的类型为 ServiceLocation,我如何创建 ServiceLocationHistoryRepository?并使其适用于所有类型?

  public override int SaveChanges()
    {
        // Get the entities that changed
        var addedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Added && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);
        var modifiedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Modified && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);
        var deletedEntries = ChangeTracker.Entries().Where(e => e.State == EntityState.Deleted && e.Entity is IHistoryBase).Select(e => (TBaseEntity)e.Entity);

        // Debugging watch varibles
        int x = addedEntries.Count();
        int y = modifiedEntries.Count();
        int z = deletedEntries.Count();

        // Create the Type adapter
        TypeAdapter adapter = new TypeAdapter(new RegisterTypesMap[] { new HistoryRegisterTypesMap() });
        var serviceLocationHistoryRepository = new ServiceLocationHistoryRepository(this);

        foreach (var addedEntry in addedEntries)
        {
            addedEntry.FromDate = DateTime.Now;
            addedEntry.ToDate = DateTime.Parse("9999-12-31 00:00:00.0000000");
        }

        foreach (var modifiedEntry in modifiedEntries)
        {

            ServiceLocationHistory history = adapter.Adapt<ServiceLocation, ServiceLocationHistory>(modifiedEntry as ServiceLocation);
            serviceLocationHistoryRepository.Add(history);

            history.FromDate = modifiedEntry.FromDate;
            history.ToDate = modifiedEntry.FromDate = DateTime.Now;
            modifiedEntry.ToDate = DateTime.Parse("9999-12-31 00:00:00.0000000");
        }

        foreach (var deletedEntry in deletedEntries)
        {
            ServiceLocationHistory history = adapter.Adapt<ServiceLocation, ServiceLocationHistory>(deletedEntry as ServiceLocation);
            serviceLocationHistoryRepository.Add(history);

            history.FromDate = deletedEntry.FromDate;
            history.ToDate = DateTime.Now;
         }
        return base.SaveChanges();

    }

【问题讨论】:

    标签: generics ef-code-first entity-framework-5


    【解决方案1】:

    下面是我用来根据类型获取存储库的概念验证代码。

           private dynamic GetHistoryRepository(TBaseEntity entry)
        {
            // TODO - How well does the below perform?
            // TODO - Is there a way that we can only get one repository if there are multiple changed entities of the same type?
            // TODO - How can we not hard code the type name?   Put a property on each entity that gives us the entitys history repository?  Factory?
    
            // Get the History repository for the entity
            Type objType = Type.GetType("Data.MainBoundedContext.CALMModule.Repositories." + entry.GetType().Name.Split('_')[0] + "HistoryRepository");
            return  Activator.CreateInstance(objType, new object[] { this });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2013-06-29
      • 2017-07-31
      • 1970-01-01
      • 2012-08-21
      相关资源
      最近更新 更多