ioc-container

Method-level attributed interception with Simple Injector

此生再无相见时 提交于 2019-12-05 01:45:25
问题 With Unity, I'm able to quickly add an attribute based interception like this public sealed class MyCacheAttribute : HandlerAttribute, ICallHandler { public override ICallHandler CreateHandler(IUnityContainer container) { return this; } public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { // grab from cache if I have it, otherwise call the intended method call.. } } Then I register with Unity this way: container.RegisterType<IPlanRepository, PlanRepository>(

UnityContainer and internal constructor

喜欢而已 提交于 2019-12-05 01:29:10
I have a class with internal constructor and want to Resolve it from Unity (2.0). public class MyClass { internal MyClass(IService service) { } } then I'm doing _container.Resolve<MyClass>(); when I do so I have an exception Exception is: InvalidOperationException - The type MyClass cannot be constructed. IService is registered and the only problem is that constructor is internal. I really want this class to be public, but I want it to be creatable only via a factory (in which I'm actually calling container.Resolve<MyClass>() ). Is there a way to make Unity see that internal constructor? Like

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

不羁的心 提交于 2019-12-05 00:51:36
问题 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")

Is it possible to use Dependency Injection with xUnit?

陌路散爱 提交于 2019-12-04 23:59:04
I have a test class with a constructor that needs an IService. public class ConsumerTests { private readonly IService _service; public ConsumerTests(IService servie) { _service = service; } [Fact] public void Should_() { //use _service } } I want to plugin my DI container of choice to build the test class . Is this possible with xUnit ? Yes there is now, these two questions and answers should be consolidated in my opinion, see answer here Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc Use Custom Web Application Factory and ServiceProvider

Why use IKernel over IWindsorContainer?

自闭症网瘾萝莉.ら 提交于 2019-12-04 23:32:50
I have seen in several code examples where people have used IKernel rather than use IWindsorContainer . Why is this? Here is one example: http://docs.castleproject.org/(S(kwaa14uzdj55gv55dzgf0vui))/Windsor.Windsor-tutorial-part-two-plugging-Windsor-in.ashx In the above example it came to bite me because I added a subresolver Container.Kernel.Resolver.AddSubResolver( new CollectionResolver(Container.Kernel, true)); that will allow me to inject collections... but yet it wasnt working. I figured out that because just the IKernel was being used it couldnt use the full features of Windsor. Why

Unity Static Factory Extension

别来无恙 提交于 2019-12-04 21:26:12
问题 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()); 回答1: 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

Simple Injector:Factory classes that need to create classes with dependencies

可紊 提交于 2019-12-04 18:30:33
问题 I have a factory class that creates a couple of different types of class. The factory is registered with the container. What is the recommended way of creating the classes inside the factory, given that they also have dependencies. I clearly want to avoid a dependency on the container but if I new those classes then they won't be using the container. e.g. public class MyFactory { public IMyWorker CreateInstance(WorkerType workerType) { if (workerType == WorkerType.A) return new WorkerA

Register Container Itself Using Autofac

♀尐吖头ヾ 提交于 2019-12-04 18:28:31
问题 I was wondering is there's any side effect to registering the container within itself IContainer container; ContainerBuilder builder = new ContainerBuilder(); container = builder.Build(); builder.RegisterInstance(container).As<IContainer>(); and the using it like this builder.RegisterType<IManagmentServiceImp>().As<ManagmentServiceImp>() .WithParameter(new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(IContainer) && pi.Name == "Container", (pi, ctx) => container )); or whether it

Using Unity in WPF

北城以北 提交于 2019-12-04 16:12:53
I have Unity 2.0 working well within the App.xaml.cs to register and resolve within that class. The question I have is regarding a best practice. I have a number of User Controls and other classes that also need to resolve some of the same and new Interface <-> implementations. The problem is theres no way to access the Unity container I created in the App.xaml.cs. I cannot use constructor or property injection to pass on the container reference. Just too many (its a large project) The user controls are added via xaml There are several very loosely related "modules" in the project that can

IOC Container Handling State Params in Non-Default Constructor

混江龙づ霸主 提交于 2019-12-04 13:23:55
问题 For the purpose of this discussion, there are two kinds of parameters an object constructor might take: state dependency or service dependency. Supplying a service dependency with an IOC container is easy: DI takes over. But in contrast, state dependencies are usually only known to the client. That is, the object requestor. It turns out that having a client supply the state params through an IOC Container is quite painful. I will show several different ways to do this, all of which have big