public static void Register <TInterface, TImplementation>(this IContainer scope,
                                                           RegistrationLifestyle lifestyle = RegistrationLifestyle.Transient) where TImplementation : TInterface
 {
     scope.Registration(new[] { new RegistrationDefinition {
                                    ExportType = typeof(TInterface), ActivationType = typeof(TImplementation)
                                } });
 }
        public static IEnumerable <RegistrationDefinition> Definitions(RegistrationLifestyle lifestyle = RegistrationLifestyle.Transient, object lifestyleInfo = null)
        {
            yield return(new RegistrationDefinition {
                ExportType = typeof(ISingletonService), ActivationType = typeof(SingletonService), RegistrationLifestyle = RegistrationLifestyle.Singleton
            });

            yield return(new RegistrationDefinition {
                ExportType = typeof(ISmallObjectService), ActivationType = typeof(SmallObjectService), RegistrationLifestyle = lifestyle, LifestyleInformation = lifestyleInfo
            });

            yield return(new RegistrationDefinition {
                ExportType = typeof(ITransientService), ActivationType = typeof(TransientService)
            });
        }
Esempio n. 3
0
        private ILifecycle GetLifecycle(RegistrationLifestyle lifestyle)
        {
            ILifecycle lifecycle = null;

            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                lifecycle = new SingletonLifecycle();
                break;

            case RegistrationLifestyle.SingletonPerScope:
                lifecycle = new ContainerLifecycle();
                break;

            case RegistrationLifestyle.SingletonPerObjectGraph:
            case RegistrationLifestyle.Transient:
                lifecycle = new TransientLifecycle();
                break;
            }

            return(lifecycle);
        }
Esempio n. 4
0
        public void RegisterFactory <TResult>(Func <TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                _container.Register(Component.For <TResult>().UsingFactoryMethod(factory).LifeStyle.Singleton);
                break;

            case RegistrationLifestyle.SingletonPerScope:
                _container.Register(Component.For <TResult>().UsingFactoryMethod(factory)
                                    .LifeStyle.Scoped());
                break;

            case RegistrationLifestyle.Transient:
                _container.Register(Component.For <TResult>().UsingFactoryMethod(factory).LifeStyle.Transient);
                break;
            }
        }
        public void RegisterFactory <TResult>(Func <TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            var binding = _configuration.Bind <TResult>().ToMethod(context => factory());

            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                binding.InSingletonScope();
                break;

            case RegistrationLifestyle.Transient:
                binding.InTransientScope();
                break;

            default:
                throw new NotSupportedException();
            }
        }
        public void RegisterFactory <T1, T2, T3, TResult>(Func <T1, T2, T3, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            IReuse reuse = null;

            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                reuse = new SingletonReuse();
                break;

            case RegistrationLifestyle.SingletonPerScope:
                reuse = new CurrentScopeReuse();
                break;

            case RegistrationLifestyle.SingletonPerObjectGraph:
                throw new NotSupportedException("");
            }

            _container.RegisterDelegate(r => factory(r.Resolve <T1>(), r.Resolve <T2>(), r.Resolve <T3>()), reuse);
        }
Esempio n. 7
0
        public void RegisterFactory <T1, T2, T3, TResult>(Func <T1, T2, T3, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            _container.Configure(c =>
            {
                var export = c.ExportFactory(factory);

                switch (lifestyle)
                {
                case RegistrationLifestyle.Singleton:
                    export.Lifestyle.Singleton();
                    break;

                case RegistrationLifestyle.SingletonPerScope:
                    export.Lifestyle.SingletonPerScope();
                    break;

                case RegistrationLifestyle.SingletonPerObjectGraph:
                    export.Lifestyle.SingletonPerObjectGraph();
                    break;
                }
            });
        }
        public void RegisterFactory <T1, TResult>(Func <T1, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            switch (lifestyle)
            {
            case RegistrationLifestyle.Transient:
                _serviceCollection.AddTransient(provider => factory(provider.GetService <T1>()));
                break;

            case RegistrationLifestyle.Singleton:
                _serviceCollection.AddSingleton(provider => factory(provider.GetService <T1>()));
                break;

            case RegistrationLifestyle.SingletonPerScope:
                _serviceCollection.AddScoped(provider => factory(provider.GetService <T1>()));
                break;
            }
        }
        public void RegisterFactory <T1, T2, T3, TResult>(Func <T1, T2, T3, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            var registration = _container.Register <T1, T2, T3, TResult>((serviceFactory, arg1, arg2, arg3) => factory(arg1, arg2, arg3));

            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                registration.SetDefaultLifetime <PerContainerLifetime>();
                break;

            case RegistrationLifestyle.SingletonPerScope:
                registration.SetDefaultLifetime <PerScopeLifetime>();
                break;
            }
        }
Esempio n. 10
0
 public void RegisterFactory <T1, T2, T3, TResult>(Func <T1, T2, T3, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
 {
     // requires t1, t2, t3 to be a reference type where no other containers require this
     throw new NotSupportedException();
 }
Esempio n. 11
0
        public void RegisterFactory <TResult>(Func <TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                _container.RegisterSingleton(factory);
                break;

            case RegistrationLifestyle.Transient:
                _container.Register(factory);
                break;
            }
        }
Esempio n. 12
0
        public void RegisterFactory <T1, T2, T3, TResult>(Func <T1, T2, T3, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            var register = _builder.Register(c => factory(c.Resolve <T1>(), c.Resolve <T2>(), c.Resolve <T3>()));

            switch (lifestyle)
            {
            case RegistrationLifestyle.Singleton:
                register.SingleInstance();
                break;

            case RegistrationLifestyle.SingletonPerScope:
                register.InstancePerLifetimeScope();
                break;
            }
        }
Esempio n. 13
0
        public void RegisterFactory <T1, TResult>(Func <T1, TResult> factory, RegistrationMode mode, RegistrationLifestyle lifestyle) where TResult : class
        {
            var lifecycle = GetLifecycle(lifestyle);

            _container.Configure(r => r.For <TResult>(lifecycle).Use(context => factory(context.GetInstance <T1>())));
        }