【Yom框架】漫谈个人框架的设计之二:新的IRepository接口+搜索和排序解耦(+基于Castle实现)

时间:2022-11-11 14:45:57

经过了上篇IRepositoryIRepository<T>的讨论【文章地址为:http://www.cnblogs.com/yomho/p/3296759.html

我选择了IRepository作为我重构框架的仓储接口

一、接口定义  

新的IRepository接口设计如下:

 namespace Yom.NFramework2_0
{
public interface IRepository<TCompositeKey>
where TCompositeKey : IEquatable<string>
{
#region 实体操作接口
T FindBy<T>(TCompositeKey primaryKey) where T : IEntity;
IEnumerable<T> FindAll<T>() where T : IEntity;
IEnumerable<T> FindAll<T>(IWhere[] where) where T : IEntity;
IEnumerable<T> FindAll<T>(IWhere[] where, IOrder[] order) where T : IEntity;
IEnumerable<T> FindAll<T>(int pageIndex, int pageSize, IWhere[] where, IOrder[] order, out int count) where T : IEntity;
void Add<T>(T entity) where T : IEntity;
void Delete<T>(T entity) where T : IEntity;
void DeleteAll<T>() where T : IEntity;
void DeleteAll<T>(IWhere[] where) where T : IEntity;
void DeleteAll<T>(string where) where T : IEntity;
void DeleteAll<T>(IEnumerable<TCompositeKey> pkValues) where T : IEntity;
void Update<T>(T entity) where T : IEntity;
bool Exists<T>(TCompositeKey primaryKey) where T : IEntity;
#endregion
#region 静态方法接口
int ExecuteSql(string sql, params System.Data.IDataParameter[] ps);
object ExecuteScalar(string sql, params System.Data.IDataParameter[] ps);
System.Data.DataTable ExecuteDataTable(string sql, params System.Data.IDataParameter[] ps);
#endregion
}
}

其中IEntity和IWhere以及IOrder的设计接口如下:

public interface IEntity
{
}
public interface IOrder
{
}
public interface IWhere
{
}
public interface ICompositeKey : IEquatable<string>
{
}

ICompositeKey为组合主键接口

为什么ICompositeKey要继承为IEquatable<string>这个接口呢?

因为IEquatable<string>是string的基类,且string为不能继承的类,

所以目的是和string兼容,经过处理可以变成一个string类,从而可以兼容普通的非组合主键。

(此文不讲组合主键的实现原理,展示组合主键接口只是让大家看看组合主键实现解耦的方法)

二、嫁接Castle实现搜索以及排序

很多人不知道怎么把自己的IRepository接口的排序和搜索对象解耦,

在用不同的ORM实现IRepository接口的时候总不知道怎么弄排序和搜索

下面就来看我怎么把Castle的排序和搜索用IOrder以及IWhere解耦的

首先是IOrder -> NHibernate.Expression.Order 的变身

 namespace Yom.NFramework2_0.CastleExtend
{
public class OrderBase : NHibernate.Expression.Order, Yom.NFramework2_0.IOrder
{
public OrderBase(string propertyName, bool ascending) : base(propertyName, ascending) { }
}
}

这个很简单,OrderBase一下子就变成了NHibernate.Expression.Order

那么IWhere是否可以顺利完成像IOrder这种变身么?

研究后发现:Castle是用NHibernate.Expression.Expression的静态方法创建查询对象的

静态方法都是返回NHibernate.Expression.AbstractCriterion抽象类或者ICriterion接口

可以继承这个抽象对象或者接口吧,这样就可以实现变身了!!!

但如果真要这样做,必然有抽象方法要实现,这实在是一个下下策(想嫁接个接口和抽象类不容易吖)

那有没有直接实现NHibernate.Expression.AbstractCriterion抽象类的类呢?

反正我是没有找到,但是Castle肯定有,如果找到这么个类,就可以像IOrder一样实现嫁接,现在没有发现只能想他法。

