using Ninesky.IBLL; using Ninesky.IDAL; namespace Ninesky.BLL { /// <summary> /// 服务基类 /// <remarks>创建:2014.02.03</remarks> /// </summary> public abstract class BaseService<T> : InterfaceBaseService<T> where T : class { protected InterfaceBaseRepository<T> CurrentRepository { get; set; } public BaseService(InterfaceBaseRepository<T> currentRepository) { CurrentRepository = currentRepository; } public T Add(T entity) { return CurrentRepository.Add(entity); } public bool Update(T entity) { return CurrentRepository.Update(entity); } public bool Delete(T entity) { return CurrentRepository.Delete(entity); } } }
这个类的构造函数中要传入一个参数就是currentRepository 这个在继承的时候进行传入。这里还是看用户类。
using Ninesky.DAL; using Ninesky.IBLL; using Ninesky.Models; using System.Linq; namespace Ninesky.BLL { /// <summary> /// 用户服务类 /// <remarks> /// 创建:2014.02.12 /// </remarks> /// </summary> public class UserService:BaseService<User>,InterfaceUserService { public UserService() : base(RepositoryFactory.UserRepository) { } public bool Exist(string userName) { return CurrentRepository.Exist(u => u.UserName == userName);} public User Find(int userID) { return CurrentRepository.Find(u => u.UserID == userID); } public User Find(string userName) { return CurrentRepository.Find(u => u.UserName == userName); } public IQueryable<User> FindPageList(int pageIndex, int pageSize, out int totalRecord, int order) { switch(order) { case 0: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, true, u => u.UserID); case 1: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, false, u => u.UserID); case 2: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, true, u => u.RegistrationTime); case 3: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, false, u => u.RegistrationTime); case 4: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, true, u => u.LoginTime); case 5: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, false, u => u.LoginTime); default: return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, true, u => u.UserID); } } } }
上面这个FindPageList代码太累赘了,一时还没想到好方法。
5、总结
今天写到这里还是在想项目间的调用实现,写了两个base接口、两个base类,以后其他的类都从它们继承,写法都很类似。下次可以开始做界面了,在Ninesky.Web项目中基本上是通过IBLL,BLL跟数据进行打交道了。
===================================================
FindPageList() 这个排序的方法确实不太通用,代码修改如下:
1、接口 InterfaceBaseRepository
修改两个接口方法如图红框部分。
image
2、BaseRepository类
添加OrderBy方法,代码如下:
/// <summary> /// 排序 /// </summary> /// <typeparam>类型</typeparam> /// <param>原IQueryable</param> /// <param>排序属性名</param> /// <param>是否正序</param> /// <returns>排序后的IQueryable<T></returns> private IQueryable<T> OrderBy(IQueryable<T> source, string propertyName, bool isAsc) { if (source == null) throw new ArgumentNullException("source", "不能为空"); if (string.IsNullOrEmpty(propertyName)) return source; var _parameter = Expression.Parameter(source.ElementType); var _property = Expression.Property(_parameter, propertyName); if (_property == null) throw new ArgumentNullException("propertyName", "属性不存在"); var _lambda = Expression.Lambda(_property, _parameter); var _methodName = isAsc ? "OrderBy" : "OrderByDescending"; var _resultExpression = Expression.Call(typeof(Queryable), _methodName, new Type[] { source.ElementType, _property.Type }, source.Expression, Expression.Quote(_lambda)); return source.Provider.CreateQuery<T>(_resultExpression); } 修改FindList和FindPageList方法,修改下图 image 3、修改UserService的FindPageList方法 修改后的代码如下: public IQueryable<User> FindPageList(int pageIndex, int pageSize, out int totalRecord, int order) { bool _isAsc = true; string _orderName = string.Empty; switch(order) { case 0: _isAsc = true; _orderName = "UserID"; break; case 1: _isAsc = false; _orderName = "UserID"; break; case 2: _isAsc = true; _orderName = "RegistrationTime"; break; case 3: _isAsc = false; _orderName = "RegistrationTime"; break; case 4: _isAsc = true; _orderName = "LoginTime"; break; case 5: _isAsc = false; _orderName = "LoginTime"; break; default: _isAsc = false; _orderName = "UserID"; break; } return CurrentRepository.FindPageList(pageIndex, pageSize, out totalRecord, u => true, _orderName, _isAsc); }