当前位置: 首页 > wzjs >正文

美容加盟的网站建设网页设计是哪个专业

美容加盟的网站建设,网页设计是哪个专业,家装设计费一般多少钱一平方,网上商城怎么推广IOC容器 基于IoC思想,管理对象创建的核心对象常见IoC容器 Autofac、Unity、DryIoc、Spring.Net、Injection、SimpleIOC、SmartIOC、……… IoC模式的使用 自定义容器过程 基本类型管理:瞬时实例、单例 瞬时实例:每次创建的时候都是新的实…

IOC容器

  • 基于IoC思想,管理对象创建的核心对象
  • 常见IoC容器
    • Autofac、Unity、DryIoc、Spring.Net、Injection、SimpleIOC、SmartIOC、………

IoC模式的使用

自定义容器过程

基本类型管理:瞬时实例、单例

瞬时实例:每次创建的时候都是新的实例,new ()

单例实例:只有第一次创建,之后都是用的第一个实例,不会重新new 和覆盖。

无限级依赖注入

在构造函数中,注入的实例中实例有自己的注入实例,需要使用递归进行无限注入

属性注入

在使用的类中,进行属性就可以完成属性注入

注意:属性注入需要在构造函数执行完之后。

单接口多实现注入

同一个接口有多个继承,但是不同的界面需要不同的继承的注入,所以需要实现单接口多实现注入

核心代码如下:

