Exemple #1
0
 /// <summary>
 /// Registers a service.
 /// </summary>
 /// <typeparam name="TService">The type, base type or interface of the service.</typeparam>
 /// <typeparam name="TImplementation">The concrete implementation of the <typeparamref name="TService"/>.</typeparam>
 /// <param name="registrator">The registrator.</param>
 /// <param name="lifecycle">The lifecycle used in the creating/locating the service.</param>
 /// <param name="behavior">The behavior to take when there is a duplicate registration.</param>
 public static void Register <TService, TImplementation>(this IRegistrator registrator,
                                                         ILifecycle lifecycle,
                                                         RegistrationConflictBehavior behavior)
     where TImplementation : TService
 {
     registrator.Register(typeof(TService), typeof(TImplementation), lifecycle, behavior);
 }
Exemple #2
0
        /// <summary>
        /// Registers type <paramref name="implementationType"/> to be created when
        /// <paramref name="serviceType" /> is resolved.
        /// </summary>
        /// <param name="serviceType">The basetype or interface to register.</param>
        /// <param name="implementationType">The type that will be created or returned when resolving the service type.</param>
        /// <param name="lifecycle">The lifecycle object used to create the <see cref="IInstanceResolver"/>s and <see cref="InstanceResolver"/>s.</param>
        /// <param name="conflictBehavior">The behavior to use when there is another type already registered for the given service type.</param>
        /// <remarks>
        /// See <seealso cref="RegistratorExtensions"/> for additional ways to register types.
        /// </remarks>
        public void Register(Type serviceType,
                             Type implementationType,
                             ILifecycle lifecycle,
                             RegistrationConflictBehavior conflictBehavior)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (implementationType == null)
            {
                throw new ArgumentNullException(nameof(implementationType));
            }
            if (!serviceType.IsAssignableFrom(implementationType))
            {
                throw new InvalidOperationException($"Cannot register '{implementationType.FullName}' as '{serviceType.FullName}' because it does not implement the service type.");
            }
            if (implementationType.IsAbstract || implementationType.IsInterface)
            {
                throw new InvalidOperationException($"Cannot create an instance of type '{implementationType.FullName}'.");
            }

            lifecycle = lifecycle ?? _options.DefaultLifecycle;

            var registration = lifecycle.CreateRegistration(serviceType, implementationType);

            _registrationMap.AddOrUpdate(serviceType,
                                         registration,
                                         (type, oldRegistration) =>
            {
                if (oldRegistration != null)
                {
                    conflictBehavior = conflictBehavior == RegistrationConflictBehavior.Default
                            ? _options.DefaultRegistrationConflictBehavior
                            : conflictBehavior;

                    switch (conflictBehavior)
                    {
                    case RegistrationConflictBehavior.Keep:
                        return(oldRegistration);

                    case RegistrationConflictBehavior.Replace:
                        return(registration);

                    default:
                    case RegistrationConflictBehavior.Throw:
                        throw new InvalidOperationException($"Service '{type}' has already been registered");
                    }
                }

                return(registration);
            });
        }
Exemple #3
0
 /// <summary>
 /// Registers a service
 /// </summary>
 /// <typeparam name="TService">The type of the service to register</typeparam>
 /// <param name="registrator">The registrator.</param>
 /// <param name="lifecycle">The lifecycle used in the creating/locating the service.</param>
 /// <param name="behavior">The behavior to take when there is a duplicate registration.</param>
 public static void Register <TService>(this IRegistrator registrator,
                                        ILifecycle lifecycle,
                                        RegistrationConflictBehavior behavior)
 {
     registrator.Register <TService, TService>(lifecycle, behavior);
 }
Exemple #4
0
 /// <summary>
 /// Registers a service.
 /// </summary>
 /// <typeparam name="TService">The type, base type or interface of the service.</typeparam>
 /// <typeparam name="TImplementation">The concrete implementation of the <typeparamref name="TService"/>.</typeparam>
 /// <param name="registrator">The registrator.</param>
 /// <param name="lifecycle">The lifecycle used in the creating/locating the service.</param>
 /// <param name="behavior">The behavior to take when there is a duplicate registration.</param>
 public static void Register <TService, TImplementation>(this IRegistrator registrator,
                                                         LifecycleType lifecycleType,
                                                         RegistrationConflictBehavior behavior)
 {
     registrator.Register(typeof(TService), typeof(TImplementation), Lifecycle.GetByType(lifecycleType), behavior);
 }
Exemple #5
0
 /// <summary>
 /// Registers a service.
 /// </summary>
 /// <typeparam name="TService">The type, base type or interface of the service.</typeparam>
 /// <typeparam name="TImplementation">The concrete implementation of the <typeparamref name="TService"/>.</typeparam>
 /// <param name="registrator">The registrator.</param>
 /// <param name="behavior">The behavior to take when there is a duplicate registration.</param>
 public static void RegisterSingleton <TService, TImplementation>(this IRegistrator registrator, RegistrationConflictBehavior behavior)
     where TImplementation : TService
 {
     registrator.Register(typeof(TService), typeof(TImplementation), Lifecycle.Singleton, behavior);
 }
 /// <summary>
 /// Initializes an instance of the <see cref="ContainerOptions"/> class.
 /// </summary>
 public ContainerOptions()
 {
     DefaultRegistrationConflictBehavior = RegistrationConflictBehavior.Throw;
     EnsureDefaults();
 }