添加不同类型的数据库对象

问题描述 投票:0回答:1

我有带有表格的数据库(

Resistor
Transistor
Capacitor 
...等)。我所有的数据库集都是继承基类
DomainObject

我有通用的 DataService 来访问数据库。

 public class GenericDataService<T> : IDataService<T> where T : DomainObject
 {
   private readonly OperationMapsDbContextFactory _dbContextFactory;
   public GenericDataService(OperationMapsDbContextFactory dbContextFactory)
   {
     _dbContextFactory = dbContextFactory;
   }
   public async Task<T> Create(T entity)
   {
     using (OperationMapsDbContext context = _dbContextFactory.CreateDbContext())
     {
       EntityEntry<T> createdResult = await context.Set<T>().AddAsync(entity);
       await context.SaveChangesAsync();
       return createdResult.Entity;
    }
}

我有清单

 List<DomainObject> allItems

public class Program
{
    public static List<DomainObject> allItems;
    public static DbContextFactory dbContextFactory = new DbContextFactory();
    static void Main(string[] args)
    {
       allItems = new List<DomainObject>
       {
         new Resistor(),
         new Capacitor(),
         new Transitor(),
       }
    }
}


public static void AddItemsToDataBase()
{
  foreach(var item in allItems)
  {
    IDataService<> dataService = new GenericDataService<>(dbContextFactory) // How to init generic class with correct type depeinding on item
    dataService.Create(_domainObject); // How to cast domainObject to its real type depending on item?
  }
}

现在我用开关盒做到了。每个 DbSet 类都有其类型的枚举值。

enum ItemType{
Resistor,
Transisor, 
Capacitor 

添加元素的方法看起来

public static void AddItemsToDataBase()
{
  foreach(var item in allItems)
  {
    switch(item.ItemType)
    {
      case(Resistor):
        IDataService<Resistor> dataService = new GenericDataService<Resistor>(dbContextFactory) 
        dataService.Create((Resistor)_domainObject); 
      break;
      case(Transistor):
        IDataService<Transistor> dataService = new GenericDataService<Transistor>(dbContextFactory) 
        dataService.Create((Transistor)_domainObject); 
      break;
      case(Capacitor)
        IDataService<Capacitor> dataService = new GenericDataService<Capacitor>(dbContextFactory) 
        dataService.Create((Capacitor)_domainObject); 
      break;
    }
  }
}

那么如何在不使用 switch/case 的情况下正确实现这个功能呢?

c# database entity-framework
1个回答
0
投票

要在

DbContext
中添加实体,无需引用特定的
DbSet
。打电话
context.Add(entity);
就足够了。

如果您的服务只是创建

DbContext
实例,然后添加您的实体 - 这几乎毫无意义,因为您以过低的价格使代码变得更加复杂。最有效的方法是这样的:

public class Program
{
    public static List<DomainObject> allItems;
    public static DbContextFactory dbContextFactory = new DbContextFactory();

    static void Main(string[] args)
    {
        allItems = new List<DomainObject>
        {
            new Resistor(),
            new Capacitor(),
            new Transitor(),
        }
    }
}


public static async void AddItemsToDataBase()
{
    using var context = dbContextFactory.CreateDbContext();

    context.AddRange(allItems);

    await context.SaveChangesAsync();
}
© www.soinside.com 2019 - 2024. All rights reserved.