コード例 #1
0
ファイル: Scope.cs プロジェクト: smcardle88/lamar
        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");
            }
        }
コード例 #2
0
ファイル: LoggerPolicy.cs プロジェクト: brokenbot/jasper
        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));
        }
コード例 #3
0
 public void ArrangeFrames(ServiceGraph services = null)
 {
     foreach (var method in _methods)
     {
         method.ArrangeFrames(this, services);
     }
 }
コード例 #4
0
        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;
        }
コード例 #6
0
ファイル: Scope.cs プロジェクト: tralivali1234/lamar
        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));
        }
コード例 #7
0
        public void SetUp()
        {
            var registry = new FubuRegistry();
            registry.Services<ResourcesServiceRegistry>();

            theServices = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #8
0
        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);
        }
コード例 #9
0
 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;
        }
コード例 #11
0
ファイル: Scope.cs プロジェクト: smcardle88/lamar
        public object TryGetInstance(Type serviceType, string name)
        {
            assertNotDisposed();
            var instance = ServiceGraph.FindInstance(serviceType, name);

            return(instance?.Resolve(this));
        }
コード例 #12
0
        protected override void beforeEach()
        {
            var registry = new FubuRegistry();

            ClassUnderTest.As <IFubuRegistryExtension>().Configure(registry);
            _services = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #13
0
        public void SetUp()
        {
            var registry = new FubuRegistry();

            registry.Services <ResourcesServiceRegistry>();

            theServices = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #14
0
        public void SetUp()
        {
            var registry = new FubuRegistry();

            registry.Services <JsonServiceRegistry>();

            services = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #15
0
        public void SetUp()
        {
            var registry = new FubuRegistry();

            registry.Services <OAuth2ServiceRegistry>();

            theServiceGraph = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #16
0
        public void SetUp()
        {
            var registry = new FubuRegistry();

            registry.Import <YuiCompressionExtensions>();

            theServices = BehaviorGraph.BuildFrom(registry).Services;
        }
コード例 #17
0
        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");
        }
コード例 #18
0
        public void SetUp()
        {
            FubuMode.RemoveTestingMode();
            var registry = new FubuRegistry();

            registry.Services <MonitoringServiceRegistry>();
            services = BehaviorGraph.BuildFrom(registry)
                       .Services;
        }
コード例 #19
0
ファイル: try_get_instance.cs プロジェクト: yuzd/lamar
        public ServiceFamily Build(Type type, ServiceGraph serviceGraph)
        {
            if (type != typeof(IFancy))
            {
                return(null);
            }

            return(new ServiceFamily(type, new IDecoratorPolicy[0], ConstructorInstance.For <IFancy, Very>()));
        }
コード例 #20
0
        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;
        }
コード例 #21
0
        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);
        }
コード例 #22
0
        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);
        }
コード例 #23
0
ファイル: ActivatingInstance.cs プロジェクト: JasperFx/lamar
        protected override IEnumerable <Instance> createPlan(ServiceGraph services)
        {
            _inner.CreatePlan(services);
            foreach (var message in _inner.ErrorMessages)
            {
                ErrorMessages.Add(message);
            }

            return(base.createPlan(services));
        }
コード例 #24
0
        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");
        }
コード例 #25
0
        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();
        }
コード例 #26
0
        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));
        }
コード例 #27
0
        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);
        }
コード例 #28
0
        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);
        }
コード例 #29
0
        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);
        }
コード例 #30
0
        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));
        }
コード例 #31
0
        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");
        }
コード例 #32
0
        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>();
        }
コード例 #33
0
ファイル: Scope.cs プロジェクト: smcardle88/lamar
        public object GetInstance(Type serviceType)
        {
            assertNotDisposed();
            var resolver = ServiceGraph.FindResolver(serviceType);

            if (resolver == null)
            {
                throw new LamarMissingRegistrationException(serviceType);
            }

            return(resolver(this));
        }
コード例 #34
0
        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");
        }
コード例 #35
0
        public void RunAction(ServiceGraph graph)
        {
            _registry.As<IServiceRegistration>().Apply(graph);

            graph.As<ITracedModel>().RecordEvents(AddEvent);
        }
コード例 #36
0
ファイル: ConfigGraph.cs プロジェクト: swcomp/fubumvc
 public void RegisterServices(ServiceGraph services)
 {
     AllServiceRegistrations().OfType<IServiceRegistration>().Each(x => x.Apply(services));
 }
コード例 #37
0
 public void SetUp()
 {
     theGraph = new ServiceGraph();
 }
コード例 #38
0
 public void SetUp()
 {
     theServices = BehaviorGraph.BuildEmptyGraph().Services;
 }
コード例 #39
0
 public void SetUp()
 {
     theServices = new FubuRegistry().BuildGraph().Services;
 }
コード例 #40
0
 public void Apply(ServiceGraph services)
 {
     _registry.As<IServiceRegistration>().Apply(services);
     services.As<ITracedModel>().RecordEvents(AddEvent);
 }