您可以使用每种数据访问技术来利用存储库.
存储库是对现有数据访问助手/服务的抽象,允许业务逻辑与数据访问层分离.存储库与Query一起使用以启用过滤.它通常与工作单元一起使用,以将更改存储回数据库.
存储库至少有:
> Get-object-by-key操作
> Get-all-objects操作
> Get-first-object-by-query操作
>按查询对象获取操作
一个非常简单的例子:):
A.产品类,在Common中定义:
public class Product
{
public int Id { get; private set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
B. Query,IRepository和IUnitOfWork的类在DAL.interfaces.dll或Common.dll中定义(但不在DAL中!).
public class Query
{
public string Text { get; set; }
}
public interface IRepository
where TEntity : class
{
bool TryGet(int key, out TEntity value);
TEntity this[int key] { get; }
IEnumerable GetAll();
bool TryGetFirst(Query condition, out TEntity value);
TEntity GetFirst(Query condition);
IEnumerable GetAll(Query condition);
int Count { get; }
}
public interface IUnitOfWork
{
void SetAdded(TEntity value); // Marks entity as added for further INSERT
void SetRemoved(TEntity value); // Marks entity as removed for further DELETE
void SetChanged(TEntity value); // Marks entity as modified for further UPDATE
void Save(); // Save all the changes
}
IUnitOfWork了解已更改的实体. Save()为每个更改的实体调用适当的DatabaseHelper / OdbcHelper CRUD方法,以便在数据库中保留更改.
IRepository< Product>,… IRepository< EntityXY>的实现和IUnitOFWork应放在DAL中.然后,BLL使用IRepository和IUnitOFWork来实现业务(域)逻辑. BLL本身可以组织为域模型顶部的服务层,但它超出了讨论的范围:).
我希望我的回答有所帮助.
请随时问我一个问题……