. net unity IOC framework use case details


. net unity IOC framework, as follows

1.IOC introduction

IOC (inversion of control)

Di (dependency injection)

The basic concept of IOC is: do not create objects, but describe how to create them. Do not connect directly to objects and services in code, but describe which component requires which service in the configuration file. The container is responsible for linking these together.

2. Introduction of unity

3. Create a singleton schema container class

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 singleton mode 
 /// </summary>
 public class UnitySingleton
  // single case
  private static UnitySingleton instance;

  //ioc container
  public IUnityContainer container;

  //Get a single example
  public static UnitySingleton getInstance()
   if (instance == null || instance.container == null)
    string configFile = "Unity.config";
    var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFile };
    //Read configuration information from config file
    Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    //Gets the configuration section with the specified name
    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 injection entity
  public static T GetInstanceDAL<T>()
   return getInstance().container.Resolve<T>();

This is to separate the unity config configuration into a file for easy management.

4. Add IOC related classes

Interface class

public interface IExampleClass
 void DoHelloWord();

Concrete implementation class

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

Implementation class extension

public class DIExampleClass
  //Attribute injection
  public IExampleClass example { get; set; }
  private IExampleClass testInject;

  public void DoWork()

  //Method injection
  public void Initialize(IExampleClass instance)
   testInject = instance;

5. Add configuration file unity.config

<?xml version="1.0" encoding="utf-8" ?>
 <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" />
 <! -- define type alias -- >
  <add alias="IExampleClass" type="UnityIocTest.IExampleClass,UnityIocTest" />
  <add alias="ExampleClass" type="UnityIocTest.ExampleClass,UnityIocTest" />
 <! -- container -- >
 <container name="MyContainer">
  <! -- mapping relationship -- >
  <!--<register type="IExampleClass" mapTo="ExampleClass"></register>-->
  <register type="UnityIocTest.IExampleClass,UnityIocTest" mapTo="UnityIocTest.ExampleClass,UnityIocTest"></register>

6. Function call

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

7. Result display

Hello Word!

Hello Word!

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.