Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- public static List<int> HashCodes = new List<int>();
- static void Main(string[] args)
- {
- RunTest<StandardKernel>(() =>
- {
- var kernel = new Ninject.StandardKernel();
- return () => kernel.Get<One>();
- });
- RunTest<UnityContainer>(() =>
- {
- var container = new UnityContainer();
- return () => container.Resolve<One>();
- });
- RunTest<DefaultKernel>(() =>
- {
- var kernel = new DefaultKernel();
- kernel.Register(Classes.FromThisAssembly()
- .Where(t => true)
- .WithService.DefaultInterfaces()
- .LifestyleTransient());
- return () => kernel.Resolve<One>();
- });
- RunTest<Container>(() =>
- {
- var container = new Container();
- return () => container.GetInstance<One>();
- });
- Console.ReadLine();
- }
- static void RunTest<T>(Func<Func<One>> config)
- {
- PrintType<T>();
- var resolve = config();
- HashCodes.Clear();
- resolve();
- if (ReferenceCount() == 1)
- {
- Console.WriteLine("References are same! Running Resolve again");
- resolve();
- if (ReferenceCount() == 1)
- Console.WriteLine("The container is faulty using Singleton scope");
- else
- {
- Console.WriteLine("The container is only true transient per call to Resolve");
- }
- }
- else
- Console.WriteLine("References are different");
- }
- private static int ReferenceCount()
- {
- return HashCodes.GroupBy(c => c).Count();
- }
- static void PrintType<T>()
- {
- Console.WriteLine(string.Format("{0}{1}", Environment.NewLine, typeof(T).FullName));
- }
- }
- public class One
- {
- public One(Three three, Two two)
- {
- Program.HashCodes.Add(three.GetHashCode());
- }
- }
- public class Two
- {
- public Two(Three three)
- {
- Program.HashCodes.Add(three.GetHashCode());
- }
- }
- public class Three
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment