unity-container

Resolve instance with multiple constructors using unity

白昼怎懂夜的黑 提交于 2019-12-20 10:27:21
问题 I'd like to create an instance of a class using unity where the class has two constructors with the same number of parameters. Here is the instantiation: _unityContainer.Resolve<IGradeType>(new ParameterOverride("gradeTypeStringFromXmlFile", gradeTypeStringFromXmlFile)); And here are the constructors: public GradeType(string gradeTypeStringFromXmlFile) { _gradeTypeStringFromXmlFile = gradeTypeStringFromXmlFile; } public GradeType(Enum.GradeType gradeType) { _gradeType = gradeType; } If I try

Should Unity be configured in code or configuration file?

走远了吗. 提交于 2019-12-20 09:37:39
问题 Microsoft's Unity dependency injection framework can be configured either through code or through the applications configuration file (app.config). Code example: IUnityContainer container = new UnityContainer() .RegisterType<IInterface, ConcreteImplementation>(); Configuration example: <unity> <containers> <container> <types> <type type="IInterface, MyAssembly" mapTo="ConcreteImplementation, MyAssembly" /> What are the advantages/disadvantages to each approach? I can think of the obvious

Resolve with parameter from other constructor with unity

雨燕双飞 提交于 2019-12-20 07:22:17
问题 I'm using Unity and inject all my dependencies. Let's say that I have a Manager class like so public class Manager : IManager { public Manager(IRepository repository, string customerName) { } } And a repository class like: public class Repository : IRepository { public Repository(string customerName) { } } I'm using WCF and will resolve the manager and the repository once per request. I register all my types at service startup, but at startup i do not know the "customerName"-parameter. This

How do I create and populate a Dictionary<string,Object> using Unity's xml configuration?

倾然丶 夕夏残阳落幕 提交于 2019-12-20 05:13:07
问题 How do I create and populate a Dictionary using Unity's xml configuration? I need to create a Dictionary and populate the objects using dependency references in unity. How can I accomplish this? 回答1: This works <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <alias alias="MyStuffObject" type="MyStuffObject, MyStuff"/> <!-- dictionary stuff --> <alias alias="IDictionaryOfStuff" type="System.Collections.Generic.IDictionary`2[[System.String, mscorlib],[MyStuffObject, MyStuff]],

Is this possible with Unity (Instead of Castle Windsor)?

本小妞迷上赌 提交于 2019-12-20 04:59:43
问题 This blog post shows a way to implement auto mocking with Castle Windsor and NSubstitute. I don't know or use Castle Windsor, but I do use Unity and NSubstitute. Is there a way to do what he shows using Unity? Here is relevant content of the post: First of all, register an ILazyComponentLoader into Windsor: var c = new WindsorContainer(); c.Register(Component.For<LazyComponentAutoMocker>()); Then, the implementation of LazyComponentAutoMocker is simply this: public class

Unable to inject DBContext into my Web API 2 Controller with Unity

浪尽此生 提交于 2019-12-20 04:25:16
问题 I've been at it for days, but I can't get Unity to inject anything with RegisterType<> into my Controller . I'm using Web Api 2, in Visual Studio 2015, with Unity 4. Whenever I try to inject IUnitOfWork or IRFContext , I get "message": "An error occurred when trying to create a controller of type 'ClPlayersController'. Make sure that the controller has a parameterless public constructor." . I'm using the Unity.AspNet.WebApi to bootstrapp into WebApi. Below is my UnityWebApiActivator [assembly

polymorphic resolution of generic parameters in Unity registerType

僤鯓⒐⒋嵵緔 提交于 2019-12-20 02:27:41
问题 I am trying to do the following and failing: class base {} class derived1 : base {} class derived2 : base {} interface itr<t> where t : class, base {} class c1: itr<derived1> {} class c2 : itr<derived2> {} //The following 2 registrations fail: _unityContainer.RegisterType<itr<base>, c1>("c1"); _unityContainer.RegisterType<itr<base>, c2>("c2"); The error I get is that the second parameter in the above registrations cannot be typecast into the first parameter and this registration is not valid.

Unity: The current type is an interface and cannot be constructed

£可爱£侵袭症+ 提交于 2019-12-20 00:59:19
问题 Got below code to start with public interface IDataContextAsync : IDataContext { Task<int> SaveChangesAsync(CancellationToken cancellationToken); Task<int> SaveChangesAsync(); } public partial class DB1Context : DataContext{ } public partial class DB2Context : DataContext{ } Below is the UnityConfig file. Note: I am using Nuget bootstrapper for ASP.Net MVC and below is my UnityConfig file public static void RegisterTypes(IUnityContainer container) { container .RegisterType<IDataContextAsync,

PerRequestLifetimeManager can only be used in the context of an HTTP request

廉价感情. 提交于 2019-12-19 15:41:03
问题 I have a MVC application that uses Unity as its IoC container and have multiple services defined in my application using the PerRequestLifetimeManager . container.RegisterType<IFileService, FileService>(); Everything works fine, except when I tried to roll my solution to automate tasks (like SharePoint TimerJobs), started in various intervals. For that, I've defined a ServiceLocator -Type class ContainerManager in a separate project, that does essentially this: public static object Resolve

Unity Batch Register by convention

时光怂恿深爱的人放手 提交于 2019-12-19 11:49:20
问题 I'm trying to do the equivalent of the following Autofac code in Unity IoC. builder.RegisterAssemblyTypes(typeof (DataRepository<>).Assembly) .Where(t => t.Name.EndsWith("Repository")) .AsImplementedInterfaces(); This basically replaces individually registering the following: DataSourceDataRepository : DataRepository<DataSource>, IDataSourceDataRepository For clarity: This registers all of my Repository types as their implemented interfaces, so when i inject IDataSourceDataRepository I get a