2014-10-08 52 views
1

我想做一個泛型函數,這將有助於避免重複我的代碼。該函數必須與通用類型T(也許是T2)通用,因爲它必須引用正在創建的表中的不同字段。不過,我不知道這是否可能。通用函數添加實體到表 - 實體框架6和C#

比方說,我有以下實體添加到數據庫:

List<Type_1> entities_1 = new List<Type_1>() 
{ 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    ... 
    new Type_1{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
}; 
entities_1.ForEach(e => dbContext.Types_1.Add(e)); 
dbContext.SaveChanges(); 
entities_1 = null; 

List<Type_2> entities_2 = new List<Type_2>() 
{ 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
    ... 
    new Type_2{Field1="<Value of field 1",Field2="Value of Field 2", ...}, 
}; 
entities_2.ForEach(e => dbContext.Types_2.Add(e)); 
dbContext.SaveChanges(); 
entities_2 = null; 

etc. 

是否有可能使帶有參數的功能:List<T1>dbContextT2,將負責創建不同的表的功能?也許這需要參數化這些Type_(n)Types_(n)。我想下面的東西,但編譯器不支持T2接受它,並點:

private void addEntitiesToDbContext<T1,T2>(List<T1> ent, MyTypeContext dbContext) 
{ 
    ent.ForEach(e => dbContext.T2.Add(e)); 
    dbContext.SaveChanges(); 
} 

感謝



編輯:


我覺得我已經得到了採取空間道歉評論部分-.-

我剛剛意識到什麼C Bauer說關於存儲庫模式。這是如果有人也對這個設計感興趣,下面是我推薦的一個非常好的鏈接:Repository Pattern Explained。還有一個關於Repository Pattern設計here的問題。祝你好運。

PS。在Repository Pattern

回答

1

我希望這些會幫助你。

public interface IRepository<TEntity> 
    where TEntity : class 
{ 
} 

public abstract class RepositoryBase<TEntity> : IRepository<TEntity> 
    where TEntity : class 
{ 
    private DbContext dbContext; 

    private DbSet<TEntity> dbSet; 

    public IQueryable<TEntity> All 
    { 
     get 
     { 
      return dbSet; 
     } 
    } 

    public void InsertOrUpdate(TEntity entity) 
    { 
    } 
} 

public class Repository<T> : RepositoryBase<T> 
    where T : class 
{ 
    public Repository(DbContext context) 
    { 
     DbContext = context; 
     DbSet = context.Set<T>(); 
    } 
} 
+0

你好Tau。我不知道你爲什麼被打分,所以我給你打分。你的解決方案真的很先進。雖然我理解抽象類的概念,但仍然很難遵循。我想我需要更多時間,並會盡快破解它。感謝您的時間:) – Celdor 2014-10-08 11:31:42

+1

@Ziko - 存儲庫是一種設計模式,這是相當先進的,但應滿足您的需求100%。在使用存儲庫時,輸入'Repository entityTypeRepository = new Repository ();'其中entitytype是您試圖對其執行CRUD操作的實體。雖然通常您的存儲庫實現會暴露SubmitChanges或SaveChanges。 – 2014-10-08 12:51:00

+0

嗨。我花了一些教程來了解這裏的例子。現在沒問題,但仍需要就這個例子提出兩個問題。 *爲什麼RepositoryBase 繼承自IRepository ?我的猜測是Interface提供的約束在這裏是引用類型。另外,接口不允許實現字段。 *在Repository 中,我不明白輸入:'DbContext = context; DbSet = context.Set ();' 不應該是:'DbContext dbContext = context; DbSet DbSet = context.Set ();'謝謝。 – Celdor 2014-10-25 16:47:30

0

另一個很好的來源,我想不出爲什麼這是行不通的:

1)建立一個共同的基類(或接口),稱之爲BaseEntity,這所有的實體從派生。

2)添加到您的上下文類:

IDbSet<BaseEntity> Entitys { get; set; } 

3)讓你的Add方法採取的列表BaseEntity的

private void addEntitiesToDbContext(List<BaseEntity> ent, MyTypeContext dbContext) 
{ 
    ent.ForEach(e => dbContext.Entitys.Add(e)); 
    dbContext.SaveChanges(); 
} 
+0

好,它說:'「XXdbContext」不包含「T2」,沒有擴展方法「T2」接受型「XXdbContext」的第一個參數的定義可以找到(是否缺少使用指令或彙編引用?)'。就像它說的dbContext沒有T2的知識:/我會嘗試你所建議的,但我在c#的初學者,所以它可能仍然是壓倒性的:我希望它不會:p – Celdor 2014-10-08 10:42:54