
欢迎来到《FreeSql.Repository 仓储模式》系列文档,完整文档请前往 wiki 中心:https://github.com/dotnetcore/FreeSql/wiki
FreeSql是功能强大的 .NET ORM,支持 .NetFramework 4.0+、.NetCore 2.1+、Xamarin 等支持 NetStandard 所有运行平台。支持 MySql/SqlServer/PostgreSQL/Oracle/Sqlite/Firebird/达梦/神通/人大金仓/翰高/MsAccess 数据库。QQ群:4336577(已满)、8578575(在线)、52508226(在线)
FreeSql 支持五种使用方式,根据实际情况选择团队合适的一种:
本系列文档,专注介绍 【仓储+工作单元】 的使用方式。
仓储是一种设计模式概念,不同于以往的 DAL,在 .NET 世界人们往往把仓储向 DDD 靠近,又把 EFCore 向 DDD 靠近。
我理解的仓储对标 JPA,更像一种 ORM 规范,使得应用程序不再深度依赖某一个特定的 ORM。
使用仓储的目标:能低成本的切换 ORM
以上几点是仓储的几个基本功能要求,定义不宜复杂,越复杂最终切换 ORM 越困难。
简单的仓储接口定义如下:
public interface IBaseRepository : IDisposable
{
Type EntityType { get; }
IUnitOfWork UnitOfWork { get; set; }
}
public interface IBaseRepository<TEntity> : IBaseRepository
where TEntity : class
{
IDataFilter<TEntity> DataFilter { get; }
ISelect<TEntity> Select { get; }
ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp);
ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp);
TEntity Insert(TEntity entity);
List<TEntity> Insert(IEnumerable<TEntity> entitys);
int Update(TEntity entity);
int Update(IEnumerable<TEntity> entitys);
TEntity InsertOrUpdate(TEntity entity);
int Delete(TEntity entity);
int Delete(IEnumerable<TEntity> entitys);
int Delete(Expression<Func<TEntity, bool>> predicate);
}
public interface IBaseRepository<TEntity, TKey> : IBaseRepository<TEntity>
where TEntity : class
{
TEntity Get(TKey id);
TEntity Find(TKey id);
int Delete(TKey id);
}仓储定义越简单,切换 ORM 越容易没错,但是开发起来也越麻烦,鱼和熊掌不可兼得,需要找到一个平衡点。
FreeSql.Repository 在基本功能上有额外的定义:
后续文章将对 FreeSql.Repository 功能逐一展开解释。