ioc-container

Should controller lifestyle always be transient in Windsor configuration for ASP.NET MVC?

感情迁移 提交于 2021-02-18 12:22:31
问题 I ran into a problem where I had an Html.DropDownList in my view that would postback the selected value the first time I submitted the form, but each subsequent postback would only post data from the initial postback. So I added lifestyle="transient" to the component element where I had configured my controller for castle windsor, which fixed the problem, but of course made postbacks take longer since a new controller was being instantiated per request. Given the information above, what

how to implement IOC without a global static service (non-service locator solution)?

跟風遠走 提交于 2021-02-17 08:44:16
问题 we want to use Unity for IOC. All i've seen is the implementation that there is one global static service (let's call it the the IOCService) which holds a reference to the Unity container, which registers all interface/class combinations and every class asks that object: give me an implementation for Ithis or IThat. Frequently i see a response that this pattern is not good because it leads to a dependency from ALL classes to the IOCService (not to the Unity container because it is only known

IoC Container. Inject container

旧街凉风 提交于 2021-02-04 19:56:57
问题 What I want: Resolve object A, and inside object A, I want use same container to resolve object C: public static void Work() { IUnityContainer con = new UnityContainer(); con.RegisterType<IA, A>(); con.RegisterType<IB, B>(); con.RegisterType<IC, C>(); var a = con.Resolve<IA>(); } interface IA { } interface IB { } interface IC { } class A : IA { public A(IB b, IUnityContainer con) { for (int i = 0; i < 10; i++) { var c = con.Resolve<IC>(); } } } class B : IB { }; class C : IC { }; Problem: On

Spring Dependency Injection - Private fields - Anti Pattern? Why does it even work?

允我心安 提交于 2021-01-29 21:33:07
问题 I am generally a c# developer but working on Java now and then I see a lot of dependency injection using Spring on private properties, with no public way of setting the value. I was surprised this actually works, but I guess it’s possible via reflection? Surely this is terrible practice?! I can't see how anyone unit testing or inspecting the class would possibly know that a private member needs to be set from some external framework. How would you even set the property when you are unit

SimpleInjector : Register collection with InstanceCreator

主宰稳场 提交于 2021-01-28 08:14:33
问题 I'm trying to register a collection along with its instanceCreator. I wasn't able to find any overload for the Container.Collection.Register method which accepts an instanceCreator. If i try to run a loop and register a type along with its instanceCreator multiple times using the Container.Register(instanceCreator, lifestyle) , i'm unable to do it because i get the error that type cannot be registerd multiple times. Basically what i'm trying to do is: foreach(var item in items){ Container

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

Resolving wrapper classes in C# with the Unity IoC container

放肆的年华 提交于 2020-06-22 10:55:09
问题 I want to use Unity resolve IService to two different implementations, to make use of a wrapper class, the equivalent of: IService service = new DispatcherService(new RealService(), Application.Current.Dispatcher); Where both DispatcherService and RealService implement the IService interface. I have a library containing some services with asynchronous operations. A simplified form of this service looks like this: public interface IService { IAsyncResult StartSomeOperation(); event

How to Inject Dependencies to Dynamically Loaded Assemblies

ⅰ亾dé卋堺 提交于 2020-04-10 18:08:55
问题 I have a manager class that loads various plug-in modules contained in separate assemblies through reflection. This modules are communication to the outside world (WebAPI, various other web protocols). public class Manager { public ILogger Logger; // Modules need to access this. private void LoadAssemblies() { // Load assemblies through reflection. } } These plug-in modules must communicate with an object contained within the manager class. How can I implement this? I thought about using