Example #1
0
        private object Resolve(Type dependencyType, ServiceImplementations implementationName = ServiceImplementations.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);
        }
Example #2
0
        private ImplementationInfo GetImplementationInfo(Type dependencyType, ServiceImplementations name)
        {
            var implementations = dependencies[dependencyType];

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

            return((T)Resolve(dependencyType, implementationName));
        }
Example #4
0
        public void Register(Type dependency, Type implementationType, Lifetime lifetime = Lifetime.Transient, ServiceImplementations implementation = ServiceImplementations.None)
        {
            var newImplInfo = new ImplementationInfo(implementationType, lifetime, implementation);

            if (!dependencies.ContainsKey(dependency))
            {
                dependencies.Add(dependency, new List <ImplementationInfo>());
            }

            dependencies[dependency].Add(newImplInfo);
        }
Example #5
0
        public void Register <TDependency, TImplementation>(Lifetime lifetime = Lifetime.Transient, ServiceImplementations implementation = ServiceImplementations.None)
            where TDependency : class
            where TImplementation : TDependency
        {
            var newImplInfo = new ImplementationInfo(typeof(TImplementation), lifetime, implementation);

            if (!dependencies.ContainsKey(typeof(TDependency)))
            {
                dependencies.Add(typeof(TDependency), new List <ImplementationInfo>());
            }

            dependencies[typeof(TDependency)].Add(newImplInfo);
        }
Example #6
0
 public ImplementationInfo(Type implType, Lifetime lifetime, ServiceImplementations implementation)
 {
     this.lifetime      = lifetime;
     implementationType = implType;
     implementationName = implementation;
 }
Example #7
0
 public DependencyKeyAttribute(ServiceImplementations name)
 {
     implementationName = name;
 }