public IServiceRegister Register <TService>(Func <IServiceProvider, TService> serviceCreator) where TService : class { structureMapContainer.Configure( c => c.For <TService>().Singleton().Use(() => serviceCreator(this)) ); return(this); }
public void Register(Type concrete, Lifestyle lifestyle) { _container.Configure(x => { x.For(concrete).Use(concrete).SetLifecycleTo(ConvertLifestyle(lifestyle)); foreach (var implementedInterface in GetAllInterfacesImplementedBy(concrete)) { x.For(implementedInterface).Use(c => c.GetInstance(concrete)); } }); }
private void BootstrapContainer() { _container = new StructureMap.Container(); _container.Configure(x => { x.For <IConfiguration>() .Singleton() .Use <Configuration>(); }); var registry = new SystemViewRegistry(_container.GetInstance <IConfiguration>()); _container.Configure(x => x.AddRegistry(registry)); }
protected override void ConfigureApplicationContainer(StructureMap.IContainer existingContainer) { existingContainer.Configure(c => c.Scan(s => { s.AssemblyContainingType <IConvertStrings>(); s.IncludeNamespaceContainingType <Md5Encoder>(); s.AddAllTypesOf <IConvertStrings>(); s.RegisterConcreteTypesAgainstTheFirstInterface(); s.WithDefaultConventions().OnAddedPluginTypes(t => t.Singleton()); })); existingContainer.Configure(a => a.For <IRazorConfiguration>().Singleton().Use <RazorConfiguration>()); existingContainer.Configure(c => c.For <IConverterRepository>().Singleton().Use <ConverterRepository>()); ServiceLocator.SetLocatorProvider(() => new StructureMapServiceLocator(existingContainer)); base.ConfigureApplicationContainer(existingContainer); }
public void Handle(Algorithm setting) { var t = _property.PropertyType; if (typeof(IAlgorithmFactory).IsAssignableFrom(t)) { IAlgorithmFactory factory; if (t.IsGenericType && typeof(IAlgorithmFactory <,>).IsAssignableFrom(t.GetGenericTypeDefinition())) { var arguments = t.GetGenericArguments(); var algorithmSetting = CreateAlgorithmSettings(setting.Settings, arguments[1], setting); factory = (IAlgorithmFactory)Activator.CreateInstance(typeof(GenericSettingAlgorithmFactory <,>).MakeGenericType(arguments), setting.Name, algorithmSetting, _container); _container.Configure(x => x.For(arguments[1]).Singleton().Use(algorithmSetting)); } else if (t.IsGenericType && typeof(IAlgorithmFactory <>).IsAssignableFrom(t.GetGenericTypeDefinition())) { var arguments = t.GetGenericArguments(); factory = (IAlgorithmFactory)Activator.CreateInstance(typeof(GenericAlgorithmFactory <>).MakeGenericType(arguments), setting.Name, _container); } else { factory = new DefaultAlgorithmFactory(setting.Name, _container); } _property.SetMethod.Invoke(_settingObject, new[] { factory }); } else { _logger.Error("Setting [{0}] in area [{1}] is not a property for an algorithm. Expected type for this setting is [{2}]", _property.Name, _area.Name, _property.PropertyType.Name); } }
protected override void ConfigureContainer(StructureMap.IContainer container) { container.Configure(cfg => { cfg.For <Supply>().Use(new Supply(new SupplyPile(1, Treasure.Copper, new MockEventAggregator()))); }); }
public void RegisterServices(StructureMap.IContainer container) { // todo: add custom services. container.Configure(cfg => { cfg.For <IArticlesService>().Use <EfArticlesService>(); }); }
protected override void ConfigureContainer(StructureMap.IContainer container) { base.ConfigureContainer(container); Transaction = new TransactionScope(); Context = new DbContext(GetConnectionString()); container.Configure(cfg => cfg.For <IDbContext>().Use(Context)); }
protected override void ConfigureContainer(StructureMap.IContainer container) { var builder = new SupplyPileBuilder(new MockEventAggregator()); container.Configure(cfg => cfg.For <Supply>().Use( new Supply( builder.OfSize(1).OfType(Treasure.Copper), builder.OfSize(0).OfType(Victory.Estate), builder.OfSize(1).OfType(Victory.Province) ))); }
protected override void ConfigureContainer(StructureMap.IContainer container) { container.Configure(cfg => { cfg.For <IEventAggregator>().Singleton().Use <MockEventAggregator>(); cfg.For <Deck>().Singleton().Use(new Deck(7.Coppers(), 3.Estates())); cfg.For <DiscardPile>().Use <DiscardPile>(); cfg.For <Player>().Use <Player>().Ctor <string>().Is("Test player"); cfg.For <Guid>().Use(Guid.NewGuid); }); _eventAggregator = container.GetInstance <IEventAggregator>() as MockEventAggregator; }
public static void LoadFromConfigurationFile(StructureMap.IContainer container, string file, string classNameConvention) { Log.DebugFormat("Reading configuration data from '{0}'", file); var fileName = Path.GetFileNameWithoutExtension(file); var className = string.Format(classNameConvention, fileName); // http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx/ var types = new List <Type>(); foreach (var assembly in GetAssemblies()) { try { var t = assembly.GetTypes(); types.AddRange(t); } catch (ReflectionTypeLoadException ex) { types.AddRange(ex.Types); } } var matchingTypes = types.Where(x => x != null && x.Name == className).ToList(); if (matchingTypes.Count == 0) { return; } if (matchingTypes.Count > 1) { throw new Exception(string.Format("Multiple types found with the name '{0}'", className)); } var type = matchingTypes.First(); if (container.TryGetInstance(type) != null) { return; } var configJson = File.ReadAllText(file); var deserialized = JsonConvert.DeserializeObject(configJson, type); Log.DebugFormat("Mapping configuration data in '{0}' to '{1}'", file, type.Name); //Note: When using Unity, naming the instance BREAKs the resolving container.Configure(x => x.For(type).Use(deserialized)); }
protected override void ConfigureContainer(StructureMap.IContainer container) { base.ConfigureContainer(container); new Bootstrapper().BootstrapApplication(container); container.Configure(cfg => { cfg.For <PlayerBuilder>() .EnrichAllWith(x => x.ForSpec(new PlayerSpec().WithController(new NaivePlayerController()))); cfg.For <IEventAggregator>().Singleton().Use <MockEventAggregator>(); //cfg.For<IPlayerController>().Use<NaivePlayerController>(); }); _eventAggregator = container.GetInstance <IEventAggregator>() as MockEventAggregator; container.SetDefaultsToProfile("UnitTests"); }
public static void LoadContainer(StructureMap.IContainer container, string path, string pattern) { var dirCat = new DirectoryCatalog(path, pattern); var importDef = BuildImportDefinition(); try { var assemblies = dirCat.LoadedFiles; container.Configure(ct => ct.Scan(scan => { //scan.TheCallingAssembly(); //scan.(assembly.FullName); scan.WithDefaultConventions(); scan.AssembliesFromApplicationBaseDirectory(assembly => assembly.FullName.Contains("CodingTest.")); })); using (var aggregateCatalog = new AggregateCatalog()) { aggregateCatalog.Catalogs.Add(dirCat); using (var componsitionContainer = new CompositionContainer(aggregateCatalog)) { IEnumerable <Export> exports = componsitionContainer.GetExports(importDef); IEnumerable <IModule> modules = exports.Select(export => export.Value as IModule).Where(m => m != null); var registrar = new StructureMapModuleRegistrar(container); foreach (IModule module in modules) { module.Initialize(registrar); } } } } catch (ReflectionTypeLoadException typeLoadException) { var builder = new StringBuilder(); foreach (Exception loaderException in typeLoadException.LoaderExceptions) { builder.AppendFormat("{0}\n", loaderException.Message); } throw new TypeLoadException(builder.ToString(), typeLoadException); } }
public static void LoadContainer(StructureMap.IContainer container, string path, string pattern) { var assemblies = Directory.GetFiles(path, pattern) .Select(AssemblyLoadContext.GetAssemblyName) .Select(AssemblyLoadContext.Default.LoadFromAssemblyName) .ToList(); var configuration = new ContainerConfiguration().WithAssemblies(assemblies); try { container.Configure(ct => ct.Scan(scan => { scan.WithDefaultConventions(); scan.AssembliesFromApplicationBaseDirectory(assembly => assembly.FullName.Contains("Net.Core.")); })); using (var componsitionContainer = configuration.CreateContainer()) { IEnumerable <IModule> modules = componsitionContainer.GetExports <IModule>().Where(m => m != null); var registrar = new StructureMapModuleRegistrar(container); foreach (IModule module in modules) { module.Initialize(registrar); } } } catch (ReflectionTypeLoadException typeLoadException) { var builder = new StringBuilder(); foreach (Exception loaderException in typeLoadException.LoaderExceptions) { builder.AppendFormat("{0}\n", loaderException.Message); } throw new TypeLoadException(builder.ToString(), typeLoadException); } }
protected override void ConfigureContainer(StructureMap.IContainer container) { base.ConfigureContainer(container); container.Configure(cfg => cfg.For <SchemaManagerGlobalOptions>().Use(SchemaManagerGlobalOptions.Defaults)); }
public void Register(Type concrete, Contracts.Lifestyle lifestyle) { _container.Configure(x => x.For(concrete).Use(concrete).SetLifecycleTo(ConvertLifestyle(lifestyle))); }
public void Register(Type typeFor, Type typeUse) { _mapContainer.Configure(x => x.For(typeFor).Use(typeUse)); }