public Scope(IServiceCollection services, PerfTimer timer = null) { if (timer == null) { Bootstrapping = new PerfTimer(); Bootstrapping.Start("Bootstrapping Container"); } else { Bootstrapping = timer; Bootstrapping.MarkStart("Lamar Scope Creation"); } Root = this; Bootstrapping.MarkStart("Build ServiceGraph"); ServiceGraph = new ServiceGraph(services, this); Bootstrapping.MarkFinished("Build ServiceGraph"); ServiceGraph.Initialize(Bootstrapping); if (timer == null) { Bootstrapping.Stop(); } else { Bootstrapping.MarkFinished("Lamar Scope Creation"); } }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (!type.Closes(typeof(ILogger <>))) { return(null); } var inner = type.GetGenericArguments().Single(); var loggerType = typeof(Logger <>).MakeGenericType(inner); Instance instance = null; if (inner.IsPublic) { instance = new ConstructorInstance(type, loggerType, ServiceLifetime.Transient); } else { instance = new LambdaInstance(type, provider => { var factory = provider.GetService <ILoggerFactory>(); return(Activator.CreateInstance(loggerType, factory)); }, ServiceLifetime.Transient); } return(new ServiceFamily(type, instance)); }
public void ArrangeFrames(ServiceGraph services = null) { foreach (var method in _methods) { method.ArrangeFrames(this, services); } }
public void find_dependencies_deep() { var theServices = new ServiceRegistry(); theServices.AddTransient <IWidget, AWidget>(); theServices.AddTransient <Rule, BlueRule>(); theServices.AddTransient <OtherGuy>(); theServices.AddTransient <GuyWithWidgetAndRule>(); var theGraph = new ServiceGraph(theServices, Scope.Empty()); theGraph.Initialize(); var instance = ConstructorInstance.For <GuyWithGuys>(); instance.CreatePlan(theGraph); var expected = new[] { typeof(AWidget), typeof(BlueRule), typeof(OtherGuy), typeof(GuyWithWidgetAndRule) }; instance.Dependencies.OfType <ConstructorInstance>() .Select(x => x.ImplementationType) .ShouldBe(expected, true); }
public void SetUp() { var registry = new FubuRegistry(); registry.Import<YuiCompressionExtensions>(); theServices = BehaviorGraph.BuildFrom(registry).Services; }
public object QuickBuild(Type objectType) { assertNotDisposed(); if (!objectType.IsConcrete()) { throw new InvalidOperationException("Type must be concrete"); } var ctor = new ConstructorInstance(objectType, objectType, ServiceLifetime.Transient).DetermineConstructor(ServiceGraph, out var message); if (ctor == null) { throw new InvalidOperationException(message); } var dependencies = ctor.GetParameters().Select(x => { var instance = ServiceGraph.FindInstance(x); return(instance.QuickResolve(this)); }).ToArray(); return(ctor.Invoke(dependencies)); }
public void SetUp() { var registry = new FubuRegistry(); registry.Services<ResourcesServiceRegistry>(); theServices = BehaviorGraph.BuildFrom(registry).Services; }
internal void Export(ServiceGraph serviceGraph, GeneratedInstance[] instances, string path) { var system = new FileSystem(); if (system.DirectoryExists(path)) { system.CleanDirectory(path); } else { system.CreateDirectory(path); } var matching = instances.Where(x => canBePrebuilt(x) && Include(x) && !Exclude(x)).ToArray(); var typenames = new Dictionary <string, string>(); foreach (var instance in matching) { writeResolverCodeFile(serviceGraph, path, instance, system, typenames); } writeResolverLoaderClass(serviceGraph, path, typenames, system); }
public MethodFrameArranger(GeneratedMethod method, GeneratedType type, ServiceGraph services) : this(method, type) { if (services != null) { _services = new ServiceVariableSource(services); } }
public void SetUp() { var registry = new FubuRegistry(); registry.Services<ServerSentEventRegistry>(); services = BehaviorGraph.BuildFrom(registry).Services; }
public object TryGetInstance(Type serviceType, string name) { assertNotDisposed(); var instance = ServiceGraph.FindInstance(serviceType, name); return(instance?.Resolve(this)); }
protected override void beforeEach() { var registry = new FubuRegistry(); ClassUnderTest.As <IFubuRegistryExtension>().Configure(registry); _services = BehaviorGraph.BuildFrom(registry).Services; }
public void SetUp() { var registry = new FubuRegistry(); registry.Services <ResourcesServiceRegistry>(); theServices = BehaviorGraph.BuildFrom(registry).Services; }
public void SetUp() { var registry = new FubuRegistry(); registry.Services <JsonServiceRegistry>(); services = BehaviorGraph.BuildFrom(registry).Services; }
public void SetUp() { var registry = new FubuRegistry(); registry.Services <OAuth2ServiceRegistry>(); theServiceGraph = BehaviorGraph.BuildFrom(registry).Services; }
public void SetUp() { var registry = new FubuRegistry(); registry.Import <YuiCompressionExtensions>(); theServices = BehaviorGraph.BuildFrom(registry).Services; }
public void has_simple_argument() { var graph = ServiceGraph.For(x => x.AddTransient <IWidget, AWidget>()); var ctor = ConstructorInstance.For <WithSimples>().DetermineConstructor(graph, out var message); message.ShouldContain("* int number is a 'simple' type that cannot be auto-filled"); }
public void SetUp() { FubuMode.RemoveTestingMode(); var registry = new FubuRegistry(); registry.Services <MonitoringServiceRegistry>(); services = BehaviorGraph.BuildFrom(registry) .Services; }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (type != typeof(IFancy)) { return(null); } return(new ServiceFamily(type, new IDecoratorPolicy[0], ConstructorInstance.For <IFancy, Very>())); }
protected override IEnumerable<Instance> createPlan(ServiceGraph services) { _inner = services.FindInstance(ServiceType, _instanceKey); if (_inner == null) throw new InvalidOperationException($"Referenced instance of {ServiceType.FullNameInCode()} named '{_instanceKey}' does not exist"); _inner.Parent = Parent; Lifetime = _inner.Lifetime; yield return _inner; }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (type.IsConcrete() && matches(type)) { var instance = new ConstructorInstance(type, type, ServiceLifetime.Scoped); return(new ServiceFamily(type, new IDecoratorPolicy[0], instance)); } return(null); }
public void create_variable_should_be_through_constructor(Type concreteType, ServiceLifetime lifetime, BuildMode build, DisposeTracking disposal) { var instance = new ConstructorInstance(concreteType, concreteType, lifetime); instance.CreatePlan(ServiceGraph.Empty()); instance.CreateVariable(build, new ResolverVariables(), false).Creator .ShouldBeOfType <InstanceConstructorFrame>() .Disposal.ShouldBe(disposal); }
protected override IEnumerable <Instance> createPlan(ServiceGraph services) { _inner.CreatePlan(services); foreach (var message in _inner.ErrorMessages) { ErrorMessages.Add(message); } return(base.createPlan(services)); }
public void has_unknown_dependency() { var graph = ServiceGraph.For(x => x.AddTransient <IWidget, AWidget>()); var ctor = ConstructorInstance.For <WithHitsAndMisses>().DetermineConstructor(graph, out var message); message.ShouldContain("* int number is a 'simple' type that cannot be auto-filled"); message.ShouldContain( "* Rule is not registered within this container and cannot be auto discovered by any missing family policy"); }
public void do_not_pick_up_concrete_type_with_no_usable_ctor() { var graph = ServiceGraph.For(_ => { //_.AddTransient<IWidget, AWidget>(); //_.AddTransient<IClock, Clock>(); }); graph.TryToCreateMissingFamily(typeof(WidgetUser)) .Default.ShouldBeNull(); }
public void finds_the_single_default() { theServices.AddTransient <IWidget, AWidget>(); var theGraph = new ServiceGraph(theServices, Scope.Empty()); theGraph.Initialize(); theGraph.FindDefault(typeof(IWidget)) .ShouldBeOfType <ConstructorInstance>() .ImplementationType.ShouldBe(typeof(AWidget)); }
private void writeResolverCodeFile(ServiceGraph serviceGraph, string path, GeneratedInstance instance, FileSystem system, Dictionary <string, string> typenames) { var assembly = serviceGraph.ToGeneratedAssembly(Namespace); var(className, code) = instance.GenerateResolverClassCode(assembly); system.WriteStringToFile(Path.Combine(path, className + ".cs"), code); typenames.Add(className, Namespace + "." + className); }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (type.Closes(typeof(ILogger <>))) { var argType = type.GetGenericArguments().Single(); var instance = typeof(LoggerInstance <>).CloseAndBuildAs <Instance>(argType); return(new ServiceFamily(type, serviceGraph.DecoratorPolicies, instance)); } return(null); }
public ServiceFamily Build(Type type, ServiceGraph serviceGraph) { if (type == typeof(FakeThing)) { var @default = new ObjectInstance(type, new FakeThing { CreatedBy = "CustomMissingFamily" }); return(new ServiceFamily(type, serviceGraph.DecoratorPolicies, @default)); } return(null); }
public void can_pick_up_concrete_type_with_no_usable_ctor() { var graph = ServiceGraph.For(_ => { _.AddTransient <IWidget, AWidget>(); _.AddTransient <IClock, Clock>(); }); graph.TryToCreateMissingFamily(typeof(WidgetUser)) .Default.ShouldBeOfType <ConstructorInstance>() .ImplementationType.ShouldBe(typeof(WidgetUser)); }
public void use_custom_policy() { var graph = ServiceGraph.For(_ => { _.Policies.OnMissingFamily <CustomMissingFamily>(); }); graph.TryToCreateMissingFamily(typeof(FakeThing)) .Default.ShouldBeOfType <ObjectInstance>() .Service.ShouldBeOfType <FakeThing>() .CreatedBy.ShouldBe("CustomMissingFamily"); }
public void default_policies_in_empty_graph() { var graph = ServiceGraph.Empty(); graph.FamilyPolicies[0].ShouldBeOfType <EnumerablePolicy>(); graph.FamilyPolicies[1].ShouldBeOfType <FuncOrLazyPolicy>(); graph.FamilyPolicies[2].ShouldBeOfType <CloseGenericFamilyPolicy>(); graph.FamilyPolicies[3].ShouldBeOfType <ConcreteFamilyPolicy>(); graph.FamilyPolicies[4].ShouldBeOfType <EmptyFamilyPolicy>(); }
public object GetInstance(Type serviceType) { assertNotDisposed(); var resolver = ServiceGraph.FindResolver(serviceType); if (resolver == null) { throw new LamarMissingRegistrationException(serviceType); } return(resolver(this)); }
public void configure_is_idempotent() { var graph = new ServiceGraph(); graph.Configure<Something>(m => m.Message += "a"); graph.Configure<Something>(m => m.Message += "b"); graph.Configure<Something>(m => m.Message += "c"); graph.Configure<Something>(m => m.Message += "d"); graph.Configure<Something>(m => m.Message += "e"); graph.FindAllValues<Something>().Single().Message.ShouldEqual("abcde"); }
public void RunAction(ServiceGraph graph) { _registry.As<IServiceRegistration>().Apply(graph); graph.As<ITracedModel>().RecordEvents(AddEvent); }
public void RegisterServices(ServiceGraph services) { AllServiceRegistrations().OfType<IServiceRegistration>().Each(x => x.Apply(services)); }
public void SetUp() { theGraph = new ServiceGraph(); }
public void SetUp() { theServices = BehaviorGraph.BuildEmptyGraph().Services; }
public void SetUp() { theServices = new FubuRegistry().BuildGraph().Services; }
public void Apply(ServiceGraph services) { _registry.As<IServiceRegistration>().Apply(services); services.As<ITracedModel>().RecordEvents(AddEvent); }