Example #1
0
        protected ServiceConfigurationEntry CreateMultipleServiceConfigurationEntry(
            Type serviceType,
            Type[] implementationTypes,
            LifetimeKind lifetimeKind = LifetimeKind.InstancePerDependency)
        {
            var implementations = implementationTypes.Select(t => new ServiceImplementationInfo(t, lifetimeKind, RegistrationType.Multiple));

            return(new ServiceConfigurationEntry(serviceType, implementations));
        }
Example #2
0
        protected ServiceConfigurationEntry CreateSingleServiceConfigurationEntry(
            Type serviceType,
            Type implementationType,
            LifetimeKind lifetimeKind = LifetimeKind.InstancePerDependency)
        {
            var implementation = new ServiceImplementationInfo(implementationType, lifetimeKind, RegistrationType.Single);

            return(new ServiceConfigurationEntry(serviceType, implementation));
        }
        public void Register(Type serviceType, Type implementationType, LifetimeKind lifetime)
        {
            ArgumentUtility.CheckNotNull("serviceType", serviceType);
            ArgumentUtility.CheckNotNull("implementationType", implementationType);

            var entry = new ServiceConfigurationEntry(serviceType, new ServiceImplementationInfo(implementationType, lifetime));

            Register(entry);
        }
Example #4
0
        protected ServiceConfigurationEntry CreateDecoratorServiceConfigurationEntry(
            Type serviceType,
            Type[] decoratorTypes,
            Type implementationType,
            LifetimeKind lifetimeKind = LifetimeKind.InstancePerDependency)
        {
            var implementations = decoratorTypes.Select(t => new ServiceImplementationInfo(t, lifetimeKind, RegistrationType.Decorator))
                                  .Concat(new[] { new ServiceImplementationInfo(implementationType, lifetimeKind, RegistrationType.Single) });

            return(new ServiceConfigurationEntry(serviceType, implementations));
        }
        private ServiceImplementationInfo(Type implementationType, LifetimeKind lifetime, RegistrationType registrationType, Func <object> factory)
        {
            if (registrationType == RegistrationType.Decorator && lifetime != LifetimeKind.InstancePerDependency)
            {
                throw new ArgumentException("For implementations of type 'Decorator', the lifetime can only be specified as 'InstancePerDependency'.", "lifetime");
            }

            _implementationType = implementationType;
            _lifetime           = lifetime;
            _registrationType   = registrationType;
            _factory            = factory;
        }
Example #6
0
 public ServiceInfo(
     [NotNull] Type serviceType, [CanBeNull] Type implementationType = null,
     LifetimeKind lifetimeKind = LifetimeKind.Transient,
     [CanBeNull] string[] constructorArgumentExpressions = null,
     [CanBeNull] string instanceExpression = null)
 {
     ServiceType                    = serviceType.NotNull();
     ImplementationType             = implementationType;
     LifetimeKind                   = lifetimeKind;
     ConstructorArgumentExpressions = constructorArgumentExpressions;
     InstanceExpression             = instanceExpression;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceImplementationInfo"/> class.
 /// </summary>
 /// <param name="implementationType">The concrete implementation of the service type.</param>
 /// <param name="lifetime">The <see cref="LifetimeKind"/> of the instances of <paramref name="implementationType"/>. Defaults to <see cref="LifetimeKind.InstancePerDependency"/>.</param>
 /// <param name="registrationType">The <see cref="RegistrationType"/> of the <paramref name="implementationType"/>. Defaults to <see cref="T:RegistrationType.Single"/>.</param>
 public ServiceImplementationInfo(Type implementationType, LifetimeKind lifetime, RegistrationType registrationType = RegistrationType.Single)
     : this(ArgumentUtility.CheckNotNull("implementationType", implementationType), lifetime, registrationType, null)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ServiceImplementationInfo"/> class
 /// with <see cref="RegistrationType"/> set to <see cref="ServiceLocation.RegistrationType.Multiple"/>.
 /// </summary>
 /// <param name="factory">The factory delegate that creates an instance of the service implementation.</param>
 /// <param name="lifetime">The <see cref="LifetimeKind"/> of the instances created by the <paramref name="factory"/>. Defaults to <see cref="LifetimeKind.InstancePerDependency"/>.</param>
 public static ServiceImplementationInfo CreateMultiple <T> (Func <T> factory, LifetimeKind lifetime = LifetimeKind.InstancePerDependency)
     where T : class
 {
     ArgumentUtility.CheckNotNull("factory", factory);
     return(new ServiceImplementationInfo(typeof(T), lifetime, RegistrationType.Multiple, factory));
 }
        /// <summary>
        /// Registers a concrete implementation for the specified <paramref name="serviceType"/>.
        /// </summary>
        /// <param name="serviceConfigurationRegistry">The <see cref="IServiceConfigurationRegistry"/> with which to register the <paramref name="serviceType"/>.</param>
        /// <param name="serviceType">The service type to register a concrete implementation for.</param>
        /// <param name="concreteImplementationType">The type of the concrete implementation to be instantiated when an instance of the
        /// <paramref name="serviceType"/> is retrieved.</param>
        /// <param name="lifetime">The lifetime of the instances.</param>
        /// <param name="registrationType">The registration type of the implementation.</param>
        /// <exception cref="InvalidOperationException">An instance of the <paramref name="serviceType"/> has already been retrieved. Registering factories
        /// or concrete implementations can only be done before any instances are retrieved.</exception>
        public static void Register(this IServiceConfigurationRegistry serviceConfigurationRegistry, Type serviceType, Type concreteImplementationType, LifetimeKind lifetime, RegistrationType registrationType = RegistrationType.Single)
        {
            ArgumentUtility.CheckNotNull("serviceConfigurationRegistry", serviceConfigurationRegistry);
            ArgumentUtility.CheckNotNull("serviceType", serviceType);
            ArgumentUtility.CheckNotNull("concreteImplementationType", concreteImplementationType);

            var serviceImplemetation = new ServiceImplementationInfo(concreteImplementationType, lifetime, registrationType);
            ServiceConfigurationEntry serviceConfigurationEntry;

            try
            {
                serviceConfigurationEntry = new ServiceConfigurationEntry(serviceType, serviceImplemetation);
            }
            catch (ArgumentException ex)
            {
                throw new ArgumentException("Implementation type must implement service type.", "concreteImplementationType", ex);
            }

            serviceConfigurationRegistry.Register(serviceConfigurationEntry);
        }