unity-container

How to do open generic decorator chaining with unity + UnityAutoRegistration

杀马特。学长 韩版系。学妹 提交于 2019-12-17 19:37:12
问题 Went off on an interesting tangent today after reading this article on command handler decoration. I wanted to see if I could implement the pattern using Unity instead of SimpleInjector, and so far it is proving extremely difficult. The first thing I had to do was install UnityAutoRegistration to resolve the open generic ICommandHandler<TCommand> interface. Current solution for that aspect is as follows: Container = new UnityContainer().LoadConfiguration(); Container.ConfigureAutoRegistration

Unity: Implicit ResolvedParameter for unnamed registrations

有些话、适合烂在心里 提交于 2019-12-17 18:30:13
问题 The UserService constructor has two parameters, a IUnitOfWork and a IUserRepository : public UserService(IUnitOfWork unitofWork, IUserRepository userRepository) { ... } I am using named registrations to differentiate between multiple instances of IUnitOfWork , so when registering the UserService with the Unity container, I need to explicitly specify the parameters using an InjectionConstructor : container.RegisterType<IUserService, UserService>( new InjectionConstructor( new ResolvedParameter

Using Unity to inject dependencies into a custom ActionFilter

别等时光非礼了梦想. 提交于 2019-12-17 18:29:56
问题 At the moment, I have a custom ControllerFactory into which I inject my Unity container: in global.asax Application_Start(): var container = InitContainer(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); var factory = new UnityControllerFactory(container); ControllerBuilder.Current.SetControllerFactory(factory); In the controller factory I set my controllers to use a custom ActionInvoker like so: protected override IController GetControllerInstance(System.Web.Routing

Strategy Pattern and Dependency Injection using Unity

对着背影说爱祢 提交于 2019-12-17 15:36:25
问题 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

Singleton Per Call Context (Web Request) in Unity

a 夏天 提交于 2019-12-17 08:11:07
问题 A few days ago, I had an issue with ASP.Net threading. I wanted to have a singleton object per web request. I actually need this for my unit of work. I wanted to instantiate a unit of work per web request so that identity map is valid through out the request. This way I could use an IoC to inject my own IUnitOfWork to my repository classes transparently, and I could use the same instance to query and then update my entities. Since I am using Unity, I mistakenly used PerThreadLifeTimeManager.

Can I pass constructor parameters to Unity's Resolve() method?

末鹿安然 提交于 2019-12-17 03:05:20
问题 I am using Microsoft's Unity for dependency injection and I want to do something like this: IDataContext context = _unityContainer.Resolve<IDataContext>(); var repositoryA = _unityContainer.Resolve<IRepositoryA>(context); //Same instance of context var repositoryB = _unityContainer.Resolve<IRepositoryB>(context); //Same instance of context IDataContext context2 = _unityContainer.Resolve<IDataContext>(); //New instance var repositoryA2 = _unityContainer.Resolve<IRepositoryA>(context2);

MVC, EF - DataContext singleton instance Per-Web-Request in Unity

余生长醉 提交于 2019-12-17 02:21:06
问题 I have a MVC 3 web application, where I am using the Entity Framework for the data access. Furthermore, I have made a simple use of the repository pattern, where e.g. all Product related stuff is handled in the "ProductRepository" and all User related stuff is handled in the "UserRepository". Thus, I am using the UNITY container, to make a singleton instance of the DataContext, which I inject into each of the repositories. A quick search on Google, and everyone recommends you to NOT use a

Unity not injecting dependencies defined in base class

那年仲夏 提交于 2019-12-14 04:17:48
问题 I am integrating Unity 2.0 into my ASP.NET application (using the UnityPageHandlerFactory approach) and have everything working great until I tried to move one of the dependencies into a PageBase class that would then be shared by all pages. This property is never set when BuildUp is called. I'm using the UnityPageHandlerFactory class described here which uses the BuildUp(type, object) method to inject dependencies into each page when it is requested. As long as I have the properties defined

Having AutoMapper to inject dependencies using an IoC Container when needed

陌路散爱 提交于 2019-12-14 04:07:17
问题 I have tried almost everything, but I cannot get AutoMapper to map A => B when B doesn't have a parameterless constructor . I'm using Unity and all the dependencies are registered conveniently but, how do I say to AutoMapper "hey, if the target instance needs some dependency in the constructor, ask Unity to build it, and do the mapping later. I've tried with Mapper.Initialize(configuration => { configuration.ConstructServicesUsing(container.Resolve); configuration.CreateMap<Person,

Unity Container - Lazy injection

混江龙づ霸主 提交于 2019-12-14 03:57:10
问题 Lets say i have a clas class Foo : FooBase { public Foo(Settings settings, IDbRepository db) : base(settings) { this.db = db; } ... } Basically FooBase receives settings via constructor and loads some config from configuration file. Then i have the class MySQLRepository which implements IDbRepository class MySQLRepository : IDbRepository { ... public MySQLRepository(IConfigurationRepository config) { conn = new MySQLConnection(config.GetConnectionString()); } ... } in Program.cs i have: Foo