unity-container

How to inject constructor in controllers by unity on mvc 4

£可爱£侵袭症+ 提交于 2019-11-29 16:57:52
I'm using unity 1.1 version and i couldn't inject constructor. My code sush as: Registering dependencies in global.asax Application_Startup method: Core.Instance.Container.RegisterType<ICartBusiness, CartBusiness>(); Injecting constructor: private ICartBusiness _business; public FooController(ICartBusiness business) { _business = business; } mvc throwing this exception: No parameterless constructor defined for this object. P.S: i can't use any new version of unity because i'm using too referenced old dlls so i can't use unity.WebApi or unity.Mvc3 dlls. How to do this? You need to tell ASP.MVC

Using Unity.WebForms in ASP.NET

三世轮回 提交于 2019-11-29 15:14:14
问题 I am trying to implement DI in a webforms project, so I installed the Unity.WebForms dlls in my UI layer. As soon as I did an App_Start folder was created for me with a UnityWebFormsStart class file. Inside this file there is a method RegisterDependencies which asks to be edited. What is the next step after registering the dependencies? Is there something I need to add in the Global.asax class file? And how and where do I resolve a type inside a webform? Do I decorate that with any attributes

C# unity interception by attribute

空扰寡人 提交于 2019-11-29 15:11:47
问题 Is there a way on how to use interception by attribute in C# unity and keep the object registration code in XML file(like app.config)? If yes, can you provide me with code, how should such registration look? I did a lot of workaround but did not find some working solution for this issue. 回答1: I'm assuming you mean using a custom attribute to indicate what methods to intercept. You can use policy injection to achieve interception using XML configuration. First, let's define a custom attribute:

PRISM WPF - Navigation creates new view every time

江枫思渺然 提交于 2019-11-29 14:41:12
问题 I'm using PRISM 4 Navigation API with Unity in WPF. I have a tree-view that initiates a RequestNavigate passing in the selected tree node's ID ( GUID ). _regionManager.RequestNavigate(RegionNames.DetailRegion, ViewNames.SiteView + "?ID=" + site.ID); In my module, I have registered the view/view-model like so: _container.RegisterType<SiteDetailsViewModel>(); _container.RegisterType<object, SiteDetailsView>(ViewNames.SiteView); When I select different nodes from the tree view, the DetailsRegion

Unity: The type LogWriter cannot be constructed

穿精又带淫゛_ 提交于 2019-11-29 14:02:12
For the following "project" I am getting a very annoying and inexplicable error when resolving Unity for DI. InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value. ?ex.Message; "Resolution of the dependency failed, type = \"WindowsFormsApplication1.Performance\", name = \"(none)\".\r\nException occurred while: while resolving.\r\nException is: InvalidOperationException - The type LogWriter cannot be constructed. You must configure the container to supply this value.\r\n-----------------------------------------------\r\nAt

Breaking SOLID Principles in multiple implementation of an Interface

好久不见. 提交于 2019-11-29 12:58:46
I am facing a problem with dependency inversion in a factory method and it is also breaking Open Closed principle. My code looks like below codes public interface IWriter { void WriteToStorage(string data); } public class FileWriter : IWriter { public void WriteToStorage(string data) { //write to file } } public class DBWriter : IWriter { public void WriteToStorage(string data) { //write to DB } } Now I an using a factory class to solve the object creation. It look like below code public interface IFactory { IWriter GetType(string outputType); } public class Factory : IFactory { public IWriter

Where to place and configure IoC container in a WPF application?

拥有回忆 提交于 2019-11-29 12:31:45
问题 I am working on a middle sized WPF application (MVVM) that should be extensible and maintainable in the future. Thus I decided to use an IoC container (Unity in this case) to keep things flexible. However I am not sure where to place and configure Unity in a WPF application. I guess container should be accessible globally so it should probably go to Application class. But should I make it as static property? Should I configure it in Application_Startup() event handler? Eg: /// <summary> ///

Unity IoC for resolving assemblies dynamically

落爺英雄遲暮 提交于 2019-11-29 10:24:53
问题 We are using unity as IoC. We came across unique problem. We have created interface called IPlugin. This interface is shared across various third party vendors to develop their own plug in based on this interface. These plug ins then fits into our system. Vendors will provide their plugs in as dll. What we want is , Using unity we want to resolve all assembly’s type which is implemented with IPlugin interface. I came to know that this is achievable via MEF export attribute, I am wondering

Unity: Change default lifetime manager for implicit registrations and/or disable them

谁说我不能喝 提交于 2019-11-29 09:35:31
The Unity container will automatically resolve any type that it can figure out on its own without the need for manual registration. That's good in some ways, but the problem I have is that it uses a TransientLifetimeManager for this type of resolution, while I almost always want a ContainerControlledLifetimeManager . I can still register my types as singletons manually, of course, but if I forget, instead of getting an unhandled exception at startup, the app will launch successfully and everything will appear to work. But there will eventually be bugs, possibly very subtle, hard-to-diagnose

Unity IoC does not inject dependency into Web API Controller

雨燕双飞 提交于 2019-11-29 09:08:25
I'm very new to using Unity, but my problem is that whenever I call my web service, I get an exception stating that "Make sure that the controller has a parameterless public constructor" I've followed multiple tutorials and I still get the same issue. In the Register function of my WebApiConfig class, I have var container = new UnityContainer(); container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager()); config.DependencyResolver = new UnityResolver(container); Here is my UnityResolver class using Microsoft.Practices.Unity; using System; using System.Collections.Generic;