Esempio n. 1
0
        public static void Register(Configuration configuration)
        {
            var objectFactory = new ObjectProvider();

            // Get IoC container.
            var container = TinyIoCContainer.Current;

            var configurationSection = GetSection <InversionOfControlSection>("iocSection", configuration);

            foreach (var mapping in configurationSection.Mappings)
            {
                if (ReferenceEquals(mapping.Parameters, default(InversionOfControlSection.ParameterCollection)) || mapping.Parameters.Count == 0)
                {
                    var registerOptions = string.IsNullOrEmpty(mapping.Identifier)
                        ? container.Register(mapping.Interface, mapping.Implementation)
                        : container.Register(mapping.Interface, mapping.Implementation, mapping.Identifier);

                    if (mapping.Singleton)
                    {
                        registerOptions.AsSingleton();
                    }
                    else
                    {
                        registerOptions.AsMultiInstance();
                    }
                }
                else
                {
                    var implementation = mapping.Implementation;
                    var parameters     = mapping.Parameters;

                    if (string.IsNullOrEmpty(mapping.Identifier))
                    {
                        container.Register(mapping.Interface, (c, np) => objectFactory.CreateByDictionaryType(implementation, parameters.ToDictionary(p => p.Name, p => (object)p.Value)));
                    }
                    else
                    {
                        container.Register(mapping.Interface, (c, np) => objectFactory.CreateByDictionaryType(implementation, parameters.ToDictionary(p => p.Name, p => (object)p.Value)), mapping.Identifier);
                    }
                }
            }

            // create an ioc binding for httpcontextbase since it is used for some controllers.
            container.Register <HttpContextBase>((c, o) => new HttpContextWrapper(HttpContext.Current));
        }