inversion-of-control

Bean with multiple constructors in Java-based Spring configuration

坚强是说给别人听的谎言 提交于 2020-08-04 13:29:01
问题 I am trying to refactor some application to use Spring DI instead of plain java and stuck with the issue. Basically i have a class with several constructors: public MyClass() { this(new A()); } public MyClass(A a) { this(a, new B())); } public MyClass(String string) { this(new A(string)); } public MyClass(A a, B b) { this.a = a; this.c = a.getC(); this.b = b; this.d = b.getD(); } public MyClass(A a, B b, D d) { this.a = a; this.c = a.getC(); this.b = b; this.d = d; } These constructors are

Bean with multiple constructors in Java-based Spring configuration

拥有回忆 提交于 2020-08-04 13:27:06
问题 I am trying to refactor some application to use Spring DI instead of plain java and stuck with the issue. Basically i have a class with several constructors: public MyClass() { this(new A()); } public MyClass(A a) { this(a, new B())); } public MyClass(String string) { this(new A(string)); } public MyClass(A a, B b) { this.a = a; this.c = a.getC(); this.b = b; this.d = b.getD(); } public MyClass(A a, B b, D d) { this.a = a; this.c = a.getC(); this.b = b; this.d = d; } These constructors are

PerRequestLifetimeManager and Task.Factory.StartNew - Dependency Injection with Unity

痞子三分冷 提交于 2020-07-18 07:11:40
问题 How to manage new tasks with PerRequestLifeTimeManager? Should I create another container inside a new task?(I wouldn't like to change PerRequestLifeTimeManager to PerResolveLifetimeManager/HierarchicalLifetimeManager) [HttpPost] public ActionResult UploadFile(FileUploadViewModel viewModel) { var cts = new CancellationTokenSource(); CancellationToken cancellationToken = cts.Token; Task.Factory.StartNew(() => { // _fileService = DependencyResolver.Current.GetService<IFileService>();

PerRequestLifetimeManager and Task.Factory.StartNew - Dependency Injection with Unity

不打扰是莪最后的温柔 提交于 2020-07-18 07:11:30
问题 How to manage new tasks with PerRequestLifeTimeManager? Should I create another container inside a new task?(I wouldn't like to change PerRequestLifeTimeManager to PerResolveLifetimeManager/HierarchicalLifetimeManager) [HttpPost] public ActionResult UploadFile(FileUploadViewModel viewModel) { var cts = new CancellationTokenSource(); CancellationToken cancellationToken = cts.Token; Task.Factory.StartNew(() => { // _fileService = DependencyResolver.Current.GetService<IFileService>();

How to create a child scope from the parent with default dependency injection in .NET Core?

浪尽此生 提交于 2020-07-05 05:26:31
问题 I am building a console .NET Core application. It periodically runs a method that does some work. How do I make ServiceProvider behave in the same way it behaves in ASP.NET Core apps. I want it to resolve scoped services when the method begins it's execution and dispose the resolved services at the end of the method. // pseudocode globalProvider.AddScoped<ExampleService>(); // ... using (var scopedProvider = globalProvider.CreateChildScope()) { var exampleService = scopedProvider.Resolve

Unity - Inject different classes for the same interface

妖精的绣舞 提交于 2020-07-04 07:48:39
问题 I have one interface: IFoo Two classes implementing that interface: FooOne and FooTwo And two classes ClassOne and ClassTwo receiving an IFoo parameter in the constructor. How I configure unity so ClassOne receives a FooOne instance and ClassTwo receives a FooTwo using only one container? I can't do it at runtime so it must be in the config file. 回答1: Have a look at the Unity documentation. For a more readable config file you should define type aliases for IFoo , FooOne , FooTwo , ClassOne