Esempio n. 1
0
 public ContainerFactory WithInfoLogger(LogInfo logger)
 {
     infoLogger        = logger;
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 2
0
 public ContainerFactory WithConfigFile(string fileName)
 {
     configFileName    = fileName;
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 3
0
 public ContainerFactory WithErrorLogger(LogError logger)
 {
     errorLogger       = logger;
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 4
0
 public ContainerFactory WithValueFormatter <T>(Func <T, string> formatter)
 {
     valueFormatters[typeof(T)] = o => formatter((T)o);
     typesContextCache          = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 5
0
 public ContainerFactory WithAssembliesFilter(Func <AssemblyName, bool> newAssembliesFilter)
 {
     assembliesFilter  = n => newAssembliesFilter(n) || n.Name == "SimpleContainer";
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 6
0
 public ContainerFactory WithTypes(Type[] newTypes)
 {
     types             = () => newTypes;
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 7
0
 public ContainerFactory WithStaticConfigurator(Action <ContainerConfigurationBuilder> newConfigure)
 {
     staticConfigure   = newConfigure;
     typesContextCache = null;
     configurationByProfileCache.Clear();
     return(this);
 }
Esempio n. 8
0
 private IContainer CreateContainer(TypesContext currentTypesContext, ConfigurationRegistry configuration)
 {
     return(new Implementation.SimpleContainer(configuration, new ContainerContext
     {
         infoLogger = infoLogger,
         genericsAutoCloser = currentTypesContext.genericsAutoCloser,
         typesList = currentTypesContext.typesList,
         valueFormatters = valueFormatters
     }, errorLogger));
 }
Esempio n. 9
0
        public IContainer Build()
        {
            var typesContext = typesContextCache;

            if (typesContext == null)
            {
                var targetTypes = types()
                                  .Concat(Assembly.GetExecutingAssembly().GetTypes())
                                  .Where(x => !x.Name.StartsWith("<>", StringComparison.OrdinalIgnoreCase))
                                  .Distinct()
                                  .ToArray();
                typesContext = new TypesContext {
                    typesList = TypesList.Create(targetTypes)
                };
                typesContext.genericsAutoCloser = new GenericsAutoCloser(typesContext.typesList, assembliesFilter);
                if (configFileName != null && File.Exists(configFileName))
                {
                    typesContext.fileConfigurator = FileConfigurationParser.Parse(typesContext.typesList.Types, configFileName);
                }
                ConfigurationRegistry staticConfiguration;
                if (staticConfigure == null)
                {
                    staticConfiguration = ConfigurationRegistry.Empty;
                }
                else
                {
                    var builder = new ContainerConfigurationBuilder();
                    staticConfigure(builder);
                    staticConfiguration = builder.RegistryBuilder.Build(typesContext.typesList, null);
                }
                var configurationContainer = CreateContainer(typesContext, staticConfiguration);
                typesContext.configuratorRunner = configurationContainer.Get <ConfiguratorRunner>();
                typesContextCache = typesContext;
            }
            ConfigurationRegistry configurationRegistry;

            if (!configurationByProfileCache.TryGetValue(profile ?? typeof(ContainerFactory), out configurationRegistry))
            {
                var builder = new ContainerConfigurationBuilder();
                var configurationContext = new ConfigurationContext(profile, settingsLoader, parameters);
                typesContext.configuratorRunner.Run(builder, configurationContext, priorities);
                if (typesContext.fileConfigurator != null)
                {
                    typesContext.fileConfigurator(builder);
                }
                configurationRegistry = builder.RegistryBuilder.Build(typesContext.typesList, null);
                configurationByProfileCache.Add(profile ?? typeof(ContainerFactory), configurationRegistry);
            }
            return(CreateContainer(typesContext, configurationRegistry.Apply(typesContext.typesList, configure)));
        }
Esempio n. 10
0
        private ContainerFactory WithTypesFromAssemblies(ParallelQuery <Assembly> assemblies)
        {
            var newTypes = assemblies
                           .Where(x => assembliesFilter(x.GetName()))
                           .SelectMany(a =>
            {
                try
                {
                    return(a.GetTypes());
                }
                catch (ReflectionTypeLoadException e)
                {
                    const string messageFormat = "can't load types from assembly [{0}], loaderExceptions:\r\n{1}";
                    var loaderExceptionsText   = e.LoaderExceptions.Select(ex => ex.ToString()).JoinStrings("\r\n");
                    throw new SimpleContainerException(string.Format(messageFormat, a.GetName(), loaderExceptionsText), e);
                }
            });

            types             = newTypes.ToArray;
            typesContextCache = null;
            return(this);
        }