dependency-injection

Setting lazy static variable first initializes then assigns?

时间秒杀一切 提交于 2020-01-14 19:44:21
问题 I realize that static variables are implicitly lazy , which is really great. Doing the below will not create the instance until it's first called: static var test = Test() However, assigning a new instance to the static variable initializes the original, then assigns the new instance which is troubling for me: SomeType.test = AnotherTest() //Initializes Test then AnotherTest type To give more context on what I'm trying to do, I'm trying to setup a pure Swift dependency injection using this

Simple Injector with dependency on two objects that implement same interface

醉酒当歌 提交于 2020-01-14 14:22:45
问题 I'm writing an application (exposed via an ASP.NET web API) that's entire purpose is to consume two data sources, and expose the similarities and differences. So the API has the following setup: public class FooController : WebAPI { public FooController(IFooRepository repoFromSourceA, IFooRepository repoFromSourceB) { ... } } Maintaining the distinction of which one is of which source (SourceA and SourceB cannot be interchanged) seems to make container.RegisterCollection(..) impossible (or

Simple Injector with dependency on two objects that implement same interface

只愿长相守 提交于 2020-01-14 14:22:07
问题 I'm writing an application (exposed via an ASP.NET web API) that's entire purpose is to consume two data sources, and expose the similarities and differences. So the API has the following setup: public class FooController : WebAPI { public FooController(IFooRepository repoFromSourceA, IFooRepository repoFromSourceB) { ... } } Maintaining the distinction of which one is of which source (SourceA and SourceB cannot be interchanged) seems to make container.RegisterCollection(..) impossible (or

What is the best strategy for Dependency Injection of User Input?

别来无恙 提交于 2020-01-14 10:32:32
问题 I've used a fair amount of dependency injection, but I'd like to get input on how to handle information from the user at runtime. I have a class that connects to a com port. I allow the user to select the com port number. Right now, I have that com port parameter as a constructor argument. The reasoning being that the class cannot function without that information, and it's implementation specific (a mock version of this class wouldn't need a com port). The alternative is to have a "Start"

ServiceLocator and the Open/Closed Principle

痞子三分冷 提交于 2020-01-14 09:38:27
问题 I'd like to: Make commonly required services visible to all classes that need them, with a minimum of boilerplate, and without sacrificing testability! It's a small project and I think DI might be overkill, but maybe I'm wrong? Anyhow, I have been focusing on the ServiceLocator pattern as described by Martin Fowler In a client class' constructor, I have something like this: this->db = Locator::getDb(); this->log = Locator::getLogger(); Then the rest of the class' methods access the service

Serilog in Azure Functions

故事扮演 提交于 2020-01-14 08:47:32
问题 Each method in Azure Functions can have a Microsoft.Extensions.Logging.ILogger injected into it for logging. Using WebJobsStartup with a startup class you can change the logging to use Serilog using the following syntax: [assembly: WebJobsStartup(typeof(Startup))] namespace MyFuncApp { public class Startup : IWebJobsStartup { public void Configure(IWebJobsBuilder builder) { builder.Services.AddLogging( lb => lb.ClearProviders() .AddSerilog( new LoggerConfiguration() .Enrich.FromLogContext()

Serilog in Azure Functions

。_饼干妹妹 提交于 2020-01-14 08:47:03
问题 Each method in Azure Functions can have a Microsoft.Extensions.Logging.ILogger injected into it for logging. Using WebJobsStartup with a startup class you can change the logging to use Serilog using the following syntax: [assembly: WebJobsStartup(typeof(Startup))] namespace MyFuncApp { public class Startup : IWebJobsStartup { public void Configure(IWebJobsBuilder builder) { builder.Services.AddLogging( lb => lb.ClearProviders() .AddSerilog( new LoggerConfiguration() .Enrich.FromLogContext()

DI (Autofac) in a plugin architecture: Is one separate DI container per plug-in OK?

倾然丶 夕夏残阳落幕 提交于 2020-01-14 07:51:33
问题 I am trying to introduce DI (with Autofac) into an existing Windows Forms application. This application has a basic plug-in architecture where each plugin displays its own form. On startup, the application scans registered assemblies for types that implement IPlugin , and then activates these using Activator.CreateInstance : public interface IPlugin { Form MainForm { get; } } I cannot change this given framework. This means, each plugin class is instantiated through non-DI means, and it seems

How to use/configure Unity Container IOC in my situation

拈花ヽ惹草 提交于 2020-01-14 04:40:23
问题 I have some trouble implementing the Unity IOC into my project reading from config file. Here is what I have 1) ClasslibraryA 2) ClasslibraryB that references ClasslibraryA 3) Winforms App that references ClasslibraryB Note: SomeOther app will reference ClassLibraryA, eg. a web service. ClasslibraryA will have to be configured for IOC depending on where it is used. eg. IDataSource will be different if it is called in the web service and when it is called from a local app. ClasslibraryB will

How to implement Dagger for worker classes in Dagger 2.16 ?('android.arch.work:work-runtime')

怎甘沉沦 提交于 2020-01-14 04:36:41
问题 How to implement Dagger for worker classes in Dagger 2.16? public class SyncWorker extends Worker { @Inject ISettingRepository settingRepository; @NonNull @Override public Result doWork() { sync(); return Result.SUCCESS; } private void sync() { } } my AppComponent @Singleton @Component(modules = { AndroidSupportInjectionModule.class, BaseModule.class, ApiModule.class, UserDatabaseModule.class, SaleDatabaseModule.class, AppModule.class, ActivityBuilderModule.class }) public interface