如何创建通用数据访问对象

如何创建通用数据访问对象

本文介绍了如何创建通用数据访问对象(DAO)与CRUD方法LINQ到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的LINQ to SQL,并试图创造基本的创建,读取,更新通用数据访问对象(DAO),和销毁(CRUD)方法,使我可以重用的代码。我成功地创建将使用下面的代码删除任何实体,但如果有谁知道如何创建将选择由所有表上存在一个共同的标识字段的任何实体的泛型方法我在想,一个通​​用的方法。

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一个通用的方法,将获取我的任何实体(如客户,发票,工作单等)基础上的实体标识。在此先感谢您的答复。

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.

推荐答案

我认为你正在寻找的,下面是一个简单的实现它:

I think you are looking for Repository Pattern, the following is a simple implementation of it:

首先,你需要创建接口 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();
    ...
}



然后:

Then:

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;
    }
}



然后:

Then :

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);



其中,客户是映射到一个实体的这是在的.dbml 定义的数据库。这仅仅是一个开始,详情请参阅以下内容:

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:

  • Implementing Repository Pattern in LINQ-to-SQL.
  • LINQ to SQL and Repository Pattern.
  • Implementing IRepository Pattern in LINQ to SQL.
  • Implementation example of Repository Pattern in LINQ to SQL.

这篇关于如何创建通用数据访问对象(DAO)与CRUD方法LINQ到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:03