Exemple #1
0
        private void _addService(Type interfaceType, Type implementationType, DiType diType,
                                 IReadOnlyCollection <object> args = null)
        {
            if (!interfaceType.IsAssignableFrom(implementationType))
            {
                throw new Exception($"The type {implementationType.Name} is not subclass of {interfaceType.Name}");
            }

            if (_typeIsRegistered(implementationType))
            {
                throw new Exception("The type " + implementationType.Name + " is already registered.");
            }

            DiConfig config;

            if (args == null || args.Count == 0)
            {
                config = new DiConfig();
            }
            else
            {
                config = new DiConfigWithArguments {
                    PrimitiveArgumentList = args.ToList()
                };
            }

            config.ImplementationType = implementationType;
            config.InterfaceType      = interfaceType;
            config.Type = diType;

            _configs.Add(config);
        }
        public IIoCContainer CreateContainer()
        {
            IIoCContainer container = new IoCContainer();

            using (var sr = new StreamReader(new FileStream(_jsonConfigFile, FileMode.Open)))
            {
                var configuration = JsonConvert.DeserializeObject <IoCConfiguration>(sr.ReadToEnd());
                foreach (var serializedConfig in configuration.DiConfigs)
                {
                    var config = new DiConfig
                    {
                        InterfaceType      = GetType(serializedConfig.InterfaceType),
                        ImplementationType = GetType(serializedConfig.ImplementationType),
                        Type = serializedConfig.Type
                    };
                    container.AddConfig(config);
                }

                foreach (var serializedConfig in configuration.DiConfigsWithArguments)
                {
                    var config = new DiConfigWithArguments
                    {
                        InterfaceType      = GetType(serializedConfig.InterfaceType),
                        ImplementationType = GetType(serializedConfig.ImplementationType),
                        Type = serializedConfig.Type,
                        PrimitiveArgumentList = serializedConfig.PrimitiveArgumentList.ToList()
                    };
                    container.AddConfig(config);
                }
            }

            return(container);
        }