久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 LINQ to SQL 創(chuàng)建通用數(shù)據(jù)訪問對象 (DAO)

How To Create Generic Data Access Object (DAO) CRUD Methods with LINQ to SQL(如何使用 LINQ to SQL 創(chuàng)建通用數(shù)據(jù)訪問對象 (DAO) CRUD 方法)
本文介紹了如何使用 LINQ to SQL 創(chuàng)建通用數(shù)據(jù)訪問對象 (DAO) CRUD 方法的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我是 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應(yīng)該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設(shè)置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時(shí)如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調(diào)用)
主站蜘蛛池模板: 亚洲国产一区在线 | 欧美不卡一区二区三区 | 美女午夜影院 | 免费特级黄毛片 | 91精品中文字幕一区二区三区 | 91精品一区二区三区久久久久 | 在线成人av | 中文字幕亚洲视频 | 亚洲第一黄色网 | 亚洲天堂色 | 国产精品久久久久久久岛一牛影视 | 国产伦精品一区二区三区高清 | 男女下面一进一出网站 | 天天艹天天干天天 | 国产欧美一区二区三区在线看蜜臀 | 黄色大片免费看 | 不卡在线视频 | 精品欧美一区免费观看α√ | 在线观看视频h | 一区二区日韩精品 | 999精品视频 | tube国产| 亚洲免费网站 | 免费一区二区 | 一级黄色录像毛片 | 成人精品一区 | 一区二区三区免费在线观看 | 精品一二区 | 伊人久久综合 | 日本高清视频在线播放 | 精精精精xxxx免费视频 | 天天干天天谢 | 久久久美女 | 国产成人在线一区二区 | 国产综合久久 | 欧美一级片在线观看 | 香蕉久久网 | 中文字幕乱码亚洲精品一区 | 国产精品美女久久久久久免费 | 精品久久久久久久人人人人传媒 | 成人欧美一区二区三区在线观看 |