Beispiel #1
0
        private object Resolve(Type dependencyType, ImplementationName implementationName = ImplementationName.None)
        {
            if (dependencyType.IsGenericType && (dependencyType.GetGenericTypeDefinition() == typeof(IEnumerable <>)))
            {
                return(GetAllImplementations(dependencyType.GetGenericArguments()[0]));
            }

            if (dependencyType.IsGenericType && dependencies.ContainsKey(dependencyType.GetGenericTypeDefinition()))
            {
                var implementationType = dependencies[dependencyType.GetGenericTypeDefinition()][0].ImplementationType;
                implementationType = implementationType.MakeGenericType(dependencyType.GetGenericArguments());

                if (dependencyType.IsAssignableFrom(implementationType))
                {
                    return(CreateInstance(implementationType));
                }
            }

            if (!dependencies.ContainsKey(dependencyType))
            {
                return(null);
            }

            var implementationInfo = GetImplementationInfo(dependencyType, implementationName);

            if (!dependencyType.IsAssignableFrom(implementationInfo.ImplementationType))
            {
                return(null);
            }

            if (implementationInfo.Lifetime == LifeTime.Transient)
            {
                return(CreateInstance(implementationInfo.ImplementationType));
            }

            if (implementationInfo.Lifetime == LifeTime.Singleton)
            {
                return(Singleton.GetInstance(implementationInfo.ImplementationType, CreateInstance));
            }

            return(null);
        }
Beispiel #2
0
 public AttributeDependencyKey(ImplementationName name)
 {
     ImplementationName = name;
 }
Beispiel #3
0
        private DependencyInfo GetImplementationInfo(Type dependencyType, ImplementationName name)
        {
            var implementations = dependencies[dependencyType];

            return(implementations.Where(info => info.Name == name).First());
        }
Beispiel #4
0
        public T Resolve <T>(ImplementationName implementationName = ImplementationName.None) where T : class
        {
            Type dependencyType = typeof(T);

            return((T)Resolve(dependencyType, implementationName));
        }
Beispiel #5
0
 public DependencyKeyAttribute(ImplementationName implementationName)
 {
     ImplementationName = implementationName;
 }
 public ImplementationInfo(Type type, Lifetime lifetime, ImplementationName name)
 {
     Lifetime           = lifetime;
     ImplementationType = type;
     Name = name;
 }
 protected virtual bool ImplementationNameTest(FeatureName name)
 {
     return(name.ToString() == ImplementationName.ToString());
 }
        public void Register <TDependency, TImplementation>(Lifetime lifetime = Lifetime.Transient, ImplementationName name = ImplementationName.None)
            where TDependency : class where TImplementation : TDependency
        {
            var implementationInfo = new ImplementationInfo(typeof(TImplementation), lifetime, name);

            if (Dependencies.ContainsKey(typeof(TDependency)))
            {
                Dependencies[typeof(TDependency)].Add(implementationInfo);
            }
            else
            {
                Dependencies.Add(typeof(TDependency), new List <ImplementationInfo>());
                Dependencies[typeof(TDependency)].Add(implementationInfo);
            }
        }
Beispiel #9
0
        public T Resolver <T>(ImplementationName implementationName = ImplementationName.None)
        {
            Type serviceType = typeof(T);

            return((T)ResolveCommon(serviceType, implementationName));
        }
Beispiel #10
0
 object Resolver(Type serviceType, ImplementationName implementationName = ImplementationName.None)
 {
     return(ResolveCommon(serviceType, implementationName));
 }
Beispiel #11
0
 public DependencyInfo(Type type, LifeTime lifetime, ImplementationName name)
 {
     Lifetime           = lifetime;
     ImplementationType = type;
     Name = name;
 }