unity-container

Strategy Pattern and Dependency Injection using Unity

老子叫甜甜 提交于 2019-11-27 19:04:51
I am finally getting my feet wet with Dependency Injection (long overdue); I got started playing with Unity and run into an issue with the strategy pattern. I can use the container to return to me specific implementations of a strategy based on a name, but what I don't see is how I am supposed to get the right strategy in the context. Let's illustrate on a simple example: the context is a car, which has an IEngine (the strategy), with 2 implementations, FastEngine and SlowEngine. The code would look along these lines: public interface IEngine { double MaxSpeed { get; } } internal class

How to remove(unregister) registered instance from Unity mapping?

主宰稳场 提交于 2019-11-27 18:31:05
I meet one problem that i can't solve now. I have the following: UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), "test", instance); where UnityHelper.DefaultContainer is my helper for getting unity container with loaded configuration. here I registered instance as an instance of IMyInterface . So anywhere( some time after using) I want to remove this mapping. Remove it at all. How I can do it? I have tried: UnityHelper.DefaultContainer.Teardown(instance) but is was unsuccessful and the following code returns instance anyway: UnityHelper.DefaultContainer.ResolveAll

Resolving IEnumerable<T> with Unity

Deadly 提交于 2019-11-27 18:20:46
Can Unity automatically resolve IEnumerable<T> ? Let's say I have a class with this constructor: public CoalescingParserSelector(IEnumerable<IParserBuilder> parserBuilders) and I configure individual IParserBuilder instances in the container: container.RegisterType<IParserSelector, CoalescingParserSelector>(); container.RegisterType<IParserBuilder, HelpParserBuilder>(); container.RegisterType<IParserBuilder, SomeOtherParserBuilder>(); can I make this work without having to implement a custom implementation of IEnumerable<IParserBuilder> ? var selector = container.Resolve<IParserSelector>(); So

Register IAuthenticationManager with Unity

北城余情 提交于 2019-11-27 18:18:31
I'm using Unity for Dependencies Injection and using Identiy Provider to manage the user login, register, email confirmation, etc. When I try to register a user, I have this problem: The current type, Microsoft.Owin.Security.IAuthenticationManager, is an interface and cannot be constructed. Are you missing a type mapping? I have no idea how to register this Interface (IAuthenticationManager) in my Unity container. I tried registering the interface with this code, but if I put it, I have other problem: No IUserTokenProvider is registered. container.RegisterType<HttpContextBase>( new

How to register All types of an interface and get instance of them in unity?

匆匆过客 提交于 2019-11-27 18:14:37
问题 How unity can get all instances of an interface and then access them? Code pieces are taken from here : Fail-Tracker In StrcutureMap its possible to register all types of an interface from an assembly and then access them like following: public class TaskRegistry : Registry { public TaskRegistry() { Scan(scan => { scan.AssembliesFromApplicationBaseDirectory( a => a.FullName.StartsWith("FailTracker")); scan.AddAllTypesOf<IRunAtInit>(); scan.AddAllTypesOf<IRunAtStartup>(); scan.AddAllTypesOf

Setter / property injection in Unity without attributes

最后都变了- 提交于 2019-11-27 17:53:05
I am working on a project where the Unity framework is used as the IoC container. My question relates to injecting an optional dependency (in this case a logger) into several classes using property- or setter injection. I do not want to clutter the constructors of all my classes with these optional dependencies, but I cannot find a good way to handle this in Unity. The way you would do this is, according to the MSDN documentation , by adding an attribute to the property: private ILogger logger; [Dependency] public ILogger Logger { get { return logger; } set { logger = value; } } I find this

Cannot Inject Dependencies into ASP.NET Web API Controller using Unity

别说谁变了你拦得住时间么 提交于 2019-11-27 17:01:01
Has anyone had any success running using an IoC container to inject dependencies into ASP.NET WebAPI controllers? I cannot seem to get it to work. This is what I'm doing now. In my global.ascx.cs : public static void RegisterRoutes(RouteCollection routes) { // code intentionally omitted } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); IUnityContainer container = BuildUnityContainer(); System.Web.Http.GlobalConfiguration.Configuration.ServiceResolver.SetResolver( t => { try { return

How better to resolve dependencies in object created by factory?

左心房为你撑大大i 提交于 2019-11-27 16:28:19
For example I have dependency: public interface IMyDependency { } public class MyDependency : IMyDependency { } That injects in MyClass object: public interface IMyInterface { } public class MyClass : IMyInterface { [Dependency] public IMyDependency MyDependency { get; set; } } Also I have a Factory, that creates MyClass instance: public interface IFactory { IMyInterface CreateMyObject(); } public class Factory : IFactory { public IMyInterface CreateMyObject() { // some checks before creation // for. ex check if this type of object supported by OS and thow exception if not return new MyClass()

Could not load file or assembly System.Web.WebPages.Razor, , Version=3.0.0.0 or one of its dependencies

随声附和 提交于 2019-11-27 15:37:29
问题 I am using MVC 5, WCF and Unity framework in my application. I am getting below error when I run WCF service: Server Error in '/' Application. Could not load file or assembly 'System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the

Using Unity Dependency Injection with WCF services

北城以北 提交于 2019-11-27 14:53:58
问题 I have the following after doing some research on other questions: MyServiceHost: public class MyServiceHost : ServiceHost { public MyServiceHost(IUnityContainer container, Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { if (container == null) { throw new ArgumentNullException("container"); } foreach (var cd in this.ImplementedContracts.Values) { cd.Behaviors.Add(new DependencyInjectionInstanceProvider(container)); } } } DependencyInjectionInstanceProvider: