首页 新闻 会员 周边

有么有大神给讲解一些这个段代码,万分感谢!主要是抽象类后面格式没见过

0
悬赏园豆:20 [已解决问题] 解决于 2016-06-24 09:12
 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 }
Sola0921的主页 Sola0921 | 初学一级 | 园豆:5
提问于:2016-06-23 16:47
< >
分享
最佳答案
1
: IRepository<T> where T : EntityBase, new()

实现IRepository<T>接口;

泛型T必须是从EntityBase继承,并且必须有无参构造函数。

收获园豆:10
写代码的小2B | 老鸟四级 |园豆:4371 | 2016-06-23 17:35

谢谢,我看懂了

Sola0921 | 园豆:5 (初学一级) | 2016-06-24 09:00
其他回答(2)
1

这里面啥事都没做啊,就是用Repository把Nhibernate又封装了一遍...

收获园豆:5
爱编程的大叔 | 园豆:30839 (高人七级) | 2016-06-23 17:21

只要是那个格式没见过,这样一说也就明了了,谢谢你!

支持(0) 反对(0) Sola0921 | 园豆:5 (初学一级) | 2016-06-24 09:00
1

where是个限定语句,限定了泛型T的对象范围,只能是EntityBase或者EntityBase子类,而且毕竟包含无参数构造函数

收获园豆:5
舞动字节 | 园豆:346 (菜鸟二级) | 2016-06-24 08:32

谢谢你

支持(0) 反对(0) Sola0921 | 园豆:5 (初学一级) | 2016-06-24 09:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册