Beispiel #1
0
 /// <summary>
 /// Registers an object.
 /// </summary>
 /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings) where TImplementation : TDependency
 {
     this.context.Register(new RegisterContext()
     {
         Dependency     = typeof(TDependency),
         Implementation = typeof(TImplementation),
         Settings       = settings
     });
     return(this);
 }
Beispiel #2
0
        /// <summary>
        /// Continues the process of registration.
        /// </summary>
        /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
        /// <param name="registrationResult">The registration result.</param>
        /// <param name="settings">The settings.</param>
        private void ContinueRegistration <TObject>(object registrationResult, IocRegisterSettings settings)
        {
            MethodInfo asMethod = this.GetMethod(registrationResult.GetType().GetTypeInfo(), "As", new Type[] { typeof(Type[]) }, cached: false);
            object     asResult = asMethod.Invoke(registrationResult, new object[] { new Type[] { typeof(TObject) } });

            if (settings.IsSingleton())
            {
                MethodInfo singletonInstanceMethod = this.GetMethod(asResult.GetType().GetTypeInfo(), "SingleInstance", new Type[0]);
                singletonInstanceMethod.Invoke(asResult, new object[0]);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Registers an object.
        /// </summary>
        /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings)
        {
            this.AssertNoResolver();
            // Invocation of containerBuilder.RegisterType(typeof(TDependency)).As(typeof(TImplementation));
            MethodInfo registerMethod     = this.GetMethod(this.autofacContainerExtensionsTypeInfo, "RegisterType", this.registerParameters);
            object     registrationResult = registerMethod.Invoke(null, new object[]
            {
                this.registrar,
                typeof(TImplementation)
            });

            this.ContinueRegistration <TDependency>(registrationResult, settings);
            return(this);
        }
Beispiel #4
0
        /// <summary>
        /// Registers an object.
        /// </summary>
        /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings)
        {
            // Invocation of kernel.Bind(typeof(TDependency)).To(typeof(TImplementation));
            object[] bindArguments = new object[] { new Type[] { typeof(TDependency) } };
            object   bindResult    = this.GetMethod(this.registrar, "Bind", bindArguments.Length).Invoke(this.registrar, bindArguments);

            object[] toArguments = new object[] { typeof(TImplementation) };
            object   toResult    = this.GetMethod(bindResult, "To", toArguments.Length).Invoke(bindResult, toArguments);

            if (settings.IsSingleton())
            {
                this.GetMethod(toResult, "InSingletonScope", 0).Invoke(toResult, new object[0]);
            }

            return(this);
        }
Beispiel #5
0
        /// <summary>
        /// Registers an object.
        /// </summary>
        /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
        /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings)
        {
            // Invocation of IUnityContainer RegisterSingleton(this IUnityContainer container, Type from, Type to, params InjectionMember[] injectionMembers);
            // and IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, params InjectionMember[] injectionMembers);
            MethodInfo method = settings.IsSingleton()
                                   ? this.GetMethod(this.unityContainerExtensionsTypeInfo, "RegisterSingleton", this.registerParameters, "RegisterSingletonForSimpleRegistration")
                                   : this.GetMethod(this.unityContainerExtensionsTypeInfo, "RegisterType", this.registerParameters, "RegisterTypeForSimpleRegistration");

            method.Invoke(null, new object[]
            {
                this.registrar,
                typeof(TDependency),
                typeof(TImplementation),
                null
            });
            return(this);
        }
Beispiel #6
0
        /// <summary>
        /// Registers a factory for an object.
        /// </summary>
        /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
        /// <param name="creationAction">The factory method.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings)
        {
            this.AssertNoResolver();
            // Invocation of containerBuilder.Register<TObject>(Func<..., TObject>).As(typeof(TObject))
            MethodInfo registerMethod = this.GetMethod(this.autofacContainerExtensionsTypeInfo, "Register", 2);

            registerMethod = registerMethod.MakeGenericMethod(typeof(TObject));
            object registrationResult = registerMethod.Invoke(null, new object[]
            {
                this.registrar,
                new Func <object, TObject>(originalContainer => creationAction(this))
            });

            this.ContinueRegistration <TObject>(registrationResult, settings);
            return(this);
        }
Beispiel #7
0
        /// <summary>
        /// Registers a factory for an object.
        /// </summary>
        /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
        /// <param name="builder">The factory method builder.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        private IIoCRegistrar InternalRegister <TObject>(ObjectFactoryMethodBuilder builder, IocRegisterSettings settings)
        {
            Func <IIoCResolver, object> factoryMethod = builder.Build();

            this.factories[typeof(TObject)] = settings.IsSingleton()
                                               ? new SingleObjectFactory(factoryMethod)
                                               : new ObjectFactory(factoryMethod);
            return(this);
        }
