Exemple #1
0
        public object Resolve(ServiceIdentity identity)
        {
            var activator = GetActivator(identity);

            if (activator == null)
            {
                return(null);
            }

            var context  = new ActivationContext(this);
            var instance = activator.ActivateService(context);

            return(instance);
        }
Exemple #2
0
 public ServiceNotRegisteredException(ServiceIdentity identity)
     : this(identity.ServiceType, identity.ServiceName)
 {
 }
 private static string CreateMessage(ServiceIdentity identity)
 {
     return((identity.ServiceName == null)
         ? string.Format("An activator configuration has already been set for the registration of a service of type '{0}'.", identity.ServiceType)
         : string.Format("An activator configuration has already been set for the registration of a service of type '{0}' with the name '{1}.", identity.ServiceType, identity.ServiceName));
 }
 public ActivatorConfigurationAlreadyRegisteredException(ServiceIdentity identity)
     : base(CreateMessage(identity))
 {
     Identity = identity;
 }
Exemple #5
0
 private static string CreateMessage(ServiceIdentity identity)
 {
     return((identity.ServiceName == null)
         ? string.Format("A service of type '{0}' has already been registered.", identity.ServiceType)
         : string.Format("A service of type '{0}' has already been registered with the name '{1}.", identity.ServiceType, identity.ServiceName));
 }
Exemple #6
0
        private IActivator TryCreateGenericActivator(ServiceIdentity identity)
        {
            if (identity.ServiceType.IsConstructedGenericType)
            {
                var genericTypeDefinition = identity.ServiceType.GetGenericTypeDefinition();

                var unboundActivatorGroup = _activatorGroups.TryGetValue(genericTypeDefinition);
                if (unboundActivatorGroup != null)
                {
                    var genericArguments = identity.ServiceType.GenericTypeArguments;
                    var boundActivators  = unboundActivatorGroup.
                                           GetAllActivators().
                                           OfType <IUnboundActivator>().
                                           Select(a => a.ConstructBoundActivator(genericArguments));

                    foreach (var activator in boundActivators)
                    {
                        Register(activator);
                    }

                    var boundActivator = TryGetRegisteredActivator(identity);
                    if (boundActivator != null)
                    {
                        return(boundActivator);
                    }
                }
                else
                {
                    var realServiceType = identity.ServiceType.GenericTypeArguments[0];

                    if (genericTypeDefinition == FuncServiceType)
                    {
                        var realIdentity  = new ServiceIdentity(realServiceType, identity.ServiceName);
                        var realActivator = GetActivator(realIdentity);

                        if (realActivator != null)
                        {
                            return(new FuncActivator(identity, realActivator));
                        }
                    }
                    else if (genericTypeDefinition == LazyServiceType)
                    {
                        var realIdentity  = new ServiceIdentity(realServiceType, identity.ServiceName);
                        var realActivator = GetActivator(realIdentity);

                        if (realActivator != null)
                        {
                            return(new LazyActivator(identity, realActivator));
                        }
                    }
                    else if (
                        genericTypeDefinition == EnumerableServiceType ||
                        genericTypeDefinition == CollectionServiceType ||
                        genericTypeDefinition == ListServiceType)
                    {
                        var itemActivators = GetAllActivators(realServiceType);

                        return(new ArrayActivator(identity, realServiceType, itemActivators));
                    }
                }
            }

            return(null);
        }