0、Unity应用
 

Unity应用0、Unity应用 1
1 1、控制倒转IoC(Inversion of Control) 2
1.1 工厂模式 2
2 2、依赖注入DI(Dependency Injection) 4
2.1 BuildUp手动注入 4
3 1、基础使用步骤 4
3.1 1、建立容器 4
3.2 2、将接口与类的映射注册到容器中 4
3.2.1 RegisterType 4
3.2.2 RegisterInstance(默认单例) 5
3.3 3、从容器中解析出正确的对象 5
3.4 BuildUp 方法 5
4 5、LifetimeManager对象生命周期 5
4.5 1.ContainerControlledLifetimeManager 5
4.5 2.ExternallyControlledLifetimeManager 5
4.5 3.PerThreadLifetimeManager 6
5 6、嵌套容器 6
6 8、杜绝循环引用 6
6.1 Constructor Injection构造器的参数中互相引用 6
6.2 Constructor Injection生成的对象作为自身构造器的参数 7
6.3 method call injection生成的对象互相引用 7
6.4 property(setter) injection生成的对象互相引用 7
7 9、容器vs.工厂 8
7.1 several reasons to use containers 8
7.2 Factory实例 9
7.3 使用容器不使用工厂的理由 11
8 10、Unity vs. MEF 11

1 1、控制倒转IoC(Inversion of Control)
1.1 工厂模式

UtopiaObjectFactory
using System;
using  System.Reflection;
using  System.Configuration;
namespace  UtopiaObjectFactory
{
   /// <summary>
   ///将创建的对象放入缓存中,
   ///用的时候检查缓存是否存在对象,
   ///没用则创建对象
   /// </summary>
   public sealed class  DataAccess
   {
      public  DataAccess()
      { }
      #region  CreateObject
     
      /// <summary>
      /// 创建对象,不使用缓存
      ///<appSettings>
      ///   <add  key="AProduct "  value="strPath,strClassCacheKey)" />
      ///</appSettings>
      /// </summary>
      public  static  object  CreateObjectNoCache(string path,string  CacheKey)
        
         try
         {
           object  objType =  Assembly.Load(path).CreateInstance(CacheKey);
           return  objType;
         }
  catch//(System.Exception ex)
         {
           //string  str=ex.Message;// 记录错误日志
           return null;
              
        
      }
      /// <summary>
      /// 创建对象或从缓存获取,使用缓存
      /// </summary>
      public static  object  CreateObject(string  path,string  CacheKey)
           
         object objType  DataCache.GetCache(CacheKey);
    

相关文章:

  • 2021-12-20
  • 2021-12-28
  • 2021-04-04
  • 2022-01-18
  • 2021-07-22
  • 2021-11-28
猜你喜欢
  • 2021-08-09
  • 2021-04-16
  • 2021-07-09
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
相关资源
相似解决方案