示例#1
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));
            }
        }
示例#2
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();
        }
示例#3
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);
        }
示例#4
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();
        }
        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);
        }
        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);
        }
示例#7
0
 private static Container ConfigureGriffinContainer()
 {
     var registrar = new ContainerRegistrar(Lifetime.Scoped);
     registrar.RegisterComponents(Lifetime.Default, Assembly.GetExecutingAssembly());
     var container = registrar.Build();
     return container;
 }
示例#8
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();
        }
        /// <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);
        }
        public override void PrepareBasic()
        {
            var registrar = new ContainerRegistrar();

            RegisterBasic(registrar);

            this.container = registrar.Build();
        }
示例#11
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));
            }
        }
        public void TestAttribute()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

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

            Assert.NotNull(container.Resolve <SomeService>());
        }
        public void TestAttribute()
        {
            var registrar = new ContainerRegistrar(Lifetime.Transient);

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

            Assert.NotNull(container.Resolve<SomeService>());
        }
示例#14
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);
        }
        public void TestSingleton()
        {
            var registrar = new ContainerRegistrar();

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

            container.Resolve <OneDepencency>();
        }
示例#16
0
        private static Container CreateContainer()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterComponents(Lifetime.Scoped, Assembly.GetExecutingAssembly());

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

            return registrar.Build();
        }
示例#17
0
        public void Register(Func <object> factory, Type serviceType, string contract = null)
        {
            if (!string.IsNullOrEmpty(contract))
            {
                throw new NotSupportedException();
            }

            _registrar.RegisterService(serviceType, (c) => factory(), Lifetime.Transient);
            _container = _registrar.Build();
        }
        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 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>();
        }
        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);
        }
示例#21
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());
        }
示例#22
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);
        }
        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);
        }
示例#24
0
        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);
        }
示例#25
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);
        }
        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);
        }
        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);
        }
示例#28
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);
        }
示例#29
0
        public void RegisteredSpecific()
        {
            var registrar = new ContainerRegistrar();

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

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

            Assert.IsType <MyClass>(actual);
        }
示例#30
0
        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);
        }
        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);
        }
示例#32
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();

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

            container.AddDecorator(new ConsoleLoggingDecorator());

            container.Resolve <SampleService>().DoSomething("Hello world");

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
示例#33
0
        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);
        }
            public void Should_Resolve_Different_Instances_When_Resolving_Type_Registered_As_Transient_Twice()
            {
                // Given
                var builder = new ContainerRegistrar();

                builder.RegisterType <Foo>().Transient();
                var container = builder.Build();

                // When
                var first  = container.Resolve <Foo>();
                var second = container.Resolve <Foo>();

                // Then
                Assert.NotSame(first, second);
            }
            public void Should_Resolve_The_Same_Instance_When_Resolving_Singleton_Twice()
            {
                // Given
                var builder = new ContainerRegistrar();

                builder.RegisterType <Foo>().Singleton();
                var container = builder.Build();

                // When
                var first  = container.Resolve <Foo>();
                var second = container.Resolve <Foo>();

                // Then
                Assert.Same(first, second);
            }
示例#36
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();
        }
            public void Should_Resolve_Last_Registration_When_Requesting_Single_Registration()
            {
                // Given
                var builder = new ContainerRegistrar();

                builder.RegisterType <Foo>().As <IFoo>();
                builder.RegisterType <Bar>().As <IFoo>();
                var container = builder.Build();

                // When
                var result = container.Resolve <IFoo>();

                // Then
                Assert.IsType <Bar>(result);
            }
示例#38
0
        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();
        }
        public void Should_be_able_To_take_an_specified_generic_as_a_dependency()
        {
            var t  = typeof(ISomeDude <>);
            var t2 = typeof(ISomeDude <string>);

            var registrar = new ContainerRegistrar();

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

            var actual = container.Resolve <SomeProcessor>();

            Assert.NotNull(actual);
        }
        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;
        }
    public static void Main(string[] args)
    {
        var registrar = new ContainerRegistrar();

        registrar.RegisterComponents(Lifetime.Transient, Environment.CurrentDirectory, "MyApp.Plugin.*.dll");
        var container = registrar.Build();

        // all extension points have been loaded. To load all menu extensions simply do something like:

        var menu = GetMainMenu();

        foreach (var registrar in container.ResolveAll <IMenuRegistrar>())
        {
            registrar.Register(menu);
        }
    }
        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);
        }
            public void Should_Resolve_The_Same_Instances_When_Resolving_Registered_Instance()
            {
                // Given
                var builder  = new ContainerRegistrar();
                var instance = Substitute.For <IFoo>();

                builder.RegisterInstance(instance).Singleton();
                var container = builder.Build();

                // When
                var first  = container.Resolve <IFoo>();
                var second = container.Resolve <IFoo>();

                // Then
                Assert.Same(instance, first);
                Assert.Same(instance, second);
            }
        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;
        }
        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")));
        }
示例#46
0
        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();
        }
示例#47
0
        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();
        }
        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;
        }
        /// <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;
        }
示例#50
0
        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();
        }
示例#51
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();
        }
示例#52
0
        static void Main(string[] args)
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterConcrete<Hello>(Lifetime.Scoped); //<-- scoped
            var container = registrar.Build();

            try
            {
                // May not resolve scoped objects
                // in the global container.
                var instance1 = container.Resolve<Hello>();
                instance1.World();
            }
            catch(InvalidOperationException)
            {
            }

            using (var scope = container.CreateChildContainer())
            {
                // notice that it get disposed when the scope gets disposed.
                var obj1 = scope.Resolve<Hello>();
                obj1.World();

                // will get same instance
                var obj2 = scope.Resolve<Hello>();
                obj2.World();
            }

            using (var scope = container.CreateChildContainer())
            {
                // Will get a new instance, since it's a new scope.
                var obj1 = scope.Resolve<Hello>();
                obj1.World();
            }

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
示例#53
0
        static void Main(string[] args)
        {
            // Used to configure which classes
            // the container should create
            var registrar = new ContainerRegistrar();

            // transient = new object every time
            registrar.RegisterConcrete<Hello>(Lifetime.Transient);

            // Build the actual container
            // nothing can be done with the registrar after this point.
            // (as changes whill have no effect unless you build a new container)
            var container = registrar.Build();

            // Ask the container after your class.
            var instance = container.Resolve<Hello>();

            // and invoke something on it
            instance.World();

            Console.WriteLine("Press enter to quit");
            Console.ReadLine();
        }
 public void Init()
 {
     _registrar = new ContainerRegistrar();
     _registrar.RegisterConcrete<UserMemoryRepository>(Lifetime.Singleton);
     _registrar.Build();
 }
        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>();
        }
        public void TestSingleton()
        {
            var registrar = new ContainerRegistrar();
            registrar.RegisterInstance<MySelf>(new MySelf());
            registrar.RegisterConcrete<OneDepencency>(Lifetime.Singleton);
            var container = registrar.Build();

            container.Resolve<OneDepencency>();
        }
        public override void PrepareBasic()
        {
            var registrar = new ContainerRegistrar();

            RegisterBasic(registrar);

            this.container = registrar.Build();
        }