問題描述
我是 LINQ to SQL 的新手,并嘗試為基本的創(chuàng)建、讀取、更新和銷毀 (CRUD) 方法創(chuàng)建通用數(shù)據(jù)訪問對象 (DAO),以便我可以重用代碼.我成功地創(chuàng)建了一個(gè)通用方法,該方法將使用下面的代碼刪除任何實(shí)體,但是,我想知道是否有人知道如何創(chuàng)建一個(gè)通用方法,該方法將通過所有表上存在的公共 Id 字段選擇任何實(shí)體.>
I am new to LINQ to SQL and attempting to create a generic Data Access Object (DAO) for the basic Create, Read, Update, and Destroy (CRUD) methods so that I can reuse the code. I was successful in creating a generic method that will delete any entity by using the code below but, I was wondering if anyone knows how to create a generic method that will select any entity by a common Id field that exists on all tables.
/// <summary>
/// Generic method that deletes an entity of any type using LINQ
/// </summary>
/// <param name="entity"></param>
/// <returns>bool indicating whether or not operation was successful</returns>
public bool deleteEntity(Object entity)
{
try
{
DomainClassesDataContext db = new DomainClassesDataContext();
db.GetTable(entity.GetType()).Attach(entity);
db.GetTable(entity.GetType()).DeleteOnSubmit(entity);
db.SubmitChanges();
return true;
}
catch(Exception ex)
{
Console.WriteLine(ex.StackTrace);
return false;
}
}
我很確定相同的模式將適用于更新和插入,并希望在 GenericDAO 上有一個(gè)通用方法,該方法將基于實(shí)體 ID.預(yù)先感謝您的回復(fù).
I am pretty sure that the same patter will work for update and insert and would like to have a generic method on the GenericDAO that will retrieve me any entity (i.e. Customer, Invoice, WorkOrder, etc...) based on the entities Id. Thanks in advance for the replies.
推薦答案
我認(rèn)為您正在尋找 Repository Pattern,下面是它的簡單實(shí)現(xiàn):
I think you are looking for Repository Pattern, the following is a simple implementation of it:
首先你需要像這樣創(chuàng)建一個(gè)IRepository
接口:
First you need to create an interface IRepository
like this:
public interface IRepository<T> where T : class
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
IEnumerable<T> All();
...
}
那么:
public class Repository<T> : IRepository<T>
where T : class, IEntity
{
DataContext _db;
public Repository()
{
_db = new DataContext("Database string connection");
_db.DeferredLoadingEnabled = false;
}
public void Add(T entity)
{
if (!Exists(entity))
GetTable.InsertOnSubmit(entity);
else
Update(entity);
SaveAll();
}
public void Delete(T entity)
{
GetTable.DeleteOnSubmit(entity);
SaveAll();
}
public void Update(T entity)
{
GetTable.Attach(entity, true);
SaveAll();
}
System.Data.Linq.Table<T> GetTable
{
get { return _db.GetTable<T>(); }
}
public IEnumerable<T> All()
{
return GetTable;
}
}
然后:
public class CustomerRepository : Repository<Customer>
{
public ProductRepository()
: base()
{
}
}
然后你可以有類似的東西:
Then you can have something like:
Customer newCustomer = new Customer { FistName = "Foo", LastName = "Boo" };
_customerRepository.Add(newCustomer);
其中Customer
是映射到您的數(shù)據(jù)庫的實(shí)體,該實(shí)體在.dbml
中定義.這只是一個(gè)開始,有關(guān)詳細(xì)信息,請參閱以下內(nèi)容:
Where Customer
is an entity mapped to your database which is defined in the .dbml
. This is just a start, see the following for more details:
- 在 LINQ-to-SQL 中實(shí)現(xiàn)存儲(chǔ)庫模式.
- LINQ to SQL 和存儲(chǔ)庫模式.
- 在 LINQ to SQL 中實(shí)現(xiàn) IRepository 模式.
- Repository Pattern的實(shí)現(xiàn)示例在 LINQ to SQL 中.
這篇關(guān)于如何使用 LINQ to SQL 創(chuàng)建通用數(shù)據(jù)訪問對象 (DAO) CRUD 方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!