예제 #1
0
        /// <summary>
        /// Registers a delegate to retrieve instances of the specified type.
        /// </summary>
        public PocketContainer Register <T>(Func <PocketContainer, T> factory)
        {
            var replaced = (Func <PocketContainer, object>)Registering?.Invoke(typeof(T), factory);

            if (replaced != null)
            {
                factory = c => (T)replaced(c);
            }

            resolvers[typeof(T)]        = c => factory(c);
            resolvers[typeof(Lazy <T>)] = c => new Lazy <T>(c.Resolve <T>);
            return(this);
        }
예제 #2
0
        /// <summary>
        /// Resolves an instance of the specified type.
        /// </summary>
        public T Resolve <T>()
        {
            var resolved = (T)resolvers.GetOrAdd(typeof(T), _ =>
            {
                var implicitResolver = ImplicitResolver <T>();

                if (Registering?.Invoke(typeof(T), implicitResolver) is Func <PocketContainer, object> replacedResolver)
                {
                    implicitResolver = c => (T)replacedResolver(c);
                }

                return(implicitResolver);
            })(this);

            return(CallAfterResolve(typeof(T), resolved, out var replaced)
                       ? (T)replaced
                       : resolved);
        }
예제 #3
0
        /// <inheritdoc />
        /// <summary>
        ///     Registers the specified character spawn.
        /// </summary>
        /// <param name="characterSpawn">The character spawn.</param>
        public void Register(ICharacterSpawn characterSpawn)
        {
            if (Members.ContainsKey(characterSpawn.Id))
            {
                return;
            }

            ChannelRegisteringEventArgs channelRegisteringEventArgs = new ChannelRegisteringEventArgs(characterSpawn);

            Registering?.Invoke(this, channelRegisteringEventArgs);
            if (channelRegisteringEventArgs.Cancel)
            {
                return;
            }

            Members.Add(characterSpawn.Id, characterSpawn);

            ChannelRegisteredEventArgs channelRegisteredEventArgs = new ChannelRegisteredEventArgs(characterSpawn);

            Registered?.Invoke(this, channelRegisteredEventArgs);
        }
예제 #4
0
        /// <summary>
        /// RegisterType a type mapping with the container, where the created instances will use
        /// the given <see cref="LifetimeManager"/>.
        /// </summary>
        /// <param name="typeFrom"><see cref="Type"/> that will be requested.</param>
        /// <param name="typeTo"><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 typeFrom, Type typeTo, string name, LifetimeManager lifetimeManager, InjectionMember[] injectionMembers)
        {
            // Validate imput
            var to = typeTo ?? throw new ArgumentNullException(nameof(typeTo));

            if (typeFrom != null && !typeFrom.GetTypeInfo().IsGenericType&& !to.GetTypeInfo().IsGenericType)
            {
                if (!typeFrom.GetTypeInfo().IsAssignableFrom(to.GetTypeInfo()))
                {
                    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
                                                              Constants.TypesAreNotAssignable, typeFrom, to), nameof(typeFrom));
                }
            }

            // Register type
            var registration = new TypeRegistration(typeFrom, typeTo, name, lifetimeManager, injectionMembers);

            var old = SetOrUpdate(registration.RegisteredType, registration.Name, registration);

            if (old is IDisposable disposable)
            {
                _lifetimeContainer.Remove(disposable);
                disposable.Dispose();
            }

            Registering?.Invoke(this, new RegisterEventArgs(registration.RegisteredType, registration.MappedToType, registration.Name, registration.LifetimeManager));

            if (null != injectionMembers && injectionMembers.Length > 0)
            {
                var proxy = new PolicyListProxy(_context, registration);
                foreach (var member in injectionMembers)
                {
                    member.AddPolicies(registration.RegisteredType, registration.MappedToType, registration.Name, proxy);
                }
            }

            return(this);
        }
예제 #5
0
 /// <summary>
 /// Called by the container when the service type is registering.
 /// </summary>
 /// <param name="e">The event argument.</param>
 protected virtual void OnRegistering(ServiceRegisterEventArgs e)
 {
     Registering?.Invoke(this, e);
 }