Example #1
0
 /// <summary>
 /// Create a new instance of <see cref="RegisterEventArgs"/>.
 /// </summary>
 /// <param name="typeFrom">Type to map from.</param>
 /// <param name="typeTo">Type to map to.</param>
 /// <param name="name">Name for the registration.</param>
 /// <param name="lifetimeManager"><see cref="LifetimeManager"/> to manage instances.</param>
 public RegisterEventArgs(Type typeFrom, Type typeTo, string name, LifetimeManager lifetimeManager)
     : base(name)
 {
     this.typeFrom = typeFrom;
     this.typeTo = typeTo;
     this.lifetimeManager = lifetimeManager;
 }
 /// <summary>
 /// Create a <see cref="RegisterInstanceEventArgs"/> instance initialized with the given arguments.
 /// </summary>
 /// <param name="registeredType">Type of instance being registered.</param>
 /// <param name="instance">The instance object itself.</param>
 /// <param name="name">Name to register under, null if default registration.</param>
 /// <param name="lifetimeManager"><see cref="LifetimeManager"/> object that handles how
 /// the instance will be owned.</param>
 public RegisterInstanceEventArgs(Type registeredType, object instance, string name, LifetimeManager lifetimeManager)
     : base(name)
 {
     this.registeredType = registeredType;
     this.instance = instance;
     this.lifetimeManager = lifetimeManager;
 }
 private void SetLifetimeManager(Type lifetimeType, string name, LifetimeManager lifetimeManager)
 {
     if (lifetimeManager.InUse)
     {
         throw new InvalidOperationException(Resources.LifetimeManagerInUse);
     }
     if (lifetimeType.GetTypeInfo().IsGenericTypeDefinition)
     {
         LifetimeManagerFactory factory =
             new LifetimeManagerFactory(Context, lifetimeManager.GetType());
         Context.Policies.Set<ILifetimeFactoryPolicy>(factory,
             new NamedTypeBuildKey(lifetimeType, name));
     }
     else
     {
         lifetimeManager.InUse = true;
         Context.Policies.Set<ILifetimePolicy>(lifetimeManager,
             new NamedTypeBuildKey(lifetimeType, name));
         if (lifetimeManager is IDisposable)
         {
             Context.Lifetime.Add(lifetimeManager);
         }
     }
 }
Example #4
0
        /// <summary>
        /// RegisterType a type mapping with the container, where the created instances will use
        /// the given <see cref="LifetimeManager"/>.
        /// </summary>
        /// <param name="from"><see cref="Type"/> that will be requested.</param>
        /// <param name="to"><see cref="Type"/> that will actually be returned.</param>
        /// <param name="name">Name to use for registration, null if a default registration.</param>
        /// <param name="lifetimeManager">The <see cref="LifetimeManager"/> that controls the lifetime
        /// of the returned instance.</param>
        /// <param name="injectionMembers">Injection configuration objects.</param>
        /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
        public IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
        {
            Guard.ArgumentNotNull(to, "to");
            Guard.ArgumentNotNull(injectionMembers, "injectionMembers");

            if (string.IsNullOrEmpty(name))
            {
                name = null;
            }

            if (from != null && !from.GetTypeInfo().IsGenericType && !to.GetTypeInfo().IsGenericType)
            {
                Guard.TypeIsAssignable(from, to, "from");
            }

            Registering(this, new RegisterEventArgs(from, to, name, lifetimeManager));

            if (injectionMembers.Length > 0)
            {
                ClearExistingBuildPlan(to, name);
                foreach (var member in injectionMembers)
                {
                    member.AddPolicies(from, to, name, policies);
                }
            }
            return this;
        }
Example #5
0
 /// <summary>
 /// RegisterType an instance with the container.
 /// </summary>
 /// <remarks>
 /// <para>
 /// Instance registration is much like setting a type as a singleton, except that instead
 /// of the container creating the instance the first time it is requested, the user
 /// creates the instance ahead of type and adds that instance to the container.
 /// </para>
 /// </remarks>
 /// <param name="t">Type of instance to register (may be an implemented interface instead of the full type).</param>
 /// <param name="instance">Object to returned.</param>
 /// <param name="name">Name for registration.</param>
 /// <param name="lifetime">
 /// <para>If true, the container will take over the lifetime of the instance,
 /// calling Dispose on it (if it's <see cref="IDisposable"/>) when the container is Disposed.</para>
 /// <para>
 ///  If false, container will not maintain a strong reference to <paramref name="instance"/>. User is responsible
 /// for disposing instance, and for keeping the instance from being garbage collected.</para></param>
 /// <returns>The <see cref="UnityContainer"/> object that this method was called on (this in C#, Me in Visual Basic).</returns>
 public IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime)
 {
     Guard.ArgumentNotNull(instance, "instance");
     Guard.ArgumentNotNull(lifetime, "lifetime");
     Guard.InstanceIsAssignable(t, instance, "instance");
     RegisteringInstance(this,
                         new RegisterInstanceEventArgs(t,
                                                       instance,
                                                       name,
                                                       lifetime));
     return this;
 }
Example #6
0
        private static void RegisterSingleton(IUnityContainer _container, ServiceDescriptor serviceDescriptor,
            MethodInfo miRegisterInstanceOpen, bool isAggregateType, LifetimeManager lifetimeManager)
        {
            if (isAggregateType)
            {
                //todo: sometimes ImplementationType is not defined
                Type implementationType = typeof(string);
                if (serviceDescriptor.ImplementationType != null)
                {
                    implementationType = serviceDescriptor.ImplementationType;
                }
                else if (serviceDescriptor.ImplementationInstance != null)
                {
                    implementationType = serviceDescriptor.ImplementationInstance.GetType();
                }

                miRegisterInstanceOpen.
                    MakeGenericMethod(new Type[] {serviceDescriptor.ServiceType}).
                    Invoke(null,
                        new object[]
                        {
                            _container, implementationType.AssemblyQualifiedName,
                            serviceDescriptor.ImplementationInstance, lifetimeManager
                        });
            }
            else
            {
                _container.RegisterInstance(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationInstance,
                    lifetimeManager);
            }
        }
Example #7
0
 private static void RegisterImplementation(IUnityContainer _container, ServiceDescriptor serviceDescriptor,
     bool isAggregateType, LifetimeManager lifetimeManager)
 {
     if (isAggregateType)
     {
         _container.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType,
             serviceDescriptor.ImplementationType.AssemblyQualifiedName, lifetimeManager);
     }
     else
     {
         _container.RegisterType(serviceDescriptor.ServiceType, serviceDescriptor.ImplementationType,
             lifetimeManager);
     }
 }
Example #8
0
 private static void RegisterFactory(IUnityContainer _container, ServiceDescriptor serviceDescriptor,
     bool isAggregateType, LifetimeManager lifetimeManager)
 {
     if (isAggregateType)
     {
         _container.RegisterType
             (
                 serviceDescriptor.ServiceType,
                 serviceDescriptor.ImplementationType.AssemblyQualifiedName,
                 lifetimeManager,
                 new InjectionFactory
                     (
                     container =>
                     {
                         var serviceProvider = container.Resolve<IServiceProvider>();
                         var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
                         return instance;
                     }
                     )
             );
     }
     else
     {
         _container.RegisterType
             (
                 serviceDescriptor.ServiceType,
                 lifetimeManager,
                 new InjectionFactory
                     (
                     container =>
                     {
                         var serviceProvider = container.Resolve<IServiceProvider>();
                         var instance = serviceDescriptor.ImplementationFactory(serviceProvider);
                         return instance;
                     }
                     )
             );
     }
 }