/// <overloads> /// <summary> /// Registers a service. /// </summary> /// </overloads> /// /// <summary> /// Registers a service using a custom factory method and certain creation and disposal /// policies. /// </summary> /// <param name="serviceType">The type of the service.</param> /// <param name="key"> /// The name under which the object should be registered. Can be <see langword="null"/> or /// empty. /// </param> /// <param name="createInstance"> /// The factory method responsible for serving the requests from the container. /// </param> /// <param name="creationPolicy"> /// The creation policy that specifies when and how a service will be instantiated. /// </param> /// <param name="disposalPolicy"> /// The disposal policy that specifies when a service instance will be disposed. (Only /// relevant if the service instance implements <see cref="IDisposable"/>.) /// </param> /// <remarks> /// If a service with the same type and name is already registered, the existing entry will /// be replaced. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="serviceType"/> or <paramref name="createInstance"/> is /// <see langword="null"/>. /// </exception> public void Register(Type serviceType, string key, Func<ServiceContainer, object> createInstance, CreationPolicy creationPolicy, DisposalPolicy disposalPolicy) { ThrowIfDisposed(); if (serviceType == null) throw new ArgumentNullException(nameof(serviceType)); if (createInstance == null) throw new ArgumentNullException(nameof(createInstance)); var registration = new ServiceRegistration(serviceType, key); var entry = new ServiceEntry(createInstance, creationPolicy, disposalPolicy); lock (_registry) { _registry[registration] = entry; } }
/// <summary> /// Initializes a new instance of the <see cref="ServiceEntry"/> class by copying the settings /// of an existing service entry. /// </summary> /// <param name="entry">The <see cref="ServiceEntry"/> from which to copy the settings.</param> public ServiceEntry(ServiceEntry entry) { CreateInstance = entry.CreateInstance; CreationPolicy = entry.CreationPolicy; DisposalPolicy = entry.DisposalPolicy; }
/// <summary> /// Registers the specified service type using a certain creation and disposal policy. /// </summary> /// <param name="serviceType">The type of the service.</param> /// <param name="key"> /// The name under which the object should be registered. Can be <see langword="null"/> or /// empty. /// </param> /// <param name="instanceType">The type implementing the service.</param> /// <param name="creationPolicy"> /// The creation policy that specifies when and how a service will be instantiated. /// </param> /// <param name="disposalPolicy"> /// The disposal policy that specifies when a service instance will be disposed. (Only /// relevant if the service instance implements <see cref="IDisposable"/>.) /// </param> /// <remarks> /// If a service with the same type and name is already registered, the existing entry will /// be replaced. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="serviceType"/> or <paramref name="instanceType"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// The <paramref name="instanceType"/> is not compatible with the /// <paramref name="serviceType"/>. /// </exception> public void Register(Type serviceType, string key, Type instanceType, CreationPolicy creationPolicy, DisposalPolicy disposalPolicy) { if (serviceType == null) throw new ArgumentNullException(nameof(serviceType)); if (instanceType == null) throw new ArgumentNullException(nameof(instanceType)); if (!serviceType.IsAssignableFrom(instanceType)) #else if (!serviceType.GetTypeInfo().IsAssignableFrom(instanceType.GetTypeInfo())) { string message = string.Format( CultureInfo.InvariantCulture, "The instance type (\"{0}\") does not implement this service type (\"{1}\").", instanceType.Name, serviceType.Name); throw new ArgumentException(message, nameof(serviceType)); } Register(serviceType, key, container => container.CreateInstance(instanceType), creationPolicy, disposalPolicy); }
public object Instances; // object/WeakReference/WeakCollection<IDisposable> /// <summary> /// Initializes a new instance of the <see cref="ServiceEntry"/> class. /// </summary> /// <param name="createInstance">The factory method.</param> /// <param name="creationPolicy">The creation policy.</param> /// <param name="disposalPolicy">The disposal policy.</param> public ServiceEntry(Func <ServiceContainer, object> createInstance, CreationPolicy creationPolicy, DisposalPolicy disposalPolicy) { CreateInstance = createInstance; CreationPolicy = creationPolicy; DisposalPolicy = disposalPolicy; }
/// <summary> /// Registers the specified service type using a certain creation and disposal policy. /// </summary> /// <param name="serviceType">The type of the service.</param> /// <param name="key"> /// The name under which the object should be registered. Can be <see langword="null"/> or /// empty. /// </param> /// <param name="instanceType">The type implementing the service.</param> /// <param name="creationPolicy"> /// The creation policy that specifies when and how a service will be instantiated. /// </param> /// <param name="disposalPolicy"> /// The disposal policy that specifies when a service instance will be disposed. (Only /// relevant if the service instance implements <see cref="IDisposable"/>.) /// </param> /// <remarks> /// If a service with the same type and name is already registered, the existing entry will /// be replaced. /// </remarks> /// <exception cref="ArgumentNullException"> /// <paramref name="serviceType"/> or <paramref name="instanceType"/> is /// <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// The <paramref name="instanceType"/> is not compatible with the /// <paramref name="serviceType"/>. /// </exception> public void Register(Type serviceType, string key, Type instanceType, CreationPolicy creationPolicy, DisposalPolicy disposalPolicy) { if (serviceType == null) throw new ArgumentNullException(nameof(serviceType)); if (instanceType == null) throw new ArgumentNullException(nameof(instanceType)); #if !NETFX_CORE && !NET45 if (!serviceType.IsAssignableFrom(instanceType)) #else if (!serviceType.GetTypeInfo().IsAssignableFrom(instanceType.GetTypeInfo())) #endif { string message = string.Format( CultureInfo.InvariantCulture, "The instance type (\"{0}\") does not implement this service type (\"{1}\").", instanceType.Name, serviceType.Name); throw new ArgumentException(message, nameof(serviceType)); } Register(serviceType, key, container => container.CreateInstance(instanceType), creationPolicy, disposalPolicy); }
public object Instances; // object/WeakReference/WeakCollection<IDisposable> #endregion Fields #region Constructors /// <summary> /// Initializes a new instance of the <see cref="ServiceEntry"/> class. /// </summary> /// <param name="createInstance">The factory method.</param> /// <param name="creationPolicy">The creation policy.</param> /// <param name="disposalPolicy">The disposal policy.</param> public ServiceEntry(Func<ServiceContainer, object> createInstance, CreationPolicy creationPolicy, DisposalPolicy disposalPolicy) { CreateInstance = createInstance; CreationPolicy = creationPolicy; DisposalPolicy = disposalPolicy; }