下面解释如何在代码中执行此操作: Unity将两个接口注册为一个单例
_container.RegisterType<EventService>(new ContainerControlledLifetimeManager());
_container.RegisterType<IEventService, EventService>();
_container.RegisterType<IEventServiceInformation, EventService>();
bool singleton = ReferenceEquals(_container.Resolve<IEventService>(), _container.Resolve<IEventServiceInformation>());
Run Code Online (Sandbox Code Playgroud)
如何在XML配置中执行此操作?
我有一个实现两个接口的类,我想对类的方法应用拦截.
我遵循Unity中的建议将两个接口作为一个单独的接口,但我对结果感到惊讶.简而言之,似乎我的CallHandler被调用了两次.我有一个最简单的例子是:
public interface I1
{
void Method1();
}
public interface I2
{
void Method2();
}
public class C : I1, I2
{
[Log]
public void Method1() {}
public void Method2() {}
}
public class LogAttribute : HandlerAttribute
{
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new LogCallHandler();
}
}
public class LogCallHandler : ICallHandler
{
public IMethodReturn Invoke(
IMethodInvocation input, GetNextHandlerDelegate getNext)
{
Console.WriteLine("Entering " + input.MethodBase.Name);
var methodReturn = getNext().Invoke(input, getNext);
Console.WriteLine("Leaving " + …Run Code Online (Sandbox Code Playgroud) 我无法弄清楚如何使用UnityContainer进行操作.
interface A { }
interface B { }
interface X { }
class ConcreteAX : A, X { }
class ConcreteBX : B, X { }
Run Code Online (Sandbox Code Playgroud)
我需要注册两个具体类,以便ServiceLocator.ResolveAll<X>返回两个实例.同一时间Resolve<A>,也Resolve<B>应该工作.而且,我必须在注册服务时自己实例化它们.
如果我使用命名注册X来创建ResolveAll工作,那么将创建每个具体类的两个实例.如果我用命名注册的所有接口,然后Resolve<A>并Resolve<B>不起作用.如果我使用这种方法然后ResolveAll什么也不返回.
如何使用UnityContainer完成这个技巧?