unity-container

Unity PerRequestLifetimeManager re-using object in different requests

旧城冷巷雨未停 提交于 2019-11-30 13:28:34
I've set up Unity for dependency injection for our project. The project itself is an ASP.NET application that uses both MVC and Web API. For the database context, I'm using the PerRequestLifetimeManager . This is done so that the different bits of business logic are using the same context (and thus the same transaction). In order to be able to use the PerRequestLifetimeManager , I've added references to the nuget packages Unity bootstrapper for ASP.NET MVC and Unity bootstrapper for ASP.NET Web API . For use of this lifetime manager in Web API, the following line has been added to the startup

Unity IOC container and how to resolve different instances of the same interface

て烟熏妆下的殇ゞ 提交于 2019-11-30 10:56:30
I have a unity container that I am registering types within like so: IUnityContainer container = new UnityContainer() .RegisterType<ITaxAuthorityRateService, TaxAuthorityPopulationRateService>( "PopulationRate" ) .RegisterType<ITaxAuthorityRateService, TaxAuthorityBusinessLicenseRateService>( "BusinessLicenseRate" ); Then I also want to register 2 different services that take a ITaxAuthorityRateService variable in their constructor. Both services need a different class that derives from ITaxAuthorityRateService. How can I handle that situation? Ok I figured it out. Keeping the names the same

Microsoft Unity. How to specify a certain parameter in constructor?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 10:35:08
问题 I'm using Microsoft Unity. I have an interface ICustomerService and its implementation CustomerService . I can register them for the Unity container using the following code: container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager()); If CustomerService has a certain parameter in its constructor (e.g. ISomeService1 ), I use the following code (I need to specify SomeService1 ): container.RegisterType<ICustomerService, CustomerService>(new TransientLifetimeManager

Using Unity.WebForms in ASP.NET

笑着哭i 提交于 2019-11-30 10:07:05
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? The Unity.WebForms dll and NuGet package does a few things for you in the background. It will ensure

again about log4net and Unity IOC config

感情迁移 提交于 2019-11-30 09:57:54
I've bumped through a few of these questions laying around various sites and the answers seem to be spun by l4n officianados toward the value of the lightweight wrapper that log4net 'is' (don't you get it?) and similar points of mind-boggling fact. However it seems that what users are asking for (and this is my question) is how to fit the log4net objet model into the registertype/registerinstance sequence in the fluent config interface. The goal here wouldn't be to re-wrap l4n, but simply to grab a decent reference that doesnt' require hijacking the fluent flow, as it were. poor ejemplow, I

PRISM WPF - Navigation creates new view every time

会有一股神秘感。 提交于 2019-11-30 09:45:53
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 displays the SiteDetailsView as expected, but when I like to navigate back to the same node, a new

Unity Container Multiple Implementations of same interface

一曲冷凌霜 提交于 2019-11-30 09:14:44
I'm studying up on the unity containers and have a quick question on how to resolve a class's construction to multiple different implementations of an interface. Here's my code: public interface IRenderer { void DrawSquare(Square square); void DrawCircle(Circle circle); } public interface IShape { void Draw(IRenderer renderer); } public class Dx11Renderer : IRenderer { public void DrawSquare(Square square) { } public void DrawCircle(Circle circle) { } } public class GlRenderer : IRenderer { public void DrawSquare(Square square) { } public void DrawCircle(Circle circle) { } } public class

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

梦想的初衷 提交于 2019-11-30 08:42:33
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> /// Interaction logic for App.xaml /// </summary> public partial class App : Application { public static

Getting unity to resolve multiple instances of the same type

人走茶凉 提交于 2019-11-30 06:55:43
问题 I want to do a simple resolve of multiple type registrations (ultimately constructor injected, but using .Resolve to see if Unity is even capable of such things. In every case below, Unity resolves 0 items where it should be resolving 2. Is there some switch in unity that turns on post-2007 behavior? Or am I just drastically missing something? Here is my code: public interface IFoo {} public class Foo1 : IFoo{} public class Foo2 : IFoo{} class Program { static void Main(string[] args) { var

Best Practices for IOC Container

跟風遠走 提交于 2019-11-30 06:21:40
问题 I'm using the Unity IOC container and I'm just wondering what is the best best way to access the container for multiple classes. Should every class have an IUnityContainer member and then pass the container in by constructor? Should there be a singleton class with an IOC container? How about asp.net development? Could somebody guide me in the right direction? Thanks. 回答1: IMHO it is not advisable to inject the entire container into a class or to have an application wide static IoC service