/// <summary> /// Adds the specified configurations. /// </summary> /// <param name="configurations">The configurations.</param> /// <returns><see cref="ExposedTypes"/></returns> public ExposedTypes Add(ExposedTypeConfigurations configurations) { foreach (ExposedTypeConfiguration configuration in configurations) { Type type = TypeDiscovery.Instance.ResolveByName(configuration.TypeName) ?? throw new TypeLoadException(configuration.TypeName); if (!_items.TryGetValue(type, out HashSet <ExposeClassAttribute> list)) { _items.Add(type, list = new HashSet <ExposeClassAttribute>()); } foreach (ExposedAttributeTypeConfiguration configurationAttribute in configuration.Attributes) { Type exposedType = null; if (configurationAttribute.ExposedType != null) { exposedType = TypeDiscovery.Instance.ResolveByName(configurationAttribute.ExposedType) ?? throw new TypeLoadException(configurationAttribute.ExposedType); } ExposeClassAttribute e = new ExposeClassAttribute(configurationAttribute.Context, configurationAttribute.Name) { LifeCycle = configurationAttribute.LifeCycle, ExposedType = exposedType ?? type }; list.Add(e); } } return(this); }
private static void AppendConfiguration(IServiceCollection services, IConfiguration configuration, Type type, ExposeClassAttribute attribute) { Func <IServiceProvider, object> func = null; var typeExposed = attribute.ExposedType ?? type; string name = attribute.Name; if (!typeExposed.IsAssignableFrom(type)) { throw new IocException($"Try to register {type.FullName} configuration with contract {typeExposed.FullName}. {typeExposed.Name} can't be assignated from {type.Name}"); } var factory = new Factory <object>(type, new Type[] { }); func = srvs => { var _configuration = factory.Create(); configuration.Bind(name, _configuration); return(_configuration); }; if (attribute.LifeCycle == IocScopeEnum.Singleton) { services.Add(ServiceDescriptor.Singleton(typeExposed, func)); } else { services.Add(ServiceDescriptor.Transient(typeExposed, func)); } Trace.WriteLine($"{name} configuration node mapped on {type} and added in {attribute.LifeCycle}"); }