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); }
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)); }
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)); }
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); }