コード例 #1
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Singleton); // will register as the implemented interfaces
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve <IPrintable>();
            var instance2 = container.Resolve <IInvokable>();

            // Will get same instance
            // since both is implemented by the same class.
            instance1.Print(null);
            instance2.DoSomeWork();

            try
            {
                container.Resolve <Hello>();
            }
            catch (ServiceNotRegisteredException)
            {
                // throws exception as we've implemented
                // non .NET specific interfaces.
                //
                // since we should not depend on concretes.
                // (design choice in griffin.container)
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: GitTools/GitVersionV6
        private static async Task <int> Main(string[] args)
        {
            // TODO: load the list of assemblies from the app working directory, later we might load from nuget
            var assemblies = new[]
            {
                typeof(CommonModule).Assembly,
                typeof(NormalizeModule).Assembly,
                typeof(CalculateModule).Assembly,
                typeof(ConfigModule).Assembly,
                typeof(OutputModule).Assembly,
                typeof(CliModule).Assembly
            };

            var gitVersionModules = assemblies
                                    .SelectMany(a => a.GetTypes().Where(TypeIsGitVersionModule))
                                    .Select(t => (IGitVersionModule)Activator.CreateInstance(t) !)
                                    .ToList();

            using var serviceProvider = new ContainerRegistrar()
                                        .RegisterModules(gitVersionModules)
                                        .AddLogging(args)
                                        .Build();

            var app = serviceProvider.GetService <GitVersionApp>();

            var result = await app.RunAsync(args);

            if (!Console.IsInputRedirected)
            {
                Console.ReadKey();
            }

            return(result);
        }
コード例 #3
0
        public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore)
        {
            var builder = new ContainerRegistrar();

            //need to invoke first to allow plug-ins to override default behavior.
            action(builder);

            builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            builder.RegisterService(CreateUnitOfWork, Lifetime.Scoped);
            builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton);
            builder.RegisterService(CreateConnection, Lifetime.Transient);

            RegisterBuiltInComponents(builder);
            RegisterQueues(builder);

            builder.RegisterService(x => Container, Lifetime.Singleton);
            builder.RegisterService(x => x);
            builder.RegisterService(CreateAnalysisDbContext);
            builder.RegisterInstance(configStore);
            builder.RegisterType(typeof(IConfiguration <>), typeof(ConfigWrapper <>), Lifetime.Transient);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            builder.RegisterService(x => configStore.Load <BaseConfiguration>());
            var ioc = builder.Build();

            DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc));
            GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc);
            Container = new GriffinContainerAdapter(ioc);
        }
コード例 #4
0
        void IExtensionApplication.Initialize()
        {
            ContainerRegistrar.SetupContainer();

            this.logger = ContainerRegistrar.Container.Resolve <IWebTracker>();
            AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;

            Application.DocumentManager.MdiActiveDocument.CommandWillStart += logger.TrackCommandUsage;

            var uiGenerator = ContainerRegistrar.Container.Resolve <IUiGenerator>();

            try
            {
                uiGenerator.GenerateUi(false);
            }
            catch (Exception exception)
            {
                logger.TrackException(exception);
            }

            ContainerRegistrar.Container.Release(uiGenerator);

            IKojtoCadUpdater updater;

            try
            {
                updater = ContainerRegistrar.Container.Resolve <IKojtoCadUpdater>();
                updater.UpdateKojtoCad();
            }
            catch (Exception exception)
            {
                this.logger.TrackException(exception);
            }
        }
コード例 #5
0
ファイル: Config.cs プロジェクト: ObjectivityLtd/Bake
        private static void AddCakeCoreModules(ContainerBuilder containerBuilder)
        {
            var cakeRegistrar = new ContainerRegistrar(containerBuilder);

            cakeRegistrar.RegisterModule(new CoreModule());
            cakeRegistrar.RegisterModule(new CakeExtModule());
        }
