Created
August 11, 2013 14:02
-
-
Save tarasn/6205025 to your computer and use it in GitHub Desktop.
Castle Windsor registration examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Linq; | |
| using Castle.MicroKernel.Registration; | |
| using Castle.Windsor; | |
| namespace ConsoleApplication6 | |
| { | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| IWindsorContainer container = new WindsorContainer(); | |
| container.Register(Component.For<IKuku>().ImplementedBy<Kuku1>()); | |
| container.Register(Component.For<IKuku>().ImplementedBy<Kuku2>()); | |
| container.Register( | |
| Classes.FromThisAssembly() | |
| .BasedOn(typeof(IKuku<>)).WithService.Base() | |
| ); | |
| container.Register(Component.For<ISome>().ImplementedBy<Some>()); | |
| var kuku = container.Resolve<IKuku>(); | |
| Console.WriteLine(kuku.GetType()); | |
| var kukus = container.ResolveAll<IKuku>(); | |
| Console.WriteLine(kukus.Count()); | |
| var gKukus = container.Resolve(typeof(IKuku<int>)); | |
| Console.WriteLine(gKukus); | |
| gKukus = container.Resolve(typeof(IKuku<string>)); | |
| Console.WriteLine(gKukus); | |
| var some = container.Resolve<ISome>(); | |
| container.Register( | |
| Classes.FromThisAssembly() | |
| .BasedOn(typeof(IBase)).WithService.FromInterface() | |
| ); | |
| var c1 = container.Resolve<IConcrete1>(); | |
| var c2 = container.Resolve<IConcrete2>(); | |
| Console.ReadLine(); | |
| } | |
| } | |
| public class Concrete1:IConcrete1 | |
| { | |
| } | |
| public class Concrete2 : IConcrete2 | |
| { | |
| } | |
| public interface IBase | |
| { | |
| } | |
| public interface IConcrete1:IBase | |
| { | |
| } | |
| public interface IConcrete2 : IBase | |
| { | |
| } | |
| public interface ISome | |
| { | |
| } | |
| public class Some:ISome | |
| { | |
| IKuku<int> _k; | |
| public Some(IKuku<int> k) | |
| { | |
| _k = k; | |
| } | |
| } | |
| public interface IKuku | |
| { | |
| } | |
| public interface IKuku<T>:IKuku | |
| { | |
| } | |
| public class Kuku1:IKuku | |
| { | |
| } | |
| public class Kuku2 : IKuku | |
| { | |
| } | |
| public class Kuku3 : IKuku<int> | |
| { | |
| } | |
| public class Kuku4 : IKuku<string> | |
| { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment