Example #1
0
        public virtual void RegisterGeneric(Type sourceType, Type targetType, RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve)
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }


            Action <ContainerBuilder> registrationAction;

            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                registrationAction = builder => builder.RegisterGeneric(targetType).As(sourceType);
                break;

            case RegistrationLifetime.InstancePerApplication:
                registrationAction = builder => builder.RegisterGeneric(targetType).As(sourceType).SingleInstance();
                break;

            default:
                throw new NotSupportedException("This operation is not supported");
            }

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }
Example #2
0
        public virtual void RegisterType <TRegistered, TTo>(RegistrationLifetime registrationLifetime = RegistrationLifetime.InstancePerResolve) where TTo : TRegistered
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }

            Action <ContainerBuilder> registrationAction;

            switch (registrationLifetime)
            {
            case RegistrationLifetime.InstancePerResolve:
                registrationAction = builder => builder.RegisterType <TTo>().As <TRegistered>();
                break;

            case RegistrationLifetime.InstancePerApplication:
                registrationAction = builder => builder.RegisterType <TTo>().As <TRegistered>().SingleInstance();
                break;

            default:
                throw new NotSupportedException("This operation is not supported");
            }

            JsonPropertyConverterRepository.TryOverride <TRegistered, TTo>();
            JsonPropertiesConverterRepository.TryOverride <TRegistered, TTo>();

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }
Example #3
0
        public virtual void RegisterInstance(Type targetType, object value)
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }

            var registrationAction = new Action <ContainerBuilder>(builder => { builder.RegisterInstance(value).As(targetType).ExternallyOwned(); });

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }
Example #4
0
        public void RegisterDecorator <TDecorator, TDecorated>() where TDecorator : TDecorated
        {
            if (IsInitialized)
            {
                throw new InvalidOperationException("Cannot update container after it was already initialized");
            }

            var registrationAction = new Action <ContainerBuilder>(builder =>
            {
                builder.RegisterDecorator <TDecorator, TDecorated>();
            });

            registrationAction(_containerBuilder);
            RegistrationActions.Add(registrationAction);
        }