コード例 #6
0
        public void Build(Action <ContainerRegistrar> action, ConfigurationStore configStore)
        {
            var builder = new ContainerRegistrar();

            builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            builder.RegisterService(CreateConnection, Lifetime.Scoped);
            builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton);
            builder.RegisterInstance(Startup.ConnectionFactory);
            action(builder);

            RegisterBuiltInComponents(builder);
            RegisterQueues(builder);

            builder.RegisterService(x => Container, Lifetime.Singleton);
            builder.RegisterService(x => x);
            builder.RegisterConcrete <AnalysisDbContext>();
            builder.RegisterInstance(configStore);
            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var ioc = builder.Build();

            DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc));
            GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc);
            Container = new GriffinContainerAdapter(ioc);
        }
コード例 #7
0
 private void RegisterBuiltInComponents(ContainerRegistrar builder)
 {
     builder.RegisterComponents(Lifetime.Scoped, typeof(AppType).Assembly);
     builder.RegisterComponents(Lifetime.Scoped, typeof(UserRepository).Assembly);
     builder.RegisterComponents(Lifetime.Scoped,
                                typeof(ReportAnalyzer.Handlers.Reports.ReportAnalyzer).Assembly);
 }
コード例 #8
0
ファイル: Program.cs プロジェクト: Greeley/Samples
 private static Container ConfigureGriffinContainer()
 {
     var registrar = new ContainerRegistrar(Lifetime.Scoped);
     registrar.RegisterComponents(Lifetime.Default, Assembly.GetExecutingAssembly());
     var container = registrar.Build();
     return container;
 }
コード例 #9
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Hello>(Lifetime.Transient); // <-- transient
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve <Hello>();

            instance1.World();

            // a new instance
            var instance2 = container.Resolve <Hello>();

            instance2.World();

            // every time = transient
            var instance3 = container.Resolve <Hello>();

            instance3.World();


            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #10
0
        public void Build(Action <ContainerRegistrar> action)
        {
            var builder = new ContainerRegistrar();

            builder.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            builder.RegisterService(CreateConnection, Lifetime.Scoped);
            builder.RegisterService(CreateTaskInvoker, Lifetime.Singleton);
            action(builder);

            builder.RegisterComponents(Lifetime.Scoped, typeof(ValidateNewLoginHandler).Assembly);
            builder.RegisterComponents(Lifetime.Scoped, typeof(UserRepository).Assembly);
            builder.RegisterComponents(Lifetime.Scoped, typeof(ScanForNewErrorReports).Assembly);
            builder.RegisterComponents(Lifetime.Scoped, typeof(QueueProvider).Assembly);

            builder.RegisterService <IContainer>(x => Container, Lifetime.Singleton);
            builder.RegisterService <IServiceLocator>(x => x);

            builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            builder.RegisterControllers(Assembly.GetExecutingAssembly());

            var ioc = builder.Build();

            DependencyResolver.SetResolver(new GriffinDependencyResolver(ioc));
            GlobalConfiguration.Configuration.DependencyResolver = new GriffinWebApiDependencyResolver2(ioc);
            Container = new GriffinContainerAdapter(ioc);
        }
コード例 #11
0
        public void TwoChildContainers()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <MySelf>();
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance1, instance2;
            var    childContainer1 = container.CreateChildContainer();
            var    childContainer2 = container.CreateChildContainer();

            instance1 = childContainer1.Resolve <MySelf>();
            instance2 = childContainer2.Resolve <MySelf>();
            Assert.False(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);

            childContainer1.Dispose();

            Assert.True(instance1.IsDisposed);
            Assert.False(instance2.IsDisposed);

            childContainer2.Dispose();

            Assert.True(instance2.IsDisposed);
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Singleton); // will register as the implemented interfaces
            var container = registrar.Build();

            // will get
            var instance1 = container.Resolve<IPrintable>();
            var instance2 = container.Resolve<IInvokable>();

            // Will get same instance
            // since both is implemented by the same class.
            instance1.Print(null);
            instance2.DoSomeWork();

            try
            {
                container.Resolve<Hello>();
            }
            catch(ServiceNotRegisteredException)
            {
                // throws exception as we've implemented
                // non .NET specific interfaces.
                //
                // since we should not depend on concretes.
                // (design choice in griffin.container)
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #13
0
        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException("serviceType");
            }

            ServiceBehaviorAttribute attribute;
            var lifetime = GetLifetime(serviceType, out attribute);

            if (_container == null)
            {
                var registrar = new ContainerRegistrar();
                registrar.RegisterConcrete(serviceType, lifetime);
                registrar.RegisterModules(serviceType.Assembly);
                _container = registrar.Build();
            }

            var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses);

            if (ConfigurationCallback != null)
            {
                ConfigurationCallback(host);
            }

            return(host);
        }
