如何设计仓库模式以便轻松切换到另一个ORM? [英] how to design Repository pattern to be easy switch to another ORM later?

查看:230
本文介绍了如何设计仓库模式以便轻松切换到另一个ORM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的存储库模式,但我试过,我的目标是做一个设计,让我轻松地只需要几个编辑依赖注入或配置编辑,以便能够切换到另一个ORM而不触及其他解决方案



我达到了这个实现:



这里是代码:

  public interface IRepository< T> 
{
T Get(int key);
IQueryable< T>得到所有();
void Save(T entity);
T更新(T实体);
//将在此添加常用数据
}
public interface ICustomerRepository:IRepository< Customer>
{
//客户资料库的具体操作
}
public class CustomerRepository:ICustomerRepository
{
#region ICustomerRepository成员

public IQueryable< Customer> GetAll()
{
DataClasses1DataContext context = new DataClasses1DataContext();
从客户在上下文中返回客户选择客户;
}

#endregion

#region IRepository< Customer>成员

public Customer Get(int key)
{
throw new NotImplementedException();
}

public void Save(Customer entity)
{
throw new NotImplementedException();
}

public客户更新(客户实体)
{
抛出新的NotImplementedException();
}

#endregion
}

在我的aspx页面中:

  protected void Page_Load(object sender,EventArgs e)
{
IRepository&顾客> repository = new CustomerRepository();
var customers = repository.GetAll();

this.GridView1.DataSource = customers;
this.GridView1.DataBind();
}

如前所述,我现在使用LINQ to sql,正如你看到我的代码绑定到LINQ到sql,如何改变这个代码设计来实现我的目标能够更改到另一个ORM,例如ADO.net实体框架,或亚音速



请使用简单的示例代码建议

解决方案

Inc Wall o'Text



您正在做的是正确的,您的代码将应用于每个存储库。



As您表示,存储库模式的目的是使您可以将数据交付到应用程序的方式进行交换,而无需在应用程序(UI /传递层)中重构代码。



例如,您决定切换到实体或ADO.NET。



所有你需要的是编写你将要使用的ORM的代码(让它继承正确的接口),然后让你的代码使用该存储库。当然,您将需要替换旧存储库的所有引用,或重命名/替换旧的ORM存储库,以便您的应用程序使用适当的应用程序(除非您使用某种类型的IoC容器,否则您将在其中指定要通过哪个存储库) 。



您的应用程序的其余部分将继续正常运行,因为您用于获取/编辑数据的所有方法都将返回正确的对象。



以外行人士的名义,存储库将以相同的方式向您的应用程序提供所需的数据。唯一的区别是如何从数据库中检索数据(ADO.NET/Linq等等)



让你的类继承存储库接口是一个硬约束确保他们以统一的方式输出与您的应用程序使用方式一致的数据。


I am new to repository pattern but i tried, my goal is to make a design which will let me easily with just some few edits "dependency injection, or configuration edits" to be able to switch to another ORM without touching other solution layers.

I reached this implementation:

and here is the code:

public interface IRepository<T>
{
    T Get(int key);
    IQueryable<T> GetAll();
    void Save(T entity);
    T Update(T entity);
    // Common data will be added here
}
public interface ICustomerRepository : IRepository<Customer> 
{
    // Specific operations for the customers repository
}
public class CustomerRepository : ICustomerRepository
{
    #region ICustomerRepository Members

    public IQueryable<Customer> GetAll()
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        return from customer in context.Customers select customer;
    }

    #endregion

    #region IRepository<Customer> Members

    public Customer Get(int key)
    {
        throw new NotImplementedException();
    }

    public void Save(Customer entity)
    {
        throw new NotImplementedException();
    }

    public Customer Update(Customer entity)
    {
        throw new NotImplementedException();
    }

    #endregion
}

usage in my aspx page:

protected void Page_Load(object sender, EventArgs e)
    {
        IRepository<Customer> repository = new CustomerRepository();
        var customers = repository.GetAll();

        this.GridView1.DataSource = customers;
        this.GridView1.DataBind();
    }

As you saw in the previous code i am now using LINQ to sql, and as you see my code is tied to LINQ to sql, how to change this code design to achieve my goal "be able to change to another ORM easly, for example to ADO.net entity framework, or subsonic"

Please advice with simple sample code

解决方案

Inc Wall o' Text

What you're doing is right, your code would be applied to each repository.

As you stated, the purpose of the Repository pattern is so you can interchange the way the data is delivered to your application without having to refactor your code in your application (UI/delivery layer).

Take for example, you decide to switch to Linq to Entities or ADO.NET.

All you would need is to write the code for the ORM you'll be using (having it inherit the proper Interface), then have your code use that repository. Of course you would need replace all references of the old repository or rename/replace your old ORM repositories so that your application uses the proper ones (Unless you're using some type of IoC container, in which you would specify which repository to pass).

The rest of your application would continue to run properly since all the methods you used to get/edit your data will return the proper objects.

In layman's term, the repositories will give your application the data it needs the same way. The only difference is how that data is retrieved from your database (ADO.NET/Linq to something etc.)

Having your classes inherit the Repository Interfaces is a hard-constraint making sure they output the data in a uniform fashion that agrees with the way your application uses it.

这篇关于如何设计仓库模式以便轻松切换到另一个ORM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