成都公司:成都市成华区建设南路160号1层9号
重庆公司:重庆市江北区红旗河沟华创商务大厦18楼
利用构造方法实现模块的解耦
解耦,不只只是对顺序的扩展性而言,它能够照样你运用你的顺序从一个层面向另一个层面进步的根底,请仔细看待这个词语“解耦”。
我置信,它将会成为与“SOA”,“散布式”,“云核算”,“KV存储”,“高并发”一样的抢手的器械,我确信这点。今后,我将会持续存眷这个词语“解耦”。
今日首要是讲”代码之美“的一个话题,应用结构办法使你的对象进行一个可供注入的接口,这就是IOC里面注入的一种方法,即”结构器注入“。
.jpg)
-
///-
/// 一致实体
-
///
-
public class EntityBase
-
{-
-
}-
///
-
/// 一致操作
-
///
-
public interface IRepository
-
{-
void Insert(EntityBase entity);
-
}-
///
-
/// 用户操作完成
-
///
-
public class UserRepository : IRepository
-
{-
#region IRepository 成员-
-
public void Insert(EntityBase entity)
-
{-
throw new NotImplementedException();
-
}-
-
#endregion-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
而在结构办法去运用它的时分,普通代码是如许:
-
public abstract class IndexFileBase-
{-
IRepository _iRepository;-
public IndexFileBase(IRepository iRepository)
-
{-
_iRepository = iRepository;-
}-
-
///
-
/// 依据完成IRepository接口的分歧,Insert逻辑也是多样的
-
///
-
/// <param name="entity">
-
public void Insert(EntityBase entity)
-
{-
this._iRepository.Insert(entity);
-
}
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
上面的代码,很好的完成了new对象的松耦合,这使得它具有通用的特征,普通我们在设计通用功用时,司理运用如许方法。