最后我对WhereBase的变身是:

 namespace Yom.NFramework2_0.CastleExtend
{
public class WhereBase : Yom.NFramework2_0.IWhere
{
public NHibernate.Expression.ICriterion Instance
{
get;
set;
}
public override string ToString()
{
return Instance.ToString();
}
#region 废弃代码
//public static void AllEq(WhereBase where, System.Collections.IDictionary propertyNameValues)
//{
// where.Instance = NHibernate.Expression.Expression.AllEq(propertyNameValues);
//}
//public static void And(WhereBase where, WhereBase whereA, WhereBase whereB)
//{
// where.Instance = NHibernate.Expression.Expression.And(whereA.Instance, whereB.Instance);
//}
//public static void Between(WhereBase where, string propertyName, object lo, object hi)
//{
// where.Instance = NHibernate.Expression.Expression.Between(propertyName, lo, hi);
//}
//public static void Eq(WhereBase where, string propertyName, object value) {
// where.Instance = NHibernate.Expression.Expression.Eq(propertyName, value);
//}
//public static void EqProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.EqProperty(propertyName, otherPropertyName);
//}
//public static void Ge(WhereBase where, string propertyName, object value)
//{
// where.Instance = NHibernate.Expression.Expression.Ge(propertyName, value);
//}
//public static void GeProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.GeProperty(propertyName, otherPropertyName);
//}
//public static void Gt(WhereBase where, string propertyName, object value)
//{
// where.Instance = NHibernate.Expression.Expression.Gt(propertyName, value);
//}
//public static void GtProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.GtProperty(propertyName, otherPropertyName);
//}
//public static void IdEq(WhereBase where, object value)
//{
// where.Instance = NHibernate.Expression.Expression.IdEq(value);
//}
//public static void In(WhereBase where, string propertyName, System.Collections.ICollection values)
//{
// where.Instance = NHibernate.Expression.Expression.In(propertyName, values);
//}
//public static void InG<T>(WhereBase where, string propertyName, ICollection<T> values)
//{
// where.Instance = NHibernate.Expression.Expression.InG<T>(propertyName, values);
//}
//public static void InsensitiveLike(WhereBase where, string propertyName, object value)
//{
// where.Instance = NHibernate.Expression.Expression.InsensitiveLike(propertyName, value);
//}
//public static void IsEmpty(WhereBase where, string propertyName)
//{
// where.Instance = NHibernate.Expression.Expression.IsEmpty(propertyName);
//}
//public static void propertyName(WhereBase where, string propertyName)
//{
// where.Instance = NHibernate.Expression.Expression.IsNotEmpty(propertyName);
//}
//public static void IsNotNull(WhereBase where, string propertyName)
//{
// where.Instance = NHibernate.Expression.Expression.IsNotNull(propertyName);
//}
//public static void IsNull(WhereBase where, string propertyName)
//{
// where.Instance = NHibernate.Expression.Expression.IsNull(propertyName);
//}
//public static void Le(WhereBase where, string propertyName, object value)
//{
// where.Instance = NHibernate.Expression.Expression.Le(propertyName, value);
//}
//public static void LeProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.LeProperty(propertyName, otherPropertyName);
//}
//public static void Like(WhereBase where, string propertyName, string value, NHibernate.Expression.MatchMode matchModes)
//{
// where.Instance = NHibernate.Expression.Expression.Like(propertyName, value, matchModes);
//} //public static void Lt(WhereBase where, string propertyName, object value)
//{
// where.Instance = NHibernate.Expression.Expression.Lt(propertyName, value);
//}
//public static void LtProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.LtProperty(propertyName, otherPropertyName);
//}
//public static void Not(WhereBase where)
//{
// where.Instance = NHibernate.Expression.Expression.Not(where.Instance);
//}
//public static void NotEqProperty(WhereBase where, string propertyName, string otherPropertyName)
//{
// where.Instance = NHibernate.Expression.Expression.NotEqProperty(propertyName, otherPropertyName);
//}
//public static void Or(WhereBase where, WhereBase whereA, WhereBase whereB)
//{
// where.Instance = NHibernate.Expression.Expression.Or(whereA.Instance, whereB.Instance);
//}
//public static void Sql(WhereBase where, string sql)
//{
// where.Instance = NHibernate.Expression.Expression.Sql(sql);
//}
#endregion
}
}

