Unity 笔记二
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace UnityTest { using System.Diagnostics; using System.Windows.Forms; using Microsoft.Practices.Unity.Configuration; using Microsoft.Practices.Unity; using System.Configuration; public interface IMessage { DateTime Time { get; set; } string Text { get; set; } string ToString(string split); } public class MyMessage:IMessage { public DateTime Time { get; set; } public string Text { get; set; } public MyMessage(string text,DateTime time) { Text = text; Time = time; } public MyMessage(string text):this(text,DateTime.Now){} public string ToString(string split ="\t") { return Time.ToString() + split + Text +split+ GetHashCode(); } } public interface ITrace { IMessage Message { get; set; } void Write(string message); } public class ConsoleTrace:ITrace { [Dependency] public IMessage Message { get; set; } public void Write(string message) { if (Message != null) { message = Message.ToString("\t"); } Trace.WriteLine(message); } } public class FormTrace:ITrace { public IMessage Message { get; set; } public void Write(string message) { if (Message != null) { message = Message.ToString("\t"); } Trace.WriteLine(message); } } public class Class1 { public void test1() { IUnityContainer container = new UnityContainer(); IUnityContainer childContainter = container.CreateChildContainer(); ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = "UnityTest.dll.config"; System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); UnityConfigurationSection section= (UnityConfigurationSection)config.GetSection("unity"); var defultContainter = config.AppSettings.Settings["DefultContainter"].Value; section.Configure(container, defultContainter); section.Configure(childContainter, "First"); //container.LoadConfiguration("Fourth"); //container.RegisterType<ITrace, CosoleTrace>(new ContainerControlledLifetimeManager()); //container.RegisterType<ITrace, FormTrace>(); var message = container.Resolve<IMessage>(); Trace.WriteLine(message.ToString("\t")); message.Text = "1111"; var trace = childContainter.Resolve<ITrace>(); trace.Message = message; trace.Write("fdsfds"); trace = childContainter.Resolve<ITrace>(); //trace.Message = message; trace.Write("fdsfds"); trace = childContainter.Resolve<ITrace>(); trace.Write("fdsfds"); //container.LoadConfiguration("Second"); //trace = container.Resolve<ITrace>(); //trace.Write("fdsfds"); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name ="unity" type ="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/> </configSections> <appSettings> <add key ="DefultContainter" value="ChildContainter"/> </appSettings> <unity xmlns ="http://schemas.microsoft.com/practices/2010/unity"> <namespace name="UnityTest"/> <assembly name="UnityTest"/> <container name="ChildContainter"> <register type="IMessage" mapTo="MyMessage"> <constructor> <param name="text" type="System.String" value="abc"/> </constructor> </register> </container> <container name="First"> <register type="ITrace" mapTo="ConsoleTrace"> <lifetime type="singleton"/> </register> </container> <container name="Second"> <register type="ITrace" mapTo="FormTrace"> <lifetime type="singleton"/> </register> </container> <container name="Third"> <register type="ITrace" mapTo="ConsoleTrace"> <lifetime type="perresolve"/> </register> </container> <container name="Fourth"> <register type="ITrace" mapTo="ConsoleTrace"> <lifetime type="perthread"/> </register> </container> </unity> </configuration>