本文整理汇总了C#中DefaultKernel类的典型用法代码示例。如果您正苦于以下问题:C# DefaultKernel类的具体用法?C# DefaultKernel怎么用?C# DefaultKernel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DefaultKernel类属于命名空间,在下文中一共展示了DefaultKernel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MaxSize
public void MaxSize()
{
IKernel kernel = new DefaultKernel();
kernel.Register(Component.For(typeof(PoolableComponent1)).Named("a"));
var instances = new List<PoolableComponent1>
{
kernel.Resolve<PoolableComponent1>("a"),
kernel.Resolve<PoolableComponent1>("a"),
kernel.Resolve<PoolableComponent1>("a"),
kernel.Resolve<PoolableComponent1>("a"),
kernel.Resolve<PoolableComponent1>("a")
};
PoolableComponent1 other1 = kernel.Resolve<PoolableComponent1>("a");
Assert.IsNotNull(other1);
Assert.IsTrue(!instances.Contains(other1));
foreach (PoolableComponent1 inst in instances)
{
kernel.ReleaseComponent(inst);
}
kernel.ReleaseComponent(other1);
PoolableComponent1 other2 = kernel.Resolve<PoolableComponent1>("a");
Assert.IsNotNull(other2);
Assert.IsTrue(other1 != other2);
Assert.IsTrue(instances.Contains(other2));
kernel.ReleaseComponent(other2);
}
示例2: TransientMultipleConstructorNonValueTypeTest
public void TransientMultipleConstructorNonValueTypeTest()
{
DefaultKernel container = new DefaultKernel();
container.AddComponent("FooBar", typeof(FooBarNonValue));
Tester1 bla1 = new Tester1("FOOBAR");
Tester2 bla2 = new Tester2(666);
Hashtable arguments1 = new Hashtable();
arguments1.Add("test1", bla1);
Hashtable arguments2 = new Hashtable();
arguments2.Add("test2", bla2);
object a = container.Resolve(typeof(FooBarNonValue), arguments1);
object b = container.Resolve(typeof(FooBarNonValue), arguments2);
Assert.AreNotSame(a, b, "A should not be B");
// multi resolve test
a = container.Resolve(typeof(FooBarNonValue), arguments1);
b = container.Resolve(typeof(FooBarNonValue), arguments2);
Assert.AreNotSame(a, b, "A should not be B");
}
示例3: TransientMultipleConstructorNonValueTypeTest
public void TransientMultipleConstructorNonValueTypeTest()
{
DefaultKernel container = new DefaultKernel();
((IKernel)container).Register(Component.For(typeof(FooBarNonValue)).Named("FooBar"));
Tester1 bla1 = new Tester1("FOOBAR");
Tester2 bla2 = new Tester2(666);
var arguments1 = new Dictionary<object, object>();
arguments1.Add("test1", bla1);
var arguments2 = new Dictionary<object, object>();
arguments2.Add("test2", bla2);
object a = container.Resolve(typeof(FooBarNonValue), arguments1);
object b = container.Resolve(typeof(FooBarNonValue), arguments2);
Assert.AreNotSame(a, b, "A should not be B");
// multi resolve test
a = container.Resolve(typeof(FooBarNonValue), arguments1);
b = container.Resolve(typeof(FooBarNonValue), arguments2);
Assert.AreNotSame(a, b, "A should not be B");
}
示例4: SimpleUsage
public void SimpleUsage()
{
IKernel kernel = new DefaultKernel();
kernel.Register(Component.For(typeof(PoolableComponent1)).Named("a"));
PoolableComponent1 inst1 = kernel.Resolve<PoolableComponent1>("a");
PoolableComponent1 inst2 = kernel.Resolve<PoolableComponent1>("a");
Assert.IsNotNull(inst1);
Assert.IsNotNull(inst2);
kernel.ReleaseComponent(inst2);
kernel.ReleaseComponent(inst1);
PoolableComponent1 other1 = kernel.Resolve<PoolableComponent1>("a");
PoolableComponent1 other2 = kernel.Resolve<PoolableComponent1>("a");
Assert.IsNotNull(other1);
Assert.IsNotNull(other2);
Assert.AreSame(inst1, other1);
Assert.AreSame(inst2, other2);
kernel.ReleaseComponent(inst2);
kernel.ReleaseComponent(inst1);
}
示例5: SimpleUsage
public void SimpleUsage()
{
IKernel kernel = new DefaultKernel();
kernel.AddComponent("a", typeof(PoolableComponent1));
PoolableComponent1 inst1 = kernel["a"] as PoolableComponent1;
PoolableComponent1 inst2 = kernel["a"] as PoolableComponent1;
Assert.IsNotNull(inst1);
Assert.IsNotNull(inst2);
kernel.ReleaseComponent(inst2);
kernel.ReleaseComponent(inst1);
PoolableComponent1 other1 = kernel["a"] as PoolableComponent1;
PoolableComponent1 other2 = kernel["a"] as PoolableComponent1;
Assert.IsNotNull(other1);
Assert.IsNotNull(other2);
Assert.AreSame(inst1, other1);
Assert.AreSame(inst2, other2);
kernel.ReleaseComponent(inst2);
kernel.ReleaseComponent(inst1);
}
示例6: Will_give_good_error_message_if_cannot_resolve_service_that_is_likely_decorated
public void Will_give_good_error_message_if_cannot_resolve_service_that_is_likely_decorated()
{
DefaultKernel kernel = new DefaultKernel();
kernel.Register(
Component.For<IRepository>().ImplementedBy<Repository1>(),
Component.For<IRepository>().ImplementedBy<DecoratedRepository2>()
);
try
{
kernel.Resolve<IRepository>();
}
catch (HandlerException e)
{
const string expectedMessage = @"Can't create component 'Castle.MicroKernel.Tests.ClassComponents.Repository1' as it has dependencies to be satisfied.
Castle.MicroKernel.Tests.ClassComponents.Repository1 is waiting for the following dependencies:
Services:
- Castle.MicroKernel.Tests.ClassComponents.IRepository.
A dependency cannot be satisfied by itself, did you forget to add a parameter name to differentiate between the two dependencies?
Castle.MicroKernel.Tests.ClassComponents.DecoratedRepository2 is registered and is matching the required service, but cannot be resolved.
Castle.MicroKernel.Tests.ClassComponents.DecoratedRepository2 is waiting for the following dependencies:
Keys (components with specific keys)
- name which was not registered.
";
Assert.AreEqual(expectedMessage, e.Message);
}
}
示例7: MaxSize
public void MaxSize()
{
IKernel kernel = new DefaultKernel();
kernel.AddComponent("a", typeof(PoolableComponent1));
ArrayList instances = new ArrayList();
instances.Add(kernel["a"] as PoolableComponent1);
instances.Add(kernel["a"] as PoolableComponent1);
instances.Add(kernel["a"] as PoolableComponent1);
instances.Add(kernel["a"] as PoolableComponent1);
instances.Add(kernel["a"] as PoolableComponent1);
PoolableComponent1 other1 = kernel["a"] as PoolableComponent1;
Assert.IsNotNull(other1);
Assert.IsTrue(!instances.Contains(other1));
foreach(object inst in instances)
{
kernel.ReleaseComponent(inst);
}
kernel.ReleaseComponent(other1);
PoolableComponent1 other2 = kernel["a"] as PoolableComponent1;
Assert.IsNotNull(other2);
Assert.IsTrue(other1 != other2);
Assert.IsTrue(instances.Contains(other2));
kernel.ReleaseComponent(other2);
}
示例8: AddingComponentToRootKernelWhenChildKernelHasStartableFacility
public void AddingComponentToRootKernelWhenChildKernelHasStartableFacility()
{
IKernel kernel = new DefaultKernel();
IKernel childKernel = new DefaultKernel();
kernel.AddChildKernel(childKernel);
childKernel.AddFacility("StartableFacility", new StartableFacility());
kernel.AddComponent("string", typeof(String)); // exception here
}
示例9: AddingComponentToRootKernelWhenChildKernelHasStartableFacility
public void AddingComponentToRootKernelWhenChildKernelHasStartableFacility()
{
IKernel kernel = new DefaultKernel();
IKernel childKernel = new DefaultKernel();
kernel.AddChildKernel(childKernel);
childKernel.AddFacility(new StartableFacility());
kernel.Register(Component.For(typeof (A)).Named("string")); // exception here
}
示例10: AddingDependencyToServiceWithCustomDependency
public void AddingDependencyToServiceWithCustomDependency()
{
var kernel = new DefaultKernel();
kernel.Register(Component.For<NeedClassWithCustomerDependency>(),
Component.For<HasCustomDependency>().DependsOn(new Dictionary<object, object> { { "name", new CompA() } }));
Assert.AreEqual(HandlerState.Valid, kernel.GetHandler(typeof(HasCustomDependency)).CurrentState);
Assert.IsNotNull(kernel.Resolve(typeof(NeedClassWithCustomerDependency)));
}
示例11: Init
public void Init()
{
kernel = new DefaultKernel();
kernel.Register(Component.For(typeof(CompA)).Named("compa"));
kernel.Register(Component.For(typeof(CompB)).Named("compb"));
deps = new Dictionary<string, object>();
deps.Add("cc", new CompC(12));
deps.Add("myArgument", "ernst");
}
示例12: DefaultKernel
public void Will_give_good_error_message_if_cannot_resolve_service_that_is_likely_decorated_when_there_are_multiple_service()
{
DefaultKernel kernel = new DefaultKernel();
kernel.Register(
Component.For<IRepository>().ImplementedBy<Repository1>(),
Component.For<IRepository>().ImplementedBy<DecoratedRepository2>().Named("foo"),
Component.For<IRepository>().ImplementedBy<Repository1>().Named("bar")
);
kernel.Resolve<IRepository>();
}
示例13: No_context_uses_transient
public void No_context_uses_transient() {
using (var k = new DefaultKernel()) {
k.Register(Component.For<Dummy>().LifeStyle.HybridPerWebRequestTransient());
var d1 = k.Resolve<Dummy>();
Assert.IsNotNull(d1);
var d2 = k.Resolve<Dummy>();
Assert.IsNotNull(d2);
Assert.AreNotSame(d1, d2);
}
}
示例14: Should_ignore_reference_to_itself
public void Should_ignore_reference_to_itself()
{
DefaultKernel kernel = new DefaultKernel();
kernel.Register(
Component.For<IRepository>().ImplementedBy<Repository1>(),
Component.For<IRepository>().ImplementedBy<DecoratedRepository>()
);
Repository1 repos = (Repository1)kernel.Resolve<IRepository>();
Assert.IsInstanceOfType(typeof(DecoratedRepository), repos.InnerRepository);
}
示例15: ContainerShouldUseFirstRegisteredDependencyOfTypeByDefault_SmsRegisteredFirst
public void ContainerShouldUseFirstRegisteredDependencyOfTypeByDefault_SmsRegisteredFirst()
{
IKernel kernel = new DefaultKernel();
kernel.Register(Component.For(typeof(IAlarmSender)).ImplementedBy(typeof(SmsSender)).Named("sms"));
kernel.Register(Component.For(typeof(IAlarmSender)).ImplementedBy(typeof(EmailSender)).Named("email"));
kernel.Register(Component.For(typeof(AlarmGenerator)).Named("generator"));
var gen = kernel.Resolve<AlarmGenerator>("generator");
Assert.AreEqual(typeof(SmsSender), gen.Sender.GetType());
}