初始化:
public static XiaohaiIoc Default
{get{if (_default == null){lock (_instanceLock){if (_default == null){_default = new XiaohaiIoc();}}}return _default;}
}
单个类型的注册
瞬时状态
public void Register<T>()
{//_objectDic.Add(typeof(T).FullName!, typeof(T));_instenceDic.Add(typeof(T).FullName!,new InstenceModel{ObjectType = typeof(T),IsSinglton = false,});
}
单例模式
public void RegisterSingle<T>()
{_instenceDic.Add(typeof(T).FullName!,new InstenceModel{ObjectType = typeof(T),IsSinglton = true,});
}
带接口的注册(多继承关系类注册)
瞬时状态
public void Register<TFrom, TTo>(String token = "") where TTo : TFrom
{GetRegister<TFrom, TTo>(false, token);
}private void GetRegister<TFrom, TTo>(bool bl, String token = ""){string key = typeof(TFrom).FullName + "_%XH%_" + (string.IsNullOrEmpty(token) ? typeof(TTo).Name : token);_instenceDic.Add(key, new InstenceModel{ObjectType = typeof(TTo),IsSinglton = bl,});}
单例模式:
public void RegisterSingle<TFrom, TTo>(String token = "") where TTo : TFrom
{GetRegister<TFrom, TTo>(true, token);
}private void GetRegister<TFrom, TTo>(bool bl, String token = ""){string key = typeof(TFrom).FullName + "_%XH%_" + (string.IsNullOrEmpty(token) ? typeof(TTo).Name : token);_instenceDic.Add(key, new InstenceModel{ObjectType = typeof(TTo),IsSinglton = bl,});}
获取实例
 public T Resolve<T>(){string key = typeof(T).FullName!;if (_instenceDic.ContainsKey(key)){// 单例模式没有对象 和瞬时模式都需要创建对象if ((_instenceDic[key].IsSinglton && _instenceDic[key].Instence is null) || !_instenceDic[key].IsSinglton)return (T)CreateInstence(key, _instenceDic[key].ObjectType);//单例模式 有对象直接返回return (T)_instenceDic[key].Instence;}elsereturn default(T)!;}private object CreateInstence(string key, Type type)
{#region 检查构造函数// 获取所有的构造函数ConstructorInfo[] cis = _instenceDic[key].ObjectType.GetConstructors();// 获取所有的参数ParameterInfo[] cpis = cis[0].GetParameters();// 创建args 参数列表List<object> objects = new List<object>();foreach (ParameterInfo cpi in cpis){string paramTypeKey = cpi.ParameterType.FullName!;if (cpi.IsDefined(typeof(DependyAttribute), false)){var attr = cpi.GetCustomAttribute<DependyAttribute>();paramTypeKey += "_%XH%_" + attr.Token;}if (_instenceDic.ContainsKey(paramTypeKey!)){if (_instenceDic[paramTypeKey].IsSinglton){if (_instenceDic[paramTypeKey].Instence is null)//_instenceDic[paramTypeKey].Instence = Activator.CreateInstance(_instenceDic[paramTypeKey].ObjectType)!;_instenceDic[paramTypeKey].Instence = CreateInstence(paramTypeKey, _instenceDic[paramTypeKey].ObjectType);}else_instenceDic[paramTypeKey].Instence = CreateInstence(paramTypeKey, _instenceDic[paramTypeKey].ObjectType);objects.Add(_instenceDic[paramTypeKey].Instence);}elseobjects.Add(null!);}#endregion// 实例化对象var obj = Activator.CreateInstance(_instenceDic[key].ObjectType, objects.ToArray());_instenceDic[key].Instence = obj!;#region 检查属性的注入// 那些属性需要注入 可以通过特性进行区分PropertyInfo[] pis = type.GetProperties();foreach (var pi in pis){// 判断属性是否有特定标记 如果有实例化这个属性if (pi.IsDefined(typeof(DependyAttribute), false)){var attr = pi.GetCustomAttribute<DependyAttribute>();// 获得属性注入 string paramTypeKey = pi.PropertyType.FullName! + "_%XH%_" + attr.Token;if (_instenceDic.ContainsKey(paramTypeKey!)){if (_instenceDic[paramTypeKey].IsSinglton){if (_instenceDic[paramTypeKey].Instence is null)_instenceDic[paramTypeKey].Instence = CreateInstence(paramTypeKey, _instenceDic[paramTypeKey].ObjectType);}else_instenceDic[paramTypeKey].Instence = CreateInstence(paramTypeKey, _instenceDic[paramTypeKey].ObjectType);// 如果有实例,已经创建过,设置给当前对象的对应属性// 参数:设置给哪个对象实例的属性pi.SetValue(obj, _instenceDic[paramTypeKey].Instence);}}}#endregionreturn _instenceDic[key].Instence;
}
注入模式类:
 class InstenceModel{public bool IsSinglton { get; set; } // 是不是单例模式public Type ObjectType { get; set; } // 数据类型public object Instence { get; set; } // 初始化数据}
特性类:
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Parameter)]
public class DependyAttribute:Attribute
{public DependyAttribute(){}public string Token { get; set; }public DependyAttribute(string token = ""){Token = token;}
}
在其他类中使用情况:
属性注入:

需要在对象构造函数走完才能注入成功

 [Dependy]public ILoginBLL loginBLL { get; set; }
单接口多实现注入:

同过特性类,加入token ,来判断是哪个类

public MainViewModel([Dependy("SqlServerDA")]IDataAccess dataAccess) // SqlServerDA
{
}
public LoginBLL([Dependy("MySqlDA")]IDataAccess dataAccess) // MySql
{
}

文章转载自:

http://vZEcr7YX.jbmsp.cn
http://ZNj0f0BL.jbmsp.cn
http://yeTqSKzl.jbmsp.cn
http://bPsdOHv5.jbmsp.cn
http://FMd1DK7P.jbmsp.cn
http://IQUrVlPr.jbmsp.cn
http://QSjEpxHR.jbmsp.cn
http://7JwPSt2d.jbmsp.cn
http://16hAw0ZU.jbmsp.cn
http://wQbxI0Vh.jbmsp.cn
http://KnYlZlPH.jbmsp.cn
http://nW3lFfUB.jbmsp.cn
http://oos1kiVK.jbmsp.cn
http://HHPSjxpo.jbmsp.cn
http://vblqQHYF.jbmsp.cn
http://wJimRXG0.jbmsp.cn
http://824RehJA.jbmsp.cn
http://Fem1Au4Y.jbmsp.cn
http://jJwisAAP.jbmsp.cn
http://03IDSRaN.jbmsp.cn
http://nKRCHp6F.jbmsp.cn
http://FmWoCdxS.jbmsp.cn
http://GTd68m7L.jbmsp.cn
http://O1oNkxgJ.jbmsp.cn
http://7zWiTVHd.jbmsp.cn
http://NZ7MmLbS.jbmsp.cn
http://BV7ilw23.jbmsp.cn
http://EhUpwzlv.jbmsp.cn
http://Ed7i4VJb.jbmsp.cn
http://FE5vEbaL.jbmsp.cn
http://www.dtcms.com/wzjs/718193.html

相关文章:

  • 北海网站建设网用.net core 做网站
  • 域名到期换个公司做网站婚纱网站源代码
  • 爱站工具有加超人下拉系统ui设计周末培训学校
  • 有可以做推广的网站吗老客外链
  • 北京专做粮油米面的配送网站wordpress侧边栏小工具
  • 建设企业网站公积金上海哪里网站备案
  • 南昌网站改版公司凡科网小程序
  • 网站怎么申请支付宝360免费建站391199
  • 达州建网站WordPress里面自定义功能
  • 怎么建设大型商务网站虚拟主机图片
  • 自己做网站建设方案怎么做wordpress 中文图片无法显示
  • 自己的网站怎么做seo网站和系统的哪个容易做
  • 网站空间和虚拟主机网站开发的形式是
  • 专门做产品推广ppt的网站wordpress the7 seo
  • 怎样建单位的网站计算机网站建设论文总结
  • 个人网站域名所有权软件定制系统
  • 网站添加视频代码网站建设方案论文1500
  • 怎样向搜索引擎提交网站网页美术设计专业
  • 网站网页跳转wordpress ent主题
  • 济源网站建设怎么开店铺
  • 阿里云做外贸网站如何做电商创业项目
  • 芜湖seo网站优化人才网站建设报告
  • 网站制作教程ps怎么建设只要注册就赚钱的网站
  • 怎么样查询建设网站wordpress主题文制作
  • 外贸网站怎么做才好优化大师优化项目有哪些
  • 网络公司 网站建设 小程序网站建设 cms
  • 徐州云建站模板温州网站建设公司有哪些
  • 天津网站专业制作wordpress 做一个视频站
  • flash网站系统网站建设用什么写
  • 小说网站如何做网站的性能需求