コード例 #14
0
        /// <summary>
        /// Builds the required services and an <see cref="ICakeHost" /> using the specified options.
        /// </summary>
        /// <returns>The built <see cref="ICakeHost" />.</returns>
        public ICakeHost Build()
        {
            try
            {
                // Create the "base" container with the minimum
                // stuff registered to run Cake at all.
                var registrar = new ContainerRegistrar();
                registrar.RegisterModule(new CoreModule());
                registrar.RegisterModule(new FrostingModule());
                var container = registrar.Build();

                // Add custom registrations to the container.
                AddCustomRegistrations(container);

                // Find and register tasks with the container.
                RegisterTasks(container);

                // Resolve the application and run it.
                return(container.Resolve <ICakeHost>());
            }
            catch (Exception exception)
            {
                return(new ErrorCakeHost(exception));
            }
        }
コード例 #15
0
        public override void PrepareBasic()
        {
            var registrar = new ContainerRegistrar();

            RegisterBasic(registrar);

            this.container = registrar.Build();
        }
コード例 #16
0
        public override void PrepareBasic()
        {
            var registrar = new ContainerRegistrar();

            RegisterBasic(registrar);

            this.container = registrar.Build();
        }
コード例 #17
0
 public void RegistrarRegistered(
     ContainerRegistrar registrar)
 {
     if (IsEnabled(EventLevel.Verbose, Keywords.vmAspects | Keywords.DI))
     {
         RegistrarRegistered(registrar.GetType().FullName);
     }
 }
コード例 #18
0
        /// <summary>
        /// The application entry point.
        /// </summary>
        /// <returns>The application exit code.</returns>
        public static int Main()
        {
            ICakeLog log = null;

            try
            {
                // Parse arguments.
                var args = QuoteAwareStringSplitter
                           .Split(Environment.CommandLine)
                           .Skip(1) // Skip executable.
                           .ToArray();

                var builder = new ContainerRegistrar();
                builder.RegisterModule(new CakeModule());
                builder.RegisterModule(new CoreModule());
                builder.RegisterModule(new CommonModule());

                // Build the container.
                using (var container = builder.Build())
                {
                    // Resolve the log.
                    log = container.Resolve <ICakeLog>();

                    // Parse the options.
                    var parser  = container.Resolve <IArgumentParser>();
                    var options = parser.Parse(args);

                    // Set verbosity.
                    log.Verbosity = options.Verbosity;

                    // Rebuild the container.
                    builder = new ContainerRegistrar();
                    var provider = container.Resolve <CakeConfigurationProvider>();
                    builder.RegisterModule(new ConfigurationModule(provider, options));
                    builder.RegisterModule(new ArgumentsModule(options));
                    builder.RegisterModule(new ScriptingModule(options, log));
                    builder.Update(container);

                    // Register the NuGetModule
                    builder = new ContainerRegistrar();
                    var configuration = container.Resolve <ICakeConfiguration>();
                    builder.RegisterModule(new NuGetModule(configuration));
                    builder.Update(container);

                    // Load all modules.
                    var loader = container.Resolve <ModuleLoader>();
                    loader.LoadModules(container, options);

                    // Resolve and run the application.
                    var application = container.Resolve <CakeApplication>();
                    return(application.Run(options));
                }
            }
            catch (Exception ex)
            {
                return(LogException(log, ex));
            }
        }
コード例 #19
0
        public void TestAttribute()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterUsingAttribute<ContainerServiceAttribute>(Assembly.GetExecutingAssembly());
            var container = registrar.Build();

            Assert.NotNull(container.Resolve<SomeService>());
        }
