本文整理汇总了C#中UnityContainer.AddNewExtension方法的典型用法代码示例。如果您正苦于以下问题:C# UnityContainer.AddNewExtension方法的具体用法?C# UnityContainer.AddNewExtension怎么用?C# UnityContainer.AddNewExtension使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityContainer
的用法示例。
在下文中一共展示了UnityContainer.AddNewExtension方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CanConfigureRemotingInterceptionOnInterface
public void CanConfigureRemotingInterceptionOnInterface()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.Configure<Interception>().SetInterceptorFor<Interface>(new TransparentProxyInterceptor());
}
示例2: CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers
public void CanSetUpAPolicyWithExternallyConfiguredRulesAndHandlers()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container
.Configure<Interception>()
.AddPolicy("policy1")
.AddMatchingRule("rule1")
.AddCallHandler("handler1")
.AddCallHandler("handler2").Interception.Container
.RegisterType<IMatchingRule, AlwaysMatchingRule>("rule1")
.RegisterType<ICallHandler, GlobalCountCallHandler>(
"handler1",
new InjectionConstructor("handler1"))
.RegisterType<ICallHandler, GlobalCountCallHandler>(
"handler2",
new InjectionConstructor("handler2"),
new InjectionProperty("Order", 10));
GlobalCountCallHandler.Calls.Clear();
container
.Configure<Interception>()
.SetInterceptorFor<Wrappable>("wrappable", new VirtualMethodInterceptor());
Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
wrappable1.Method2();
Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler1"]);
Assert.AreEqual(1, GlobalCountCallHandler.Calls["handler2"]);
}
示例3: CanAddExtensionWithNonDefaultConstructor
public void CanAddExtensionWithNonDefaultConstructor()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<ContainerExtensionWithNonDefaultConstructor>();
var extension = container.Configure(typeof (ContainerExtensionWithNonDefaultConstructor));
Assert.IsNotNull(extension);
}
示例4: CanUseContainerToResolveFactoryParameters
public void CanUseContainerToResolveFactoryParameters()
{
bool factoryWasCalled = false;
string connectionString = "Northwind";
IUnityContainer container = new UnityContainer();
container.AddNewExtension<StaticFactoryExtension>()
.Configure<IStaticFactoryConfiguration>()
.RegisterFactory<MockDatabase>(c =>
{
Assert.AreSame(container, c);
factoryWasCalled = true;
string cs = c.Resolve<string>("connectionString");
return MockDatabase.Create(cs);
})
.Container
.RegisterInstance<string>("connectionString", connectionString);
MockDatabase db = container.Resolve<MockDatabase>();
Assert.IsTrue(factoryWasCalled);
Assert.IsNotNull(db);
Assert.AreEqual(connectionString, db.ConnectionString);
}
示例5: CanConfigureDefaultRemotingInterceptionOnMBRO
public void CanConfigureDefaultRemotingInterceptionOnMBRO()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.Configure<Interception>()
.SetDefaultInterceptorFor<Wrappable>(new TransparentProxyInterceptor());
}
示例6: CanInterceptGenericMethodWithHandlerAttributeThroughInterface
public void CanInterceptGenericMethodWithHandlerAttributeThroughInterface()
{
var container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<IInterfaceWithGenericMethod, ClassWithGenericMethod>(
new Interceptor(new TransparentProxyInterceptor()));
var instance = container.Resolve<IInterfaceWithGenericMethod>();
var result = instance.DoSomething<int>();
Assert.AreEqual(0, result);
}
示例7: SettingUpRuleWithEmptyNameThrows
public void SettingUpRuleWithEmptyNameThrows()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
try
{
container.Configure<Interception>().AddPolicy(string.Empty);
Assert.Fail("should have thrown");
}
catch (ArgumentException)
{
}
}
示例8: AttributeDrivenPolicyIsAddedByDefault
public void AttributeDrivenPolicyIsAddedByDefault()
{
GlobalCountCallHandler.Calls.Clear();
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<Interface, WrappableThroughInterfaceWithAttributes>(
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
Interface wrappedOverInterface = container.Resolve<Interface>();
wrappedOverInterface.Method();
Assert.AreEqual(1, GlobalCountCallHandler.Calls["WrappableThroughInterfaceWithAttributes-Method"]);
}
示例9: ConfiguringDefaultRemotingInterceptionOnNonMBROTypeThrows
public void ConfiguringDefaultRemotingInterceptionOnNonMBROTypeThrows()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
try
{
container.Configure<Interception>()
.SetDefaultInterceptorFor<WrappableThroughInterface>(new TransparentProxyInterceptor());
Assert.Fail("Call to SetInjectorFor<T>() should have thrown");
}
catch (ArgumentException)
{
// expected exception
}
}
示例10: CanSetUpInterceptorThroughInjectionMemberForExistingInterceptor
public void CanSetUpInterceptorThroughInjectionMemberForExistingInterceptor()
{
CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.RegisterType<IInterface, BaseClass>(
"test",
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior(interceptionBehavior));
IInterface instance = container.Resolve<IInterface>("test");
instance.DoSomething("1");
Assert.AreEqual(1, interceptionBehavior.CallCount);
}
示例11: CanSetUpAnEmptyRule
public void CanSetUpAnEmptyRule()
{
// there is no visible effect for this, but it should still be resolved.
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
// empty
container
.Configure<Interception>()
.AddPolicy("policy1");
List<InjectionPolicy> policies
= new List<InjectionPolicy>(container.ResolveAll<InjectionPolicy>());
Assert.AreEqual(2, policies.Count);
Assert.IsInstanceOfType(policies[0], typeof(AttributeDrivenPolicy));
Assert.IsInstanceOfType(policies[1], typeof(RuleDrivenPolicy));
Assert.AreEqual("policy1", policies[1].Name);
}
示例12: CanSetUpAdditionalInterfaceThroughInjectionMemberForInstanceInterception
public void CanSetUpAdditionalInterfaceThroughInjectionMemberForInstanceInterception()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
int invokeCount = 0;
container.RegisterType<IInterface, BaseClass>(
"test",
new Interceptor<InterfaceInterceptor>(),
new AdditionalInterface(typeof(IOtherInterface)),
new InterceptionBehavior(
new DelegateInterceptionBehavior(
(mi, gn) => { invokeCount++; return mi.CreateMethodReturn(0); })));
IInterface instance = container.Resolve<IInterface>("test");
((IOtherInterface)instance).DoSomethingElse("1");
Assert.AreEqual(1, invokeCount);
}
示例13: CanSetUpInterceptorThroughInjectionMember
public void CanSetUpInterceptorThroughInjectionMember()
{
CallCountHandler handler = new CallCountHandler();
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container.Configure<Interception>()
.AddPolicy("policy")
.AddMatchingRule<AlwaysMatchingRule>()
.AddCallHandler(handler);
container.RegisterType<IInterface, BaseClass>(
"test",
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<PolicyInjectionBehavior>());
IInterface instance = container.Resolve<IInterface>("test");
instance.DoSomething("1");
Assert.AreEqual(1, handler.CallCount);
}
示例14: CanSetUpAPolicyWithGivenRulesAndHandlers
public void CanSetUpAPolicyWithGivenRulesAndHandlers()
{
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
IMatchingRule rule1 = new AlwaysMatchingRule();
ICallHandler handler1 = new CallCountHandler();
container
.Configure<Interception>()
.AddPolicy("policy1")
.AddMatchingRule(rule1)
.AddCallHandler(handler1);
container
.Configure<Interception>()
.SetInterceptorFor<Wrappable>("wrappable", new VirtualMethodInterceptor());
Wrappable wrappable1 = container.Resolve<Wrappable>("wrappable");
wrappable1.Method2();
Assert.AreEqual(1, ((CallCountHandler)handler1).CallCount);
}
示例15: CanInterceptMBROWithDependencyOnOtherMBRO
public void CanInterceptMBROWithDependencyOnOtherMBRO()
{
GlobalCountCallHandler.Calls.Clear();
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Interception>();
container
.RegisterInstance<IMatchingRule>(
"parentRule",
new TypeMatchingRule(typeof(WrappableWithProperty)))
.RegisterInstance<IMatchingRule>(
"childRule",
new TypeMatchingRule(typeof(Wrappable)))
.RegisterInstance<ICallHandler>(
"parentCallHandler",
new GlobalCountCallHandler("parent"))
.RegisterInstance<ICallHandler>(
"childCallHandler",
new GlobalCountCallHandler("child"))
.RegisterType<InjectionPolicy, RuleDrivenPolicy>("parentPolicy",
new InjectionConstructor(
new ResolvedArrayParameter<IMatchingRule>(
new ResolvedParameter<IMatchingRule>("parentRule")),
new string[] { "parentCallHandler" }))
.RegisterType<InjectionPolicy, RuleDrivenPolicy>("childPolicy",
new InjectionConstructor(
new ResolvedArrayParameter<IMatchingRule>(
new ResolvedParameter<IMatchingRule>("childRule")),
new string[] { "childCallHandler" }))
.RegisterType<WrappableWithProperty>(new InjectionProperty("Wrappable"))
.Configure<Interception>()
.SetDefaultInterceptorFor<WrappableWithProperty>(new TransparentProxyInterceptor())
.SetDefaultInterceptorFor<Wrappable>(new TransparentProxyInterceptor());
WrappableWithProperty instance = container.Resolve<WrappableWithProperty>();
instance.Method();
Assert.AreEqual(1, GlobalCountCallHandler.Calls["parent"]); // method
instance.Wrappable.Method();
Assert.AreEqual(2, GlobalCountCallHandler.Calls["parent"]); // method and getter
Assert.AreEqual(1, GlobalCountCallHandler.Calls["child"]);
}