Beispiel #8
0
 /// <summary>
 /// Registers a factory for an object.
 /// </summary>
 /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
 /// <param name="creationAction">The factory method.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings)
 {
     return(this.InternalRegister <TObject>(new ObjectFactoryMethodBuilder().FactoryMethod <TObject>(creationAction), settings));
 }
Beispiel #9
0
 /// <summary>
 /// Registers an object.
 /// </summary>
 /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings) where TImplementation : TDependency
 {
     return(this.InternalRegister <TDependency>(new ObjectFactoryMethodBuilder().ImplementationType <TImplementation>(), settings));
 }
Beispiel #10
0
        /// <summary>
        /// Registers a factory for an object.
        /// </summary>
        /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
        /// <param name="creationAction">The factory method.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings)
        {
            // Invocation of kernel.Bind(typeof(TDependency)).ToMethod(context => creationAction(this));
            object[] bindArguments = new object[] { new Type[] { typeof(TObject) } };
            object   bindResult    = this.GetMethod(this.registrar, "Bind", bindArguments.Length).Invoke(this.registrar, bindArguments);

            object[] toMethodArguments = new object[] { new Func <object, TObject>((object originalContainer) => creationAction(this)) };
            object   toMethodResult    = this.GetMethod(bindResult, "ToMethod", toMethodArguments.Length).Invoke(bindResult, toMethodArguments);

            if (settings.IsSingleton())
            {
                this.GetMethod(toMethodResult, "InSingletonScope", 0).Invoke(toMethodResult, new object[0]);
            }

            return(this);
        }
Beispiel #11
0
 /// <summary>
 /// Registers a factory for an object.
 /// </summary>
 /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
 /// <param name="creationAction">The factory method.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public abstract IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings);
Beispiel #12
0
 /// <summary>
 /// Registers an object.
 /// </summary>
 /// <typeparam name="TDependency">The type of the registration object in IoC.</typeparam>
 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public abstract IIoCRegistrar Register <TDependency, TImplementation>(IocRegisterSettings settings) where TImplementation : TDependency;
Beispiel #13
0
        /// <summary>
        /// Registers a factory for an object.
        /// </summary>
        /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
        /// <param name="creationAction">The factory method.</param>
        /// <param name="settings">The settings.</param>
        /// <returns>The registrar.</returns>
        public override IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings)
        {
            // Invocation of IUnityContainer RegisterSingleton(this IUnityContainer container, Type t, string name, params InjectionMember[] injectionMembers);
            // and IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, params InjectionMember[] injectionMembers);
            MethodInfo method = settings.IsSingleton()
                                   ? this.GetMethod(this.unityContainerExtensionsTypeInfo, "RegisterSingleton", this.registerFactoryParameters, "RegisterSingletonForFactoryRegistration")
                                   : this.GetMethod(this.unityContainerExtensionsTypeInfo, "RegisterType", this.registerFactoryParameters, "RegisterTypeForFactoryRegistration");
            Array array = Array.CreateInstance(this.injectionMemberType, 1);

            array.SetValue(Activator.CreateInstance(this.injectionFactoryType, new object[] { new Func <object, object>(originalContainer => creationAction(this)) }), 0);
            method.Invoke(null, new object[]
            {
                this.registrar,
                typeof(TObject),
                null,
                array
            });
            return(this);
        }
Beispiel #14
0
 /// <summary>
 /// Checks if the settings contains the singleton setting.
 /// </summary>
 /// <param name="settings">The settings to check.</param>
 /// <returns><c>True</c> if the settings contains the singleton setting, otherwise <c>false</c>.</returns>
 public static bool IsSingleton(this IocRegisterSettings settings)
 {
     return((settings & IocRegisterSettings.Singleton) != 0);
 }
Beispiel #15
0
 /// <summary>
 /// Registers a factory for an object.
 /// </summary>
 /// <typeparam name="TObject">The type of the registration object in IoC.</typeparam>
 /// <param name="creationAction">The factory method.</param>
 /// <param name="settings">The settings.</param>
 /// <returns>The registrar.</returns>
 public IIoCRegistrar Register <TObject>(Func <IIoCResolver, TObject> creationAction, IocRegisterSettings settings)
 {
     this.context.RegisterFactory(new RegisterFactoryContext()
     {
         Type          = typeof(TObject),
         ObjectFactory = () => creationAction(this),
         Settings      = settings
     });
     return(this);
 }