コード例 #20
0
ファイル: Program.cs プロジェクト: kimx/Samples
        private static Container ConfigureGriffinContainer()
        {
            var registrar = new ContainerRegistrar(Lifetime.Scoped);

            registrar.RegisterComponents(Lifetime.Default, Assembly.GetExecutingAssembly());
            var container = registrar.Build();

            return(container);
        }
コード例 #21
0
        public void ResolveFromModules()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterModules(Assembly.GetExecutingAssembly());
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            container.Resolve<MySelf>();
        }
コード例 #22
0
        public void ResolveFromComponents()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterComponents(Lifetime.Singleton, Assembly.GetExecutingAssembly());
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            container.Resolve<OneDepencency>();
        }
コード例 #23
0
        public void TestAttribute()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterUsingAttribute <ContainerServiceAttribute>(Assembly.GetExecutingAssembly());
            var container = registrar.Build();

            Assert.NotNull(container.Resolve <SomeService>());
        }
コード例 #24
0
        public void Test()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterConcrete <Service1>();

            var builder = new ContainerBuilder();

            Assert.Throws <ConcreteDependencyMissingException>(() => builder.Build(registrar));
        }
コード例 #25
0
        public void DispatchReal()
        {
            var handler = Substitute.For<IExecuteQuery<FakeQuery, string>>();
            var registrar = new ContainerRegistrar();
            registrar.RegisterInstance(handler);

            var dispatcher = new QueryDispatcher(registrar.Build());
            dispatcher.Execute(new FakeQuery());

        }
コード例 #26
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        private static Container CreateContainer()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());

            // yay, dispatch those queries.
            registrar.DispatchQueries();

            return registrar.Build();
        }
コード例 #27
0
        public void Test()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Service1>();

            var builder = new ContainerBuilder();
            Assert.Throws<ConcreteDependencyMissingException>(() =>builder.Build(registrar));


        }
コード例 #28
0
        public void TestDelegateFactory()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterService <MySelf>(ctnr => new MySelf(), Lifetime.Transient);
            registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve <OneDepencency>();
        }
コード例 #29
0
        public void ResolveFromModules()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterModules(Assembly.GetExecutingAssembly());
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            container.Resolve <MySelf>();
        }
コード例 #30
0
        public void ResolveFromComponents()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterComponents(Lifetime.Singleton, Assembly.GetExecutingAssembly());
            var builder   = new ContainerBuilder();
            var container = builder.Build(registrar);

            container.Resolve <OneDepencency>();
        }
コード例 #31
0
        public void TestSingleton()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterInstance <MySelf>(new MySelf());
            registrar.RegisterConcrete <OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve <OneDepencency>();
        }
コード例 #32
0
        public void DispatchReal()
        {
            var handler   = Substitute.For <IExecuteQuery <FakeQuery, string> >();
            var registrar = new ContainerRegistrar();

            registrar.RegisterInstance(handler);

            var dispatcher = new QueryDispatcher(registrar.Build());

            dispatcher.Execute(new FakeQuery());
        }
        public void NoRegisteredConcretes()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterConcrete<NoInstancesSubject>();

            var c = registrar.Build();
            var actual = c.Resolve<NoInstancesSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<NoInstancesSubject>(actual);
        }
コード例 #34
0
        private static void RegisterComplex(ContainerRegistrar registrar)
        {
            registrar.RegisterType<IFirstService, FirstService>(Lifetime.Singleton);
            registrar.RegisterType<ISecondService, SecondService>(Lifetime.Singleton);
            registrar.RegisterType<IThirdService, ThirdService>(Lifetime.Singleton);
            registrar.RegisterType<ISubObjectOne, SubObjectOne>(Lifetime.Transient);
            registrar.RegisterType<ISubObjectTwo, SubObjectTwo>(Lifetime.Transient);
            registrar.RegisterType<ISubObjectThree, SubObjectThree>(Lifetime.Transient);

            registrar.RegisterType<IComplex, Complex>(Lifetime.Transient);
        }
