【发布时间】:2009-10-02 01:00:39
【问题描述】:
我是存储库模式的新手,但我尝试过,我的目标是做出一个设计,让我只需进行一些编辑“依赖注入或配置编辑”即可轻松切换到另一个 ORM 而无需触及其他解决方案层。
我达到了这个实现:
这里是代码:
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
}
在我的 aspx 页面中的用法:
protected void Page_Load(object sender, EventArgs e)
{
IRepository<Customer> repository = new CustomerRepository();
var customers = repository.GetAll();
this.GridView1.DataSource = customers;
this.GridView1.DataBind();
}
正如您在前面的代码中看到的,我现在使用 LINQ to sql,并且您看到我的代码与 LINQ to sql 相关联,如何更改此代码设计以实现我的目标“能够轻松更改为另一个 ORM ,例如到 ADO.net 实体框架,或者 subsonic"
请提供简单的示例代码
【问题讨论】:
标签: c# .net linq-to-sql design-patterns repository-pattern