将不确定变为确定~接口应该是什么

回到目录

接口到底是什么,应该如何去使用它呢?

书上说:“接口规定了一组操作的规范,它将一些不相关的对象联系在一起”,说是这样说,但在实际当中我们应该如何去用接口,如何去用好接口呢?

事实上,我一直认为,在面向对象中,接口就是大哥大,用好接口,理解好接口,你对面向对象的认识也将上升一个新的台阶,接口不可遗忘的功能就是它实现了面向对象的多态性,多态,即一种对象,在接受指定信息时,实现指定的对象实例,这其实就是我对多态的比较直观的理解。

一个用户操作的接口可能是这样被定义:

 1     /// <summary>
 2     /// 用户相关接口规范
 3     /// </summary>
 4     public interface IUserRepository : Data.IRepository<UserBases>
 5     {
 6         /// <summary>
 7         /// 注册流程
 8         /// </summary>
 9         /// <param name="entity"></param>
10         VMessage RegisterUser(UserBases entity);
11 
12         void UpdateUser(UserBases entity);
13         /// <summary>
14         /// 登陆
15         /// </summary>
16         /// <param name="entity"></param>
17         /// <returns></returns>
18         VMessage Login(UserBases entity);
19 
20         /// <summary>
21         /// 修改密码
22         /// </summary>
23         /// <param name="entity"></param>
24         /// <returns></returns>
25         VMessage ChangePassword(UserBases entity);
26      }

但对于它的实现,我们可以有多种,如可以是sqlserver的,而对于sqlserver来说,又可以是linq to sql的,ado.net的及ef的等等。

而对于一个用户操作的实现类,它将被动的去实现IUserRepository的基接口IRepository,代码如下:

  1   /// <summary>
  2     /// 用户操作实现类
  3     /// sqlserver-linq to sql实现
  4     /// </summary>
  5     public class UserRepository : Car_RentalRepositoryBase, IUserRepository
  6     {
  7 
  8         #region IUserRepository 成员
  9 
 10         public VMessage RegisterUser(UserBases entity)
 11         {
 12             entity.Password = Utility.EncryptString(entity.Password, Utility.EncryptorType.MD5);
 13             VMessage vm = new VMessage();
 14             try
 15             {
 16                 base.InsertEntity(entity);
 17                 vm.IsComplete = true;
 18             }
 19             catch (Exception)
 20             {
 21                 vm.IsComplete = false;
 22                 throw;
 23             }
 24             return vm;
 25 
 26         }
 27 
 28         public void UpdateUser(UserBases entity)
 29         {
 30             DB.SubmitChanges();
 31         }
 32 
 33         public VMessage Login(UserBases entity)
 34         {
 35             VMessage vm = new VMessage();
 36             try
 37             {
 38                 entity = this.GetModel().Where(i => i.Name == entity.Name && i.Password == Utility.EncryptString(entity.Password, Utility.EncryptorType.MD5)).FirstOrDefault();
 39                 if (entity != null)
 40                 {
 41                     vm.IsComplete = true;
 42                     vm.Entity = entity;
 43                 }
 44                 else
 45                     vm.IsComplete = false;
 46             }
 47             catch (Exception)
 48             {
 49                 vm.IsComplete = false;
 50                 throw;
 51             }
 52             return vm;
 53         }
 54 
 55         public VMessage ChangePassword(UserBases entity)
 56         {
 57             VMessage vm = new VMessage();
 58             try
 59             {
 60                 vm = this.Login(entity);
 61 
 62                 if (vm.IsComplete)
 63                 {
 64                     entity = vm.Entity as UserBases;
 65                     entity.Password = Utility.EncryptString(entity.NewPassword, Utility.EncryptorType.MD5);
 66                     this.Update(entity);
 67                     vm.IsComplete = true;
 68                 }
 69                 else
 70                     vm.IsComplete = false;
 71             }
 72             catch (Exception)
 73             {
 74                 vm.IsComplete = false;
 75                 throw;
 76             }
 77             return vm;
 78         }
 79         #endregion
 80 
 81         #region IRepository<UserBases> 成员
 82 
 83         public void Update(UserBases entity)
 84         {
 85             base.UpdateEntity(entity);
 86         }
 87 
 88         public void Update(IList<UserBases> list)
 89         {
 90             list.ToList().ForEach(entity => { this.Update(entity); });
 91         }
 92 
 93         public void Insert(UserBases entity)
 94         {
 95             base.InsertEntity(entity);
 96         }
 97 
 98         public void Insert(IList<UserBases> list)
 99         {
100             list.ToList().ForEach(entity => { this.Insert(entity); });
101         }
102 
103         public UserBases InsertGetIDENTITY(UserBases entity)
104         {
105             base.InsertEntity(entity);
106             return base.Find<UserBases>(entity.UserID);
107         }
108 
109         public void Delete(UserBases entity)
110         {
111             base.DeleteEntity(entity);
112         }
113 
114         public void Delete(IList<UserBases> list)
115         {
116             list.ToList().ForEach(entity => { this.Delete(entity); });
117         }
118 
119         public IQueryable<UserBases> GetModel()
120         {
121             return base.GetEntities<UserBases>().Select(item => new UserBases_Ext
122             {
123                 CreateDate = item.CreateDate,
124                 Email = item.Email,
125                 Name = item.Name,
126                 Status = item.Status,
127                 UpdateDate = item.UpdateDate,
128                 UserID = item.UserID,
129                 Password = item.Password,
130                 ConfirmCode = item.ConfirmCode,
131                 ConfirmPassword = item.ConfirmPassword,
132                 IDNumber = item.IDNumber,
133                 IDType = item.IDType,
134                 QQ = item.QQ,
135                 Tel = item.Tel,
136                 TelVerify = item.TelVerify,
137             });
138         }
139 
140         public UserBases Find(params object[] keyValues)
141         {
142             return base.Find<UserBases>(keyValues);
143         }
144 
145         #endregion
146     }

而在业务层去调用时,你可以使用IOC去建立实例,也可以在程序里写死,但它为业务层开放时,尽量是“构架方法”注入式的,以下是商品业务模块的代码片断:

 1   /// <summary>
 2     /// 商品模块实现
 3     /// </summary>
 4     public class ProductService : ServiceBase, IProductService
 5     {
 6         IProductRepository iProductRepository = null;
 7         /// <summary>
 8         /// 默认构造方法
 9         /// </summary>
10         public ProductService()
11             : this(new ProductRepository())
12         {
13 
14         }
15         /// <summary>
16         /// 构造方法,可以在建立实例时进行实现方式的选择
17         /// </summary>
18         /// <param name="_iProductRepository"></param>
19         public ProductService(IProductRepository _iProductRepository)
20         {
21             iProductRepository = _iProductRepo

而这样,在上一层就可以转入接口的一个实现就可以了,这样是否方便TDD呢,呵呵。

回到目录

posted @ 2012-06-05 23:53  张占岭  阅读(2033)  评论(7编辑  收藏  举报