コード例 #35
0
        private static void RegisterComplex(ContainerRegistrar registrar)
        {
            registrar.RegisterType <IFirstService, FirstService>(Lifetime.Singleton);
            registrar.RegisterType <ISecondService, SecondService>(Lifetime.Singleton);
            registrar.RegisterType <IThirdService, ThirdService>(Lifetime.Singleton);
            registrar.RegisterType <ISubObjectOne, SubObjectOne>(Lifetime.Transient);
            registrar.RegisterType <ISubObjectTwo, SubObjectTwo>(Lifetime.Transient);
            registrar.RegisterType <ISubObjectThree, SubObjectThree>(Lifetime.Transient);

            registrar.RegisterType <IComplex, Complex>(Lifetime.Transient);
        }
コード例 #36
0
        public void should_Be_able_To_Register_a_service_using_a_factory_method()
        {
            var reg = new ContainerRegistrar();

            reg.RegisterService(CreateFirstService, Lifetime.Scoped);
            reg.RegisterService(CreateMyService, Lifetime.Singleton);

            var container = reg.Build();
            var actual    = container.Resolve <ISomeServiceInterface>();

            Assert.NotNull(actual);
        }
        public void RegisterServiceAsInstance()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterInstance<IThinkNot>(new Will());
            registrar.RegisterConcrete<InstanceSubject>();

            var c = registrar.Build();
            var actual = c.Resolve<InstanceSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<InstanceSubject>(actual);
        }
コード例 #38
0
 private static void RegisterStandard(ContainerRegistrar registrar)
 {
     registrar.RegisterType <ISingleton1, Singleton1>(Lifetime.Singleton);
     registrar.RegisterType <ISingleton2, Singleton2>(Lifetime.Singleton);
     registrar.RegisterType <ISingleton3, Singleton3>(Lifetime.Singleton);
     registrar.RegisterType <ITransient1, Transient1>(Lifetime.Transient);
     registrar.RegisterType <ITransient2, Transient2>(Lifetime.Transient);
     registrar.RegisterType <ITransient3, Transient3>(Lifetime.Transient);
     registrar.RegisterType <ICombined1, Combined1>(Lifetime.Transient);
     registrar.RegisterType <ICombined2, Combined2>(Lifetime.Transient);
     registrar.RegisterType <ICombined3, Combined3>(Lifetime.Transient);
 }
コード例 #39
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterComponents(Lifetime.Transient, Assembly.GetExecutingAssembly());
            registrar.RegisterService<ICommandDispatcher>(f => new ContainerDispatcher(f));
            var container = registrar.Build();

            var cmd = new CreateUser("arne", "King Arne");

            // the exception is thrown on purpose, read it.
            container.Resolve<ICommandDispatcher>().Dispatch(cmd);
        }
コード例 #40
0
        public void Should_be_able_To_resolve_open_generics_which_are_type_restricted()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(IRestrictedGeneric <>), typeof(RestrictedGeneric <>), Lifetime.Singleton);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <IRestrictedGeneric <SomeClass> >();

            Assert.NotNull(actual);
        }
コード例 #41
0
        public void NoRegisteredConcretes()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

            registrar.RegisterConcrete <NoInstancesSubject>();

            var c      = registrar.Build();
            var actual = c.Resolve <NoInstancesSubject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom <NoInstancesSubject>(actual);
        }
コード例 #42
0
        public void InterfaceToConcrete()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Transient);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <ISomeDude <string> >();

            Assert.NotNull(actual);
        }
コード例 #43
0
        public void Should_be_able_To_resolve_open_generics_in_singleton_scope()
        {
            var registrar = new ContainerRegistrar();

            registrar.RegisterType(typeof(ISomeDude <>), typeof(SomeDude <>), Lifetime.Singleton);
            registrar.RegisterConcrete <Word>(Lifetime.Transient);
            var container = registrar.Build();

            var actual = container.Resolve <ISomeDude <string> >();

            Assert.NotNull(actual);
        }
コード例 #44
0
        public void ResolveAllStartable()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Startable1>(Lifetime.Singleton);
            registrar.RegisterConcrete<Startable2>(Lifetime.Singleton);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instances = container.ResolveAll<ISingletonStartable>();

            Assert.Equal(2, instances.Count());
        }
