我是新来存储库模式,但我想,我的目标是做一个设计,这将让我轻松地只是一些小的修改“依赖注入,或配置编辑”为能够切换到另一个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到SQL,如何改变这种代码设计,实现我的目标“能够改变到另一个ORM伊斯利,例如到ADO.net实体框架,或亚音速”
请简单的示例代码忠告