Show pageOld revisionsBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. # IT:AD:Unity # <callout type="Navigation" class="small"> * [[../|(UP)]] {{indexmenu>.#2|nsort tsort}} * See also: * [[IT/AD/StructureMap/HowTo/Register Instances against Open Contracts]] * [[IT/AD/Unity3D/]] * http://www.palmmedia.de/blog/2011/8/30/ioc-container-benchmark-performance-comparison </callout> <panel title="Summary"> </panel> ## Process ## ### Preparation ### Use Unity (ie, *"Tools/Library Package Manager/Manage Packages..."* to search for and install `Unity` into your App. ## Examples ## Try the following Console Application to get a feel for dependency injection. //Register something using one of the lifetime managers: //* TransientLifetimeManager(DEFAULT) -- each time a new one //* ContainerControlledLifetimeManager -- singleton per container //* HierarchicalLifetimeManager -- same as Container, but each nested container gets its own //* PerResolveLifetimeManager - related to Transient (look it up) //* ExternallyControlledLifetimeManager - uses Weaklinks to externally killable resources //* PerThreadLifetimeManager - //unityContainer // .RegisterType<ISomeService, SomeService>(new ContainerControlledLifetimeManager()) // .RegisterType<ISomeService2, SomeService2>(new PerThreadLifetimeManager()); ////Get the service: //ISomeService someService = unityContainer.Resolve<ISomeService>(); ////Use it: //Console.WriteLine(someService.BePolite("Paul")); Example: using System; using Microsoft.Practices.Unity; namespace XAct.Studies.UnityDemo01 { class Program { static void Main(string[] args) { //Create a IoC Dictionary/Builder: IUnityContainer unityContainer = new UnityContainer(); //Register something using one of the lifetime managers: //* TransientLifetimeManager(DEFAULT) -- each time a new one //* ContainerControlledLifetimeManager -- singleton per container //* HierarchicalLifetimeManager -- same as Container, but each nested container gets its own //* PerResolveLifetimeManager - related to Transient (look it up) //* ExternallyControlledLifetimeManager - uses Weaklinks to externally killable resources //* PerThreadLifetimeManager - unityContainer .RegisterType<ISomeService, SomeService>(new ContainerControlledLifetimeManager()) .RegisterType<ISomeService2, SomeService2>(new PerThreadLifetimeManager()); //Get the service: ISomeService someService = unityContainer.Resolve<ISomeService>(); //Use it: Console.WriteLine(someService.BePolite("Paul")); } } //Define interfaces in another assembly (eg: App.Domain.Services.Contracts) interface ISomeService { string BePolite(string name); } interface ISomeService2 { } //Define instances in another assembly (eg: App.Domain.Services) public class SomeService : ISomeService { public string BePolite(string name) { return string.Format("Hello, {0}.", name); } } public class SomeService2 : ISomeService2{} } Registering it in ServiceLocator: UnityServiceLocator locator = new UnityServiceLocator(container); ServiceLocator.SetLocatorProvider(() => locator); var a = ServiceLocator.Current.GetInstance<IFoo>(); ## Resources ## * http://tanguay.info/web2010/index.php?pg=codeExamples&id=117 /home/skysigal/public_html/data/pages/it/ad/unity/home.txt Last modified: 2023/11/04 03:32by 127.0.0.1