コード例 #45
0
        public void SimpleRegistrationTransient()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>(Lifetime.Transient);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve<MySelf>();
            var instance2 = container.Resolve<MySelf>();

            Assert.NotSame(instance1, instance2);
        }
コード例 #46
0
 private static void RegisterDummies(ContainerRegistrar registrar)
 {
     registrar.RegisterType<IDummyOne, DummyOne>(Lifetime.Transient);
     registrar.RegisterType<IDummyTwo, DummyTwo>(Lifetime.Transient);
     registrar.RegisterType<IDummyThree, DummyThree>(Lifetime.Transient);
     registrar.RegisterType<IDummyFour, DummyFour>(Lifetime.Transient);
     registrar.RegisterType<IDummyFive, DummyFive>(Lifetime.Transient);
     registrar.RegisterType<IDummySix, DummySix>(Lifetime.Transient);
     registrar.RegisterType<IDummySeven, DummySeven>(Lifetime.Transient);
     registrar.RegisterType<IDummyEight, DummyEight>(Lifetime.Transient);
     registrar.RegisterType<IDummyNine, DummyNine>(Lifetime.Transient);
     registrar.RegisterType<IDummyTen, DummyTen>(Lifetime.Transient);
 }
        public void Test()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);
            registrar.RegisterConcrete<Will>();
            registrar.RegisterConcrete<Wont>();
            registrar.RegisterConcrete<Subject>();

            var c = registrar.Build();
            var actual = c.Resolve<Subject>();

            Assert.NotNull(actual);
            Assert.IsAssignableFrom<Subject>(actual);
        }
コード例 #48
0
        public void OneDependencySingleton()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>(Lifetime.Transient);
            registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton);
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            var instance1 = container.Resolve<OneDepencency>();
            var instance2 = container.Resolve<OneDepencency>();

            Assert.Same(instance1, instance2);
        }
コード例 #49
0
        public void ChildContainer()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<MySelf>();
            var builder = new ContainerBuilder();
            var container = builder.Build(registrar);

            MySelf instance;
            using (var childContainer = container.CreateChildContainer())
            {
                instance = childContainer.Resolve<MySelf>();
            }

            Assert.True(instance.IsDisposed);
        }
コード例 #50
0
        private Container ConfigureGriffinContainer()
        {
            var registrar = new ContainerRegistrar();

            // Repositories
            registrar.RegisterConcrete<RaceMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<UserMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<ResultMemoryRepository>(Lifetime.Singleton);
            registrar.RegisterConcrete<ApplicationStateMemoryRepository>(Lifetime.Singleton);

            registrar.RegisterComponents(Lifetime.Default, typeof(RfidTagRegistrationHandler).Assembly);
            registrar.RegisterComponents(Lifetime.Singleton, typeof(DebugUserControlViewModel).Assembly);

            return registrar.Build();
        }
コード例 #51
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        private static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());
            registrar.RegisterService<ICommandDispatcher>(x => new ContainerDispatcher(x), Lifetime.Scoped);
            var container = registrar.Build();

            using (var scope = container.CreateChildContainer())
            {
                var createUser = new CreateUser("arne", "Arne Eriksson");
                scope.Resolve<ICommandDispatcher>().Dispatch(createUser);
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #52
0
        public override void Prepare()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterType<ISingleton, Singleton>(Lifetime.Singleton);
            registrar.RegisterType<ITransient, Transient>(Lifetime.Transient);
            registrar.RegisterType<ICombined, Combined>(Lifetime.Transient);

            this.container = registrar.Build();

            registrar = new ContainerRegistrar();
            registrar.RegisterType<ICalculator, Calculator>(Lifetime.Transient);

            var containerWithLoggingInterception = registrar.Build();
            containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator());
            this.containerWithLoggingInterception = containerWithLoggingInterception;
        }
コード例 #53
0
        public override void Prepare()
        {
            var registrar = new ContainerRegistrar();

            RegisterDummies(registrar);
            RegisterStandard(registrar);
            RegisterComplex(registrar);

            this.container = registrar.Build();

            registrar = new ContainerRegistrar();
            registrar.RegisterType<ICalculator, Calculator>(Lifetime.Transient);

            var containerWithLoggingInterception = registrar.Build();
            containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator());
            this.containerWithLoggingInterception = containerWithLoggingInterception;
        }
