Beispiel #1
0
        private static IEnumerable <object> ProvideParameters(IEnumerable <ParameterInfo> parameters,
                                                              DependenciesConfiguration dependencyConfiguration)
        {
            var provider = new DependencyProvider(dependencyConfiguration);

            return(parameters.Select(provider.Resolve));
        }
        static void Main(string[] args)
        {
            DependenciesConfiguration c = new DependenciesConfiguration();

            c.Register <MySQLRepository, MySQLRepository>(false);
            c.Register(typeof(ServiceImpl <>), typeof(ServiceImpl <>), false);

            //c.Register<ClassForExample, ClassForExample>(true);
            //c.Register<ClassForExample66<ClassForExample>,ClassForExample66<ClassForExample>>(true);
            //c.Register<ClassForExample2, ClassForExample2>(true);
            //c.Register<ClassForExample3, ClassForExample3>(false);
            try
            {
                DependencyProvider            p       = new DependencyProvider(c);
                ServiceImpl <MySQLRepository> example = p.Resolve <ServiceImpl <MySQLRepository> >();
                example.GetNum();
            }
            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);
            }
        }
 public DependencyProvider(DependenciesConfiguration configuration)
 {
     if (ValidateConfiguration(configuration))
     {
         _configuration = configuration;
         _stack         = new ConcurrentStack <Type>();
     }
     else
     {
         throw new ConfigurationValidationException("Configuration is not valid!");
     }
 }
Beispiel #4
0
        public static object GetInstance(Type type, DependenciesConfiguration dependencyConfiguration)
        {
            var constructors = ChooseConstructors(type).ToList();

            if (constructors.Count == 0)
            {
                throw new CreatorException($"{type} has no injectable constructor");
            }
            foreach (var constructor in constructors)
            {
                var parameters = constructor.GetParameters();
                var arguments  = ProvideParameters(parameters, dependencyConfiguration);
                return(constructor.Invoke(arguments.ToArray()));
            }

            throw new CreatorException($"Can't create instance of {type}");
        }
        //valudation for configuration
        private bool ValidateConfiguration(DependenciesConfiguration 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);
        }
 public DependencyProvider(DependenciesConfiguration depConfig)
 {
     _config = depConfig.Config;
 }
 public DependencyContainer(DependenciesConfiguration configuration)
 {
     DependenciesConfiguration = configuration;
 }
Beispiel #8
0
 public DependencyProvider(DependenciesConfiguration configuration)
 {
     dependencies = configuration.Dependencies;
 }
Beispiel #9
0
 public DependencyProvider(DependenciesConfiguration config)
 {
     configuration = config;
     cycleStack    = new ConcurrentStack <Type>();
 }
Beispiel #10
0
 public DependencyProvider(DependenciesConfiguration dependencyConfiguration)
 {
     _dependencyConfiguration = dependencyConfiguration;
 }
Beispiel #11
0
 public DependencyProvider(DependenciesConfiguration Configuration)
 {
     configuration = Configuration;
 }
Beispiel #12
0
 public DependencyProvider(DependenciesConfiguration configuration)
 {
     dependencies           = configuration.dependencies;
     openGenericDependecies = configuration.openGenericDependencies;
 }