最好注释也去掉,这样的好处是别人开发的可以不管ORM是用什么第三方框架,直接调用WhereBase静态方法(代理模式)

WhereBase直接携带了Castle的搜索接口ICriterion,

在RepositoryBase有个这么个方法转换IWhere为Castle的搜索对象

 #region 其他方法
NHibernate.Expression.ICriterion[] IWhere2ICriterion(IWhere[] where)
{
if (where == null) {
return null;
}
NHibernate.Expression.ICriterion[] wheres = new NHibernate.Expression.ICriterion[where.Length];
for (var i = ; i < where.Length; i++)
{
wheres[i] = (where[i] as WhereBase).Instance;
}
return wheres;
}
#endregion

这样就可以完美地实现搜索和排序的解耦

仓储基类有个方法实现如下:

 public IEnumerable<T> FindAll<T>(IWhere[] where) where T : IEntity
{
if (where == null) {
return FindAll<T>();
}
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll(IWhere2ICriterion(where));
}

三、总结:    

1:搜索和排序的解耦不算是个难题

2: 实现的仓储基类可以通过IoC注入实例化,这样可以插拔式更换ORM

3: 定义的仓储接口和实现最好放在不同的类库,以更好的解耦

四、后记:Repository实现仓储--Castle实现      

 namespace Yom.NFramework2_0.CastleExtend
{
public abstract class EntityBase : Castle.ActiveRecord.ActiveRecordBase, Yom.NFramework2_0.IEntity
{
}
}

实体基类嫁接

 namespace Yom.NFramework2_0
{
public interface ISinglePrimaryKeyRepository : IRepository<string>
{
}
}