コード例 #54
0
        public void TestLoggingDecorator()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<TotalFailure>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var tmp = container.Resolve<TotalFailure>();
            Assert.Throws<InvalidOperationException>(() => tmp.Fail("Big!"));
            Assert.IsType<InvalidOperationException>(_exception);
        }
コード例 #55
0
        public void TestDecorateHiearchy()
        {
            // register services
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Decorated1>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated2>(Lifetime.Transient);
            registrar.RegisterConcrete<Decorated3>(Lifetime.Transient);
            var container = registrar.Build();

            // only log transient services
            var filter = new DelegateDecoratorFilter(ctx => ctx.Lifetime == Lifetime.Transient);
            var decorator = new ExceptionLoggerDecorator(this, filter);
            container.AddDecorator(decorator);

            // exception will be logged.
            var all = container.ResolveAll<IShouldBeDecorated>();
            Assert.True(all.All(x => x.GetType().Name.Contains("Proxy")));
        }
コード例 #56
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        private static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

            // working with a "db". Let's scope everything per default.
            registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());

            var container = registrar.Build();

            using (var scope = container.CreateChildContainer())
            {
                var storage = scope.Resolve<IUserStorage>();
                storage.Create("Arne");
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #57
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Transient);
            registrar.RegisterConcrete<OurDependency>(Lifetime.Singleton);
            var container = registrar.Build();

            // gets a new OurDependency
            var obj1 = container.Resolve<Hello>();
            obj1.World();

            // New Hello instance, but with the same OurDependency.
            var obj2 = container.Resolve<Hello>();
            obj2.World();

            Console.WriteLine();
            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #58
0
        public override void Prepare()
        {
            var registrar = new ContainerRegistrar();

            RegisterBasic(registrar);
            
            RegisterPropertyInjection(registrar);

            this.container = registrar.Build();

            registrar = new ContainerRegistrar();
            registrar.RegisterType<ICalculator1, Calculator1>(Lifetime.Transient);
            registrar.RegisterType<ICalculator2, Calculator2>(Lifetime.Transient);
            registrar.RegisterType<ICalculator3, Calculator3>(Lifetime.Transient);

            var containerWithLoggingInterception = registrar.Build();
            containerWithLoggingInterception.AddDecorator(new GriffinLoggingDecorator());
            this.containerWithLoggingInterception = containerWithLoggingInterception;
        }
コード例 #59
0
ファイル: Program.cs プロジェクト: Greeley/Samples
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Singleton); //<-- singleton
            var container = registrar.Build();

            // Will get
            var instance1 = container.Resolve<Hello>();
            instance1.World();

            // the same instance
            var instance2 = container.Resolve<Hello>();
            instance2.World();

            // every time = singleton
            var instance3 = container.Resolve<Hello>();
            instance3.World();

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
コード例 #60
0
        /// <summary>
        /// Creates a <see cref="T:System.ServiceModel.ServiceHost"/> for a specified type of service with a specific base address.
        /// </summary>
        /// <param name="serviceType">Specifies the type of service to host.</param>
        /// <param name="baseAddresses">The <see cref="T:System.Array"/> of type <see cref="T:System.Uri"/> that contains the base addresses for the service hosted.</param>
        /// <returns>
        /// A <see cref="T:System.ServiceModel.ServiceHost"/> for the type of service specified with a specific base address.
        /// </returns>
        protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
        {
            if (serviceType == null) throw new ArgumentNullException("serviceType");

            ServiceBehaviorAttribute attribute;
            var lifetime = GetLifetime(serviceType, out attribute);

            if (_container == null)
            {
                var registrar = new ContainerRegistrar();
                registrar.RegisterConcrete(serviceType, lifetime);
                registrar.RegisterModules(serviceType.Assembly);
                _container = registrar.Build();
            }

            var host = new ServiceLocatorServiceHost(_container, serviceType, baseAddresses);
            if (ConfigurationCallback != null)
                ConfigurationCallback(host);

            return host;
        }