Ejemplo n.º 1
0
        public void ResolveServicesWithSameUniqueDependency()
        {
            var container = new Container();

            container.Configure(x =>
            {
                var unique = new UniquePerRequestLifecycle();
                x.For <IIngredient>().LifecycleIs(unique)
                .Use <Shrimp>();
                x.For <OliveOil>().LifecycleIs(unique);
                x.For <EggYolk>().LifecycleIs(unique);
                x.For <Vinegar>().LifecycleIs(unique);
                x.For <IIngredient>().LifecycleIs(unique)
                .Use <Vinaigrette>();
                x.For <IIngredient>().LifecycleIs(unique)
                .Use <Mayonnaise>();
                x.For <Course>().LifecycleIs(unique);
            });

            var c1 = container.GetInstance <Course>();
            var c2 = container.GetInstance <Course>();

            Assert.NotSame(
                c1.Ingredients.OfType <Vinaigrette>().Single().Oil,
                c1.Ingredients.OfType <Mayonnaise>().Single().Oil);
            Assert.NotSame(
                c2.Ingredients.OfType <Vinaigrette>().Single().Oil,
                c2.Ingredients.OfType <Mayonnaise>().Single().Oil);
            Assert.NotSame(
                c1.Ingredients.OfType <Vinaigrette>().Single().Oil,
                c2.Ingredients.OfType <Vinaigrette>().Single().Oil);
        }
Ejemplo n.º 2
0
 private static void SetLifecycleForImplementationsOfInterface(
     this Registry registry,
     Type genericType,
     UniquePerRequestLifecycle lifecycle,
     Assembly[] assembliesToScan)
 {
     assembliesToScan
     .Distinct()
     .SelectMany(t => t.ExportedTypes)
     .Where(genericType.IsAssignableFrom)
     .ToList()
     .ForEach(t => registry.For(t).LifecycleIs(lifecycle));
 }
Ejemplo n.º 3
0
 public static void SetLifecycleForImplementationsOfInterface(
     this Registry registry,
     Type genericType,
     UniquePerRequestLifecycle lifecycle,
     Type[] scanAssembliesContainingTypes)
 {
     scanAssembliesContainingTypes
     .Select(t => t.Assembly)
     .Distinct()
     .SelectMany(t => t.ExportedTypes)
     .Where(genericType.IsAssignableFrom)
     .ToList()
     .ForEach(t => registry.For(t).LifecycleIs(lifecycle));
 }
Ejemplo n.º 4
0
 public static IContainer Initialize()
 {
     ObjectFactory.Initialize(x =>
     {
         var unique = new UniquePerRequestLifecycle();
         x.Scan(scan =>
         {
             scan.TheCallingAssembly();
             scan.WithDefaultConventions();
         });
         x.For <IRestClient>()
         .Use <RestSharp.RestClient>()
         .SelectConstructor(() => new RestSharp.RestClient())
         .AlwaysUnique();
         x.For <SwiftBookingDbContext>().LifecycleIs(unique).Use <SwiftBookingDbContext>().Ctor <string>().Is("SwiftBookingDbContext");
     });
     return(ObjectFactory.Container);
 }