1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using HengxinOML.Entity; 6 using AndonSys.Utility.Business; 7 using NHibernate; 8 using NHibernate.Cfg; 9 using AndonSys.Utility.Entity; 10 11 namespace HengxinOML.Business 12 { 13 public abstract class BusinessBase<T> : IRepository<T> where T : EntityBase, new() 14 { 15 private LinqContext linqContext; 16 public virtual LinqContext LingContext 17 { 18 get 19 { 20 if (linqContext == null) 21 { 22 linqContext = new LinqContext(); 23 } 24 return linqContext; 25 } 26 } 27 public virtual ISession Session 28 { 29 get 30 { 31 return NHibernateHelpers.GetCurrentSession(); 32 } 33 } 34 public BusinessBase() 35 { 36 37 } 38 public virtual void DeleteEntity(T entity) 39 { 40 Session.Delete(entity); 41 } 42 public virtual object AddEntity(T entity) 43 { 44 return Session.Save(entity); 45 } 46 public virtual void UpdateEntity(T entity) 47 { 48 Session.Update(entity); 49 } 50 public virtual void SaveEntity(T entity) 51 { 52 if (string.IsNullOrEmpty(entity.__status)) 53 Session.SaveOrUpdate(entity); 54 else if (entity.__status == EntityAction.delete.ToString()) 55 DeleteEntity(entity); 56 else if (entity.__status == EntityAction.add.ToString()) 57 AddEntity(entity); 58 else 59 UpdateEntity(entity); 60 } 61 public virtual T GetEntity(object id) 62 { 63 return Session.Get<T>(id); 64 } 65 public virtual void Load(T entity, object id) 66 { 67 Session.Load(entity, id); 68 } 69 70 71 } 72 }
实现IRepository<T>接口;
泛型T必须是从EntityBase继承,并且必须有无参构造函数。
谢谢,我看懂了
这里面啥事都没做啊,就是用Repository把Nhibernate又封装了一遍...
只要是那个格式没见过,这样一说也就明了了,谢谢你!
where是个限定语句,限定了泛型T的对象范围,只能是EntityBase或者EntityBase子类,而且毕竟包含无参数构造函数
谢谢你