Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            DependenciesConfig c = new DependenciesConfig();

            c.Register <MyRepository, MyRepository>(false);
            c.Register(typeof(ServiceImpl <>), typeof(ServiceImpl <>), false);
            try
            {
                DependencyProvider         p       = new DependencyProvider(c);
                ServiceImpl <MyRepository> example = p.Resolve <ServiceImpl <MyRepository> >();
                Console.WriteLine(example.GetNum());
                Console.ReadLine();
                Console.ReadLine();
                Console.ReadLine();
                Console.ReadLine();
            }
            catch (ConfigurationValidationException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (ConstructorNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (CycleDependencyException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (TypeNotRegisterException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Ejemplo n.º 2
0
 public DependencyProvider(DependenciesConfig configuration)
 {
     if (ValidateConfiguration(configuration))
     {
         config = configuration;
         stack  = new ConcurrentStack <Type>();
     }
     else
     {
         throw new ConfigurationValidationException("Configuration is not valid!");
     }
 }
Ejemplo n.º 3
0
        private bool ValidateConfiguration(DependenciesConfig configuration)
        {
            foreach (Type tDependency in configuration.dependencies.Keys)
            {
                if (!tDependency.IsValueType)
                {
                    foreach (ImplementationInfo dependency in configuration.dependencies[tDependency])
                    {
                        Type tImplementation = dependency.implementationType;

                        if (tImplementation.IsAbstract || tImplementation.IsInterface || !tDependency.IsAssignableFrom(tImplementation))
                        {
                            return(false);
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
            return(true);
        }