예제 #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);
 }
예제 #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);
            });
        }
예제 #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);
 }
예제 #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);
 }
예제 #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);
 }
예제 #6
0
 /// <summary>
 /// Initializes an instance of the <see cref="ContainerOptions"/> class.
 /// </summary>
 public ContainerOptions()
 {
     DefaultRegistrationConflictBehavior = RegistrationConflictBehavior.Throw;
     EnsureDefaults();
 }