非组合主键仓储扩展

 namespace Yom.NFramework2_0.CastleExtend
{
public class CastleSinglePrimaryKeyRepository : ISinglePrimaryKeyRepository//, System.Configuration.IConfigurationSectionHandler
{
#region IRepository<string> 成员 #region 实体相关操作
public T FindBy<T>(string primaryKey) where T : IEntity
{
return Castle.ActiveRecord.ActiveRecordBase<T>.Find(primaryKey);
} public IEnumerable<T> FindAll<T>() where T : IEntity
{
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll();
} public IEnumerable<T> FindAll<T>(IWhere[] where) where T : IEntity
{
if (where == null) {
return FindAll<T>();
}
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll(IWhere2ICriterion(where));
} public IEnumerable<T> FindAll<T>(IWhere[] where, IOrder[] order) where T : IEntity
{
if (order == null)
{
if (where == null)
{
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll();
}
else
{
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll(IWhere2ICriterion(where));
}
}
else if (where == null)
{
return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll(order as NHibernate.Expression.Order[]);
} return Castle.ActiveRecord.ActiveRecordBase<T>.FindAll(order as NHibernate.Expression.Order[], IWhere2ICriterion(where));
} public IEnumerable<T> FindAll<T>(int pageIndex, int pageSize, IWhere[] where, IOrder[] order, out int count) where T : IEntity
{
if (where == null)
{
count = Castle.ActiveRecord.ActiveRecordMediator.Count(typeof(T));
if (order == null) {
return Castle.ActiveRecord.ActiveRecordBase<T>.SlicedFindAll(pageIndex * pageSize, pageSize); }else{
return Castle.ActiveRecord.ActiveRecordBase<T>.SlicedFindAll(pageIndex * pageSize, pageSize, order as NHibernate.Expression.Order[]);
}
}
else
{
count = Castle.ActiveRecord.ActiveRecordMediator.Count(typeof(T), IWhere2ICriterion(where));
if (order == null) {
return Castle.ActiveRecord.ActiveRecordBase<T>.SlicedFindAll(pageIndex * pageSize, pageSize, IWhere2ICriterion(where));
}
}
return Castle.ActiveRecord.ActiveRecordBase<T>.SlicedFindAll(pageIndex * pageSize, pageSize, order as NHibernate.Expression.Order[], IWhere2ICriterion(where));
} public void Add<T>(T entity) where T : IEntity
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
(entity as Castle.ActiveRecord.ActiveRecordBase).CreateAndFlush();
tran.VoteCommit();
}
} public void Delete<T>(T entity) where T : IEntity
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
(entity as Castle.ActiveRecord.ActiveRecordBase).DeleteAndFlush();
tran.VoteCommit();
}
} public void DeleteAll<T>() where T : IEntity
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
Castle.ActiveRecord.ActiveRecordBase<T>.DeleteAll();
tran.VoteCommit();
}
} public void DeleteAll<T>(IWhere[] where) where T : IEntity
{
IEnumerable<T> entities;
if (where == null)
{
entities = this.FindAll<T>();
}
else
{
entities = this.FindAll<T>(where);
}
if (entities != null)
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
foreach (T entity in entities) {
this.Delete<T>(entity);
}
tran.VoteCommit();
}
}
}
public void DeleteAll<T>(string where) where T : IEntity
{ using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
Castle.ActiveRecord.ActiveRecordBase<T>.DeleteAll(where);
tran.VoteCommit();
} }
public void DeleteAll<T>(IEnumerable<string> pkValues) where T : IEntity
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
Castle.ActiveRecord.ActiveRecordBase<T>.DeleteAll(pkValues);
tran.VoteCommit();
}
} public void Update<T>(T entity) where T : IEntity
{
using (Castle.ActiveRecord.TransactionScope tran = new Castle.ActiveRecord.TransactionScope())
{
(entity as Castle.ActiveRecord.ActiveRecordBase).UpdateAndFlush();
tran.VoteCommit();
}
} public bool Exists<T>(string primaryKey) where T : IEntity
{
return Castle.ActiveRecord.ActiveRecordBase<T>.Exists<string>(primaryKey);
}
#endregion
#region ado执行sql
public int ExecuteSql(string sql, params System.Data.IDataParameter[] ps)
{
using (System.Data.IDbCommand cmd = Castle.ActiveRecord.ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(CastleSinglePrimaryKeyRepository)).Connection.CreateCommand())
{
cmd.CommandText = sql;
if (ps != null && ps.Length > )
{
foreach (System.Data.IDataParameter p in ps)
{
cmd.Parameters.Add(p);
}
}
return cmd.ExecuteNonQuery();
}
} public object ExecuteScalar(string sql, params System.Data.IDataParameter[] ps)
{
using (System.Data.IDbCommand cmd = Castle.ActiveRecord.ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(CastleSinglePrimaryKeyRepository)).Connection.CreateCommand())
{
cmd.CommandText = sql;
if (ps != null && ps.Length > )
{
foreach (System.Data.IDataParameter p in ps)
{
cmd.Parameters.Add(p);
}
}
return cmd.ExecuteScalar();
}
} public System.Data.DataTable ExecuteDataTable(string sql, params System.Data.IDataParameter[] ps)
{
using (System.Data.IDbCommand cmd = Castle.ActiveRecord.ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(CastleSinglePrimaryKeyRepository)).Connection.CreateCommand())
{
cmd.CommandText = sql;
if (ps != null && ps.Length > )
{
foreach (System.Data.IDataParameter p in ps)
{
cmd.Parameters.Add(p);
}
}
System.Data.DataTable result = new System.Data.DataTable();
result.Load(cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection));
return result;
}
}
#endregion
#endregion
#region 其他方法
NHibernate.Expression.ICriterion[] IWhere2ICriterion(IWhere[] where)
{
if (where == null) {
return null;
}
NHibernate.Expression.ICriterion[] wheres = new NHibernate.Expression.ICriterion[where.Length];
for (var i = ; i < where.Length; i++)
{
wheres[i] = (where[i] as WhereBase).Instance;
}
return wheres;
}
#endregion
}
}

Castle实现IReposoitory

五、项目开发的层次结构                                                                                              

【Yom框架】漫谈个人框架的设计之二:新的IRepository接口+搜索和排序解耦(+基于Castle实现)