Unity injection examples

Source: Internet
Author: User

Unity injection examples

1/* 2 * demonstrate Unity injection 3 **/4 using Microsoft. practices. unity; 5 using System; 6 7 namespace Unity. property. inject 8 {9 class Program 10 {11 public static IUnityContainer container; 12 13 static void Main (string [] args) 14 {15 container = new UnityContainer (); 16 17 // PropertyInject (); 18 // ConstructorInject (); 19 MethodInject (); 20 21 Console. readLine (); 22} 23 24 // <summary> 25 // structure injection 26/ // </Summary> 27 static void ConstructorInject () 28 {29 Console. writeLine ("constructor injection"); 30 container. registerType <ILog, ALog> (); 31 // container. registerType <ILog, BLog> (); 32 33 container. registerType <IPeople, Man> (); 34 // container. registerType <IPeople, Woman> (); 35 36 var people = container. resolve <IPeople> (); 37 people. print2 (); 38} 39 40 // <summary> 41 // property injection 42 /// </summary> 43 static void PropertyInject () 44 {45 Console. writeLine ("Property injection"); 46 // print 47 using different log components by using different ILog derived types of Register. // container. registerType <ILog, ALog> (); 49 container. registerType <ILog, BLog> (); 50 51 container. registerType <IPeople, Man> (); 52 // container. registerType <IPeople, Woman> (); 53 54 var people = container. resolve <IPeople> (); 55 people. print (); 56} 57 58 static void MethodInject () 59 {60 Console. wri TeLine ("function injection"); 61 // print 62 63 // container using different log components by using different ILog derived types of Register. registerType <ILog, ALog> (); 64 container. registerType <ILog, BLog> (); 65 66 container. registerType <IPeople, Man> (); 67 // container. registerType <IPeople, Woman> (); 68 69 var people = container. resolve <IPeople> (); 70 people. logger. write ("function injection" + people. logger! = Null? "Successful": "failed "); 71} 72} 73 74 public interface IPeople 75 {76 // <summary> 77 // 78 for property injection // </summary> 79 ILog Logger {get; set;} 80 81 // <summary> 82 // construct the injection with 83 // </summary> 84 ILog Logger2 {get; set ;} 85 86 String Name {get; set;} 87 88 String Sex {get; set ;} 89 90 // <summary> 91 // 92 for property injection // </summary> 93 void Print (); 94 95 // <summary> 96 // construct the injection with 97 /// </summary> 98 void Print2 (); 99 100 // <summary> 101 // 102 for function injection // </summary> 103 void Print3 (ILog logger); 104} 105 106 public class Man: IPeople107 {108 [Dependency] 109 public ILog Logger {get; set;} 110 public ILog Logger2 {get; set;} 111 112 public string Name {get; set ;} 113 114 public string Sex {get; set;} 115 116 public Man (ILog logger2) {this. logger2 = logger2;} 117 118 public void Print () 119 {120 Logger. write (base. toString (); 121} 122 123 public void Print2 () 124 {125 Logger. write (base. toString (); 126} 127 128 [InjectionMethod] 129 public void Print3 (ILog logger) 130 {131 this. logger = logger; 132} 133} 134 135 public class Woman: IPeople136 {137 138 [Dependency] 139 public ILog Logger {get; set;} 140 public ILog Logger2 {get; set;} 141 142 public string Name {get; set;} 143 144 public string Sex {get; set;} 145 146 public Woman (ILog logger2) {this. logger2 = logger2;} 147 148 public void Print () 149 {150 Logger. write (base. toString (); 151} 152 153 public void Print2 () 154 {155 Logger. write (base. toString (); 156} 157 158 [InjectionMethod] 159 public void Print3 (ILog logger) 160 {161 this. logger = logger; 162} 163} 164 165 166 public interface ILog167 {168 void Write (String msg); 169} 170 171 public class ALog: ILog172 {173 public void Write (String msg) {Console. writeLine ("[A] {0}", msg);} 174} 175 176 public class BLog: ILog177 {178 public void Write (String msg) {Console. writeLine ("[B] {0}", msg);} 179} 180}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.