private static IDependencyContainer BuildRootContainer() { var unityContainer = new UnityContainer(); var rootContainer = new UnityDependencyContainer(unityContainer); // Create a list of module initializers and initialize the container var section = ConfigurationManager.GetSection("container.modules") as ModuleSettingsSection; if (section != null) { section.Modules .Cast <ModuleSettings>().ToList() .ForEach(m => m.CreateInitializer().Initialize(rootContainer)); } // Read static configuration file if any and load container and override static configuration var configuration = ConfigurationManager.OpenMappedExeConfiguration( new ExeConfigurationFileMap() { ExeConfigFilename = string.Format(@"{0}\unity.config", App.AppPath) }, ConfigurationUserLevel.None); var unitySection = (UnityConfigurationSection)configuration.GetSection("unity"); if (unitySection != null) { unityContainer.LoadConfiguration(unitySection); } // To allow for runtime overriding of static configuration, return a child container. return(new UnityDependencyContainer(rootContainer.InnerContainer.CreateChildContainer())); }
public static void RegisterTypes(IUnityContainer container) { UnityDependencyContainer.GetCurrent().Register <IXmlConfigReader, XmlConfigEngine>(); UnityDependencyContainer.GetCurrent().Register <IXmlConfigWriter, XmlConfigEngine>(); UnityDependencyContainer.GetCurrent().Register <IApplicationPool, ApplicationPoolsEngine>(); UnityDependencyContainer.GetCurrent().Register <IPathProvider, ServerPathProvider>(); }
private static IUnityContainer BuildUnityContainer() { var container = UnityDependencyContainer.GetCurrent().Container; RegisterTypes(container); return(container); }
protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); IDependencyContainer dependencyContainer = new UnityDependencyContainer(); MainViewModel viewModel = new MainViewModel(); viewModel.Settings.MainBorder.Size = 60; viewModel.Settings.CommandBorder.Size = 40; DefaultLogFactory logFactory = new DefaultLogFactory("root"); logFactory.AddSerializer(viewModel.Message); dependencyContainer.Definitions .AddScoped <ICommandCollection>(dependencyContainer.ScopeName, viewModel.Commands) .AddScoped <ILogFactory>(dependencyContainer.ScopeName, logFactory); RegisterCommands(dependencyContainer, viewModel.Commands); MainWindow view = new MainWindow(); view.ViewModel = viewModel; view.Show(); }
static void TestPerf() { IDependencyProvider my = new SimpleDependencyProvider(); IDependencyProvider unity = new UnityDependencyContainer(); DebugIteration("My", 100000, () => my.Resolve <Service>()); DebugIteration("unity", 100000, () => unity.Resolve <Service>()); }
public void Container_WhenRegistered_ResolvesMappedType() { var container = new UnityDependencyContainer(); container.RegisterType(typeof(BaseType), typeof(DerivedType)); var instance = container.Resolve(typeof(BaseType)); Assert.NotNull(instance); Assert.IsType<DerivedType>(instance); }
public static void Test() { IDependencyContainer dependencyContainer = new UnityDependencyContainer(); dependencyContainer.Definitions .AddScoped <ResolveUrl>(dependencyContainer.ScopeName, new ResolveUrl(ResolveUrl)); ResolveUrl resolveUrl = dependencyContainer.Resolve <ResolveUrl>(); resolveUrl("~/root"); }
public void Container_WhenRegistered_ResolvesMappedType() { var container = new UnityDependencyContainer(); container.RegisterType(typeof(BaseType), typeof(DerivedType)); var instance = container.Resolve(typeof(BaseType)); Assert.NotNull(instance); Assert.IsType <DerivedType>(instance); }
private static HelloService Sequence() { IDependencyContainer dependencyContainer = new UnityDependencyContainer(); dependencyContainer.Definitions .AddScoped <string>(dependencyContainer.ScopeName, "Hi") .AddScoped <TextWriter>(dependencyContainer.ScopeName, Console.Out); SequenceBootstrapper bootstrapper = new SequenceBootstrapper(task => dependencyContainer.Resolve <IBootstrapTask>(task)); bootstrapper.Register <Sequence.WriterBootstrapTask>(); bootstrapper.Register <Sequence.HelloBootstrapTask>(); bootstrapper.Initialize(); //return Engine.Environment.With<HelloService>(); throw new NotImplementedException(); }
public static void RegisterTypes(IUnityContainer container) { UnityDependencyContainer.GetCurrent().Register <IPathProvider, ServerPathProvider>(); }
public async Task InitAsync() { Container = new UnityDependencyContainer(); Container.AddTransient <IRuntime>(this); Container.AddSingleton <ILogger, NullLogger>(); var assembly = typeof(Runtime).Assembly.FullName; ReflectionExtensions.GetVersionIndependentName(assembly, out string extactedVersion); Version version; try { version = new Version(extactedVersion); } catch { Console.WriteLine("Failed to get RocketMod version."); version = new Version(0, 0, 0, 0); } Container.Activate(typeof(RegistrationByConvention)); await Container.Resolve <ICommandProvider>().InitAsync(); var settingsProvider = Container.Resolve <IRocketConfigurationProvider>(); await settingsProvider.LoadAsync(); int p = (int)Environment.OSVersion.Platform; bool isLinux = (p == 4) || (p == 6) || (p == 128); string mode = settingsProvider.Configuration.Logging.ConsoleMode; if (!mode.Equals("Compat", StringComparison.OrdinalIgnoreCase)) { // use ANSI logging if ((isLinux && mode.Equals("Default", StringComparison.OrdinalIgnoreCase)) || mode.Equals("ANSI", StringComparison.OrdinalIgnoreCase)) { Container.AddSingleton <ILogger, AnsiConsoleLogger>("console_logger"); } // use RGB logging (windows only) //else if (!isLinux && (mode.Equals("Default", StringComparison.OrdinalIgnoreCase) || mode.Equals("RGB", StringComparison.OrdinalIgnoreCase))) // Container.RegisterSingletonType<ILogger, RgbConsoleLogger>("console_logger"); } IHost impl = Container.Resolve <IHost>(); Version = version; string rocketInitializeMessage = "Initializing RocketMod Runtime v" + version; impl.Console.WriteLine(rocketInitializeMessage, Color.DarkGreen); impl.Console.WriteLine(@" ,: ,' | / : --' / \/ />/ / <//_\ __/ / )'-. / ./ :\ /.' ' '/' + ' `. .-"" - ( | . .- ' '. ((. )8: .' / (_ ) _. :(. )8P ` . ( `-' ( `. . . : ( .a8a) / _`(""a `a. )""' ((/ . ' )==' (( ) .8"" + (`'8a.( _( ( ..-. `8P) ` ) + -' ( -ab: ) ' _ ` (8P""Ya _(( )b -`. ) + (8)(_.aP"" _a \( \ * + ) / (8P(88)) ("" `"" `", Color.Cyan); if (!Container.IsRegistered <ILogger>("default_file_logger")) { string logsDirectory = Path.Combine(WorkingDirectory, "Logs"); if (!Directory.Exists(logsDirectory)) { Directory.CreateDirectory(logsDirectory); } Container.AddSingleton <ILogger, FileLogger>("default_file_logger"); FileLogger fl = (FileLogger)Container.Resolve <ILogger>("default_file_logger"); fl.File = Path.Combine(logsDirectory, "Rocket.log"); fl.LogInformation(rocketInitializeMessage, Color.DarkGreen); } IPermissionProvider permissions = Container.Resolve <IPermissionProvider>(); if (!Directory.Exists(WorkingDirectory)) { Directory.CreateDirectory(WorkingDirectory); } await permissions.LoadAsync(this); Container.Resolve <ILogger>().LogInformation($"Initializing host: {impl.Name}", Color.Green); await impl.InitAsync(this); }
private static void TestUnity() { //IDependencyContainer container = new UnityDependencyContainer() // .AddTransient<IHelloService, HiService>() // .AddScoped<IMessageWriter, ConsoleWriterActivator>("Request", new ConsoleWriterActivator()) // .AddTransient<Presenter>(); IDependencyContainer container = new UnityDependencyContainer(); container.Definitions .AddScoped <IHelloService, HiService>() .AddScopedFactory <IMessageWriter, ConsoleWriterFactory>("Request") .AddTransient <Presenter>(); using (IDependencyProvider provider = container.Scope("Request")) { Presenter presenter; using (IDependencyProvider provider2 = provider.Scope("Sub")) { presenter = provider2.Resolve <Presenter>(); presenter.Execute(); presenter = provider2.Resolve <Presenter>(); presenter.Execute(); } using (IDependencyProvider provider2 = provider.Scope("Sub")) { presenter = provider2.Resolve <Presenter>(); presenter.Execute(); } presenter = provider.Resolve <Presenter>(); presenter.Execute(); } using (IDependencyProvider provider = container.Scope("Request")) { Presenter presenter; using (IDependencyProvider provider2 = provider.Scope("Sub")) { presenter = provider2.Resolve <Presenter>(); presenter.Execute(); } using (IDependencyProvider provider2 = provider.Scope("Sub")) { presenter = provider2.Resolve <Presenter>(); presenter.Execute(); } presenter = provider.Resolve <Presenter>(); presenter.Execute(); } container.Dispose(); }