private static void ResolveByNamWhenYouDontHaveAccessToTheContainer() { // add factory by type feature _container.AddFacility <TypedFactoryFacility>(); _container.Register(Component.For <NamedTypeFactorySelector>()); _container.Register(Component.For <IFactory>(). AsFactory(s => s.SelectedWith <NamedTypeFactorySelector>())); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA1>().LifestyleTransient().Named("action1")); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA2>().LifestyleTransient().Named("action2")); var factory = _container.Resolve <IFactory>(); IEngineA engineA1 = factory.Create("action1"); IEngineA engineA2 = factory.Create("action2"); _container.Register(Component.For <IService>().ImplementedBy <ServiceThatUsFactory>().LifestyleTransient()); _container.Register(Component.For <IStuff1>().ImplementedBy <Stuff1>().LifestyleTransient()); _container.Register(Component.For <IStuff2>().ImplementedBy <Stuff2>().LifestyleTransient()); _container.Register(Component.For <IStuff3>().ImplementedBy <Stuff3>().LifestyleTransient()); _container.Register(Component.For <IRepository1>().ImplementedBy <Repository1>().LifestyleTransient()); _container.Register(Component.For <IRepository2>().ImplementedBy <Repository2>().LifestyleTransient()); IService service = _container.Resolve <IService>(); while (true) { Console.WriteLine("insert action:"); var action = Console.ReadLine(); service.Execute(action); } }
private static void SimpleRegistrationWithCode() { _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA1>()); IEngineA engine = _container.Resolve <IEngineA>(); engine.DoSomething(); }
private static void RegistrationWithName() { _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA1>().LifestyleTransient().Named("action1")); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA2>().LifestyleTransient().Named("action2")); IEngineA engineA1 = _container.Resolve <IEngineA>("action1"); IEngineA engineA2 = _container.Resolve <IEngineA>("action2"); }
private static void DecoratorPatterWithCastle() { _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA1>()); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA2>()); // try to add IEngineA as dependency for EngineA1 IEngineA engine = _container.Resolve <IEngineA>(); }
private static void TransientLifeStyleIsSafer() { _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA3WithState>().LifestyleTransient()); IEngineA engine1 = _container.Resolve <IEngineA>(); IEngineA engine2 = _container.Resolve <IEngineA>(); IEngineA engine3 = _container.Resolve <IEngineA>(); engine1.DoSomething(); engine2.DoSomething(); engine3.DoSomething(); }
private static void DefaultLifeStyleIsSingleton() { // default life style is singleton so you don't have to specify the life style in this case _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA3WithState>().LifestyleSingleton()); IEngineA engine1 = _container.Resolve <IEngineA>(); IEngineA engine2 = _container.Resolve <IEngineA>(); IEngineA engine3 = _container.Resolve <IEngineA>(); engine1.DoSomething(); engine2.DoSomething(); engine3.DoSomething(); }
private static void ThereIsAlsoPerWcfRequestLifeStyle() { // need to add the wcf lifestyle to the container _container.AddFacility <WcfFacility>(); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA3WithState>().LifestylePerWcfOperation()); IEngineA engine1 = _container.Resolve <IEngineA>(); IEngineA engine2 = _container.Resolve <IEngineA>(); IEngineA engine3 = _container.Resolve <IEngineA>(); engine1.DoSomething(); engine2.DoSomething(); engine3.DoSomething(); }
private static void RegistrationWithXML() { _container.Install(Configuration.FromXmlFile("CastleRegistrations.xml")); Console.WriteLine("***resolve one***"); IEngineA engine = _container.Resolve <IEngineA>(); engine.DoSomething(); Console.WriteLine("***resolve all***"); // resolve all give us all the registrations for that interface IEnumerable <IEngineA> engines = _container.ResolveAll <IEngineA>(); foreach (var e in engines) { e.DoSomething(); } }
private static void OrderOfRegistrationMatter() { _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA1>()); _container.Register(Component.For <IEngineA>().ImplementedBy <EngineA2>()); // first registration selected Console.WriteLine("***resolve one***"); IEngineA engine = _container.Resolve <IEngineA>(); engine.DoSomething(); Console.WriteLine("***resolve all***"); // resolve all give us all the registrations for that interface IEnumerable <IEngineA> engines = _container.ResolveAll <IEngineA>(); foreach (var e in engines) { e.DoSomething(); } }
private static void MissingRegistration() { IEngineA engine = _container.Resolve <IEngineA>(); engine.DoSomething(); }