unity-container

Unity Dependency Injection for WCF services

萝らか妹 提交于 2019-12-03 18:01:45
问题 Can someone direct me to a good example of Unity Dependency Injection for WCF services? Any blog or msdn article will also help. 回答1: This answer gives an example on how to enable DI in WCF with Castle Windsor. Just replace the IWindsorContainer with an IUnityContainer in the example and you should be all set, although you may also want to change the class names from WindsorXyz to UnityXyz :) 回答2: To inject dependencies into WCF services I had to implement a service host factory. I have found

Unity - how to use multiple mappings for the same type and inject into an object

♀尐吖头ヾ 提交于 2019-12-03 16:39:11
I'm using Unity 2.0 and in the following code I'm trying to inject a specific tool in the Worker object. I would like to use the following code. But ofcourse there is an error "Resolution of the dependency failed". I believe I should be able to do something like this, but I'm having a difficult time figuring it out. IUnityContainer container = new UnityContainer(); container.RegisterType<IWorker, Worker>("Worker") .RegisterType<ITool, ToolA>("ToolA") .RegisterType<ITool, ToolB>("ToolB") .RegisterType<ITool, ToolC>("ToolC"); IWorker worker = container.Resolve<Worker>("ToolA"); I know this doesn

Lazy resolution of dependency injection

谁说胖子不能爱 提交于 2019-12-03 16:13:46
I have .net classes I am using unity as IOC for resolving our dependencies. It tries to load all the dependencies at the beginning. Is there a way (setting) in Unity which allows to load a dependency at runtime? There's even better solution - native support for Lazy<T> and IEnumerable<Lazy<T>> in the Unity 2.0. Check it out here . I have blogged some code here to allow passing 'lazy' dependencies into your classes. It allows you to replace: class MyClass(IDependency dependency) with class MyClass(ILazy<IDependency> lazyDependency) This gives you the option of delaying the actual creation of

ControllerFactory for specific portable area

≡放荡痞女 提交于 2019-12-03 15:26:14
My main ASP.NET MVC composite application uses a global Unity container to register types. The app sets the controller factory up to use this global container. I would like to refactor this such that each one of my portable areas leverages it's own child Unity container, so that different areas can implement interfaces in varying ways. It seems like I would need to have a different ControllerFactory per area. How would I accomplish that, given that the following sets the factory for all? ControllerBuilder.Current .SetControllerFactory(/* controller factory with global unity container */); You

How do I implement a delegate factory?

狂风中的少年 提交于 2019-12-03 15:02:09
The documentation for Autofac has an interesting page describing its ability to automatically generate delegate factories . It also strongly suggests that you can get similar results without Autofac by writing them by hand. I'm using Unity for IoC and would like to avoid passing the container around to objects that need to create other objects, so how would you write a delegate factory without Autofac? Well I've never used Unity so far, so my answer is quite vague. The principal is simple. You define some delegates which represent factories. Then you create a ‘factory’-class which has public

.Net RIA Services: DomainService Needs a Parameterless Constructor?

﹥>﹥吖頭↗ 提交于 2019-12-03 14:30:48
I'm using the July CTP of .Net RIA Services in an ASP.Net application with some Silverlight components. I'm calling the RIA Services from Silverlight. My problem arose when I tried to use Unity and constructor dependency injection in my Domain Service (a LinqToEntitiesDomainService object). The Silverlight application now complains about not having a parameterless constructor. I don't want to have a parameterless constructor, I want Unity to resolve the constructor arguments. Is this possible? Am I doing something wrong? Or should I find another way to inject my constructor arguments? public

Unity Static Factory Extension

瘦欲@ 提交于 2019-12-03 13:55:46
I can't seem to find Microsoft.Practices.Unity.StaticFactory.dll anywhere. Is there another way of registering a static factory? Looking for something like this container.RegisterFactory(()=> FooFactory.CreateFoo()); StaticFactory.dll was rolled into the main assembly as part of Unity 2.0. It was generally useful enough that we didn't want to force people to carry around a separate DLL just to get it. As such, you can still use the existing API, you just don't need to add the assembly reference. However, we've deprecated the old API. The extension can be added, but does nothing, it's already

WPF with Unity Container - How to register and resolve ViewModels to Views

空扰寡人 提交于 2019-12-03 13:29:36
问题 Hi I am trying to use Unity container in WPF MVVM application. I have not used Prism as it seems to heavy. Here is the application structure. I am trying to figure out how to resolve Views to ViewModels and dependencies of the view models (services). Application: Views MainWindow.xaml CustomerList.xaml CustomerDetail.xaml BookList.xaml BookDetail.xaml ViewModels MainViewModel CustomerListViewModel BoolListViewModel BookDetailViewModel CustomerDetailViewModel Library ICustomerService

Unity Resolve Multiple Classes

末鹿安然 提交于 2019-12-03 13:22:22
How do I get microsoft unity to 'construct' a list of classes for a given interface type. Very Simple example: List<IShippingCalculation> list = new List<IShippingCalculation>(); list.Add(new NewYorkShippingCalculation()); list.Add(new FloridaShippingCalculation()); list.Add(new AlaskShippingCalculation()); //Not What I want public void calcship(List<IShippingCalculation> list) { var info = new ShippingInfo(list); info.CalculateShippingAmount(State.Alaska) } //Somehow in unity, must i do this for all the concrete classes? //how does it know to give a list. Container.RegisterType<IShippingInfo

How to use Unity.RegisterType with Moq?

☆樱花仙子☆ 提交于 2019-12-03 10:44:33
I have a running code with unity. Now I want to use Moq to do my unit testing for ASP-MVC. In the global.asax's code, I have the following: IUnityContainer container = new UnityContainer(); container.RegisterType<IFoo, Foo>(new InjectionConstructor("xxx")); Now I wrote testcode with Moq: IUnityContainer container = new UnityContainer(); var mockFoo = new Mock<IFoo>(); container.RegisterType<IFoo, mockFoo) >(new InjectionConstructor("xxx")); but this don't work. Error: The type 'Moq.Mock' cannot be used as type parameter 'TTo' in the generic type or method 'Microsoft.Practices.Unity