.NET Unity IOC框架使用实例详解

所属分类: 网络编程 / ASP.NET 阅读数: 880
收藏 0 赞 0 分享

.NET Unity IOC框架的使用实例,具体内容如下

1.IOC简介

IOC(Inversion of Control), 控制反转

DI (Dependency Injection),依赖注入

IOC的基本概念是:不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。容器负责将这些联系在一起。

2.Unity引入

3.创建单例模式容器类

using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnityIocTest
{
 /// <summary>
 /// Unity IOC单例模式 
 /// </summary>
 public class UnitySingleton
 {
  //单例
  private static UnitySingleton instance;

  //ioc容器
  public IUnityContainer container;

  //获取单例
  public static UnitySingleton getInstance()
  {
   if (instance == null || instance.container == null)
   {
    string configFile = "Unity.config";
    var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
    //从config文件中读取配置信息
    Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    //获取指定名称的配置节
    UnityConfigurationSection section = (UnityConfigurationSection)configuration.GetSection("unity");
    instance = new UnitySingleton()
    {
     //container = new UnityContainer().LoadConfiguration((UnityConfigurationSection)ConfigurationManager.GetSection("unity"), "MyContainer")
     container = new UnityContainer().LoadConfiguration(section, "MyContainer")
     //container = new UnityContainer()
    };
    //instance.container.RegisterType<IExampleClass, ExampleClass>();
   }
   return instance;
  }

  //IOC注入实体
  public static T GetInstanceDAL<T>()
  {
   return getInstance().container.Resolve<T>();
  }
 }
}

此处是将Unity config配置单独成一个文件,方便管理。

4.添加IOC相关类

接口类

public interface IExampleClass
{
 void DoHelloWord();
}

 具体实现类

public class ExampleClass : IExampleClass
 {
  public void DoHelloWord()
  {
   Console.WriteLine("Hello Word!");
  }
 }

实现类扩展

public class DIExampleClass
 {
  //属性注入
  [Dependency]
  public IExampleClass example { get; set; }
  
  private IExampleClass testInject;

  public void DoWork()
  {
   example.DoHelloWord();
   testInject.DoHelloWord();
  }

  //方法注入
  [InjectionMethod]
  public void Initialize(IExampleClass instance)
  {
   testInject = instance;
  }
 }

5.添加配置文件Unity.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <configSections>
 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
 </configSections>
 <unity>
 <!--定义类型别名-->
 <aliases>
  <add alias="IExampleClass" type="UnityIocTest.IExampleClass,UnityIocTest" />
  <add alias="ExampleClass" type="UnityIocTest.ExampleClass,UnityIocTest" />
 </aliases>
 <!--容器-->
 <container name="MyContainer">
  <!--映射关系-->
  <!--<register type="IExampleClass" mapTo="ExampleClass"></register>-->
  <register type="UnityIocTest.IExampleClass,UnityIocTest" mapTo="UnityIocTest.ExampleClass,UnityIocTest"></register>
 </container>
 </unity>
</configuration>

6.函数调用

class Program
 {
  static void Main(string[] args)
  {
   //var dao = UnitySingleton.GetInstanceDAL<IExampleClass>();
   var dao = UnitySingleton.GetInstanceDAL<DIExampleClass>();
   dao.DoWork();
   Console.ReadLine();
  }
 }

7.结果显示

Hello Word!

Hello Word!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

.NET Core源码解析配置文件及依赖注入

这篇文章我们设计了一些复杂的概念,因为要对ASP.NET Core的启动及运行原理、配置文件的加载过程进行分析,依赖注入,控制反转等概念的讲解等
收藏 0 赞 0 分享

.NET Corek中Git的常用命令及实战演练

这篇文章将通过故事的形式从Git的历史谈起,并讲述Git的强大之处。然后通过实战演练教你如何在Github以及码云上托管我们的代码并进行代码的版本控制
收藏 0 赞 0 分享

Asp.Net Core WebAPI使用Swagger时API隐藏和分组详解

这篇文章主要给大家介绍了关于Asp.Net Core WebAPI使用Swagger时API隐藏和分组的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Asp.Net Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

如何利用FluentMigrator实现数据库迁移

这篇文章主要给大家介绍了关于如何利用FluentMigrator实现数据库迁移的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

ASP.NET Core利用Jaeger实现分布式追踪详解

这篇文章主要给大家介绍了关于ASP.NET Core利用Jaeger实现分布式追踪的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用ASP.NET Core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

浅谈从ASP.NET Core2.2到3.0你可能会遇到这些问题

这篇文章主要介绍了ASP.NET Core2.2到3.0可能会遇到的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解.net core webapi 前后端开发分离后的配置和部署

这篇文章主要介绍了.net core webapi 前后端开发分离后的配置和部署,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁

这篇文章主要介绍了ASP.Net Core 中如何借助CSRedis实现一个安全高效的分布式锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

.net 4.5部署到docker容器的完整步骤

这篇文章主要给大家介绍了关于.net 4.5部署到docker容器的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用.net4.5具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

.net core并发下线程安全问题详解

这篇文章主要给大家介绍了关于.net core并发下线程安全问题的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用.net core具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多