本文整理汇总了C#中Ninject.Core.StandardKernel类的典型用法代码示例。如果您正苦于以下问题:C# StandardKernel类的具体用法?C# StandardKernel怎么用?C# StandardKernel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StandardKernel类属于Ninject.Core命名空间,在下文中一共展示了StandardKernel类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ResultOfMethodCallWithSameArgumentsIsCached
public void ResultOfMethodCallWithSameArgumentsIsCached()
{
IModule testModule = new InlineModule(m => m.Bind<CacheMock>().ToSelf());
using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
{
var obj = kernel.Get<CacheMock>();
CacheMock.ResetCounts();
int result;
result = obj.Multiply(2, 3);
Assert.That(result, Is.EqualTo(6));
Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));
result = obj.Multiply(2, 3);
Assert.That(result, Is.EqualTo(6));
Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));
result = obj.Multiply(4, 4);
Assert.That(result, Is.EqualTo(16));
Assert.That(CacheMock.MultiplyCount, Is.EqualTo(2));
}
}
示例2: MemberBasedBindingOfTypeDependencies
public void MemberBasedBindingOfTypeDependencies()
{
IModule module = new InlineModule(
m => m.Bind(typeof(ObjectWithMethodInterceptor)).ToSelf(),
m => m.Intercept(When.Request.Method.Name.StartsWith("F")).With<CountInterceptor>()
);
using (var kernel = new StandardKernel(module))
{
kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());
var obj = kernel.Get<ObjectWithMethodInterceptor>();
IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));
IRequest request = new StandardRequest(
context,
obj,
typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
new object[0],
Type.EmptyTypes
);
ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
Assert.That(interceptors.Count, Is.EqualTo(2));
IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();
enumerator.MoveNext();
Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
enumerator.MoveNext();
Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
}
}
示例3: TryingToUnloadAModuleThatIsNotLoadedThrowsException
public void TryingToUnloadAModuleThatIsNotLoadedThrowsException()
{
var moduleA = new MockModuleA();
using (var kernel = new StandardKernel())
kernel.Unload(moduleA);
}
示例4: MemberBasedBindingOfTypeDependencies
public void MemberBasedBindingOfTypeDependencies()
{
IModule module = new InlineModule(
m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Member.Tag.EqualTo("A")),
m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Member.Tag.EqualTo("B")),
m => m.Bind(typeof(IMock)).To(typeof(ImplC)).Only(!When.Context.Member.Tag.EqualTo("A") & !When.Context.Member.Tag.EqualTo("B"))
);
using (var kernel = new StandardKernel(module))
{
var obj1 = kernel.Get<RequestsTagA>();
Assert.That(obj1, Is.Not.Null);
Assert.That(obj1.Child, Is.Not.Null);
Assert.That(obj1.Child, Is.InstanceOfType(typeof(ImplA)));
var obj2 = kernel.Get<RequestsTagB>();
Assert.That(obj2, Is.Not.Null);
Assert.That(obj2.Child, Is.Not.Null);
Assert.That(obj2.Child, Is.InstanceOfType(typeof(ImplB)));
var obj3 = kernel.Get<RequestsNoTag>();
Assert.That(obj3, Is.Not.Null);
Assert.That(obj3.Child, Is.Not.Null);
Assert.That(obj3.Child, Is.InstanceOfType(typeof(ImplC)));
}
}
示例5: StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods
public void StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods()
{
var module = new InlineModule(m => m.Bind<ObjectWithMethodInterceptor>().ToSelf());
using (var kernel = new StandardKernel(module))
{
kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());
var obj = kernel.Get<ObjectWithMethodInterceptor>();
IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));
IRequest request = new StandardRequest(
context,
obj,
typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
new object[0],
Type.EmptyTypes
);
ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();
enumerator.MoveNext();
Assert.That(interceptors.Count, Is.EqualTo(1));
Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
}
}
示例6: ManyPublishersOneSubscriber
public void ManyPublishersOneSubscriber()
{
using (var kernel = new StandardKernel(new MessageBrokerModule()))
{
PublisherMock pub1 = kernel.Get<PublisherMock>();
PublisherMock pub2 = kernel.Get<PublisherMock>();
Assert.That(pub1, Is.Not.Null);
Assert.That(pub2, Is.Not.Null);
SubscriberMock sub = kernel.Get<SubscriberMock>();
Assert.That(sub, Is.Not.Null);
Assert.That(pub1.HasListeners);
Assert.That(pub2.HasListeners);
Assert.That(sub.LastMessage, Is.Null);
pub1.SendMessage("Hello, world!");
Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));
sub.LastMessage = null;
Assert.That(sub.LastMessage, Is.Null);
pub2.SendMessage("Hello, world!");
Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));
}
}
示例7: StandardKernelUsesDefaultOptions
public void StandardKernelUsesDefaultOptions()
{
using (var kernel = new StandardKernel())
{
Assert.That(kernel.Options, Is.SameAs(KernelOptions.Default));
}
}
示例8: CanCallParameterlessConstructor
public void CanCallParameterlessConstructor()
{
using (var kernel = new StandardKernel())
{
var mock = kernel.Get<SimpleObject>();
Assert.That(mock, Is.Not.Null);
}
}
示例9: StartableObjectIsStartedWhenActivated
public void StartableObjectIsStartedWhenActivated()
{
using (var kernel = new StandardKernel())
{
var mock = kernel.Get<StartableMock>();
Assert.That(mock, Is.Not.Null);
Assert.That(mock.Started, Is.True);
}
}
示例10: DefiningMultipleDefaultBindingsThrowsException
public void DefiningMultipleDefaultBindingsThrowsException()
{
var module = new InlineModule(
m => m.Bind(typeof(IMock)).To(typeof(ImplA)),
m => m.Bind(typeof(IMock)).To(typeof(ImplB))
);
var kernel = new StandardKernel(module);
}
示例11: Main
public static void Main()
{
IKernel kernel = new StandardKernel(new InlineModule(
m => m.Bind<IReportBuilder>().To<ReportBuilder>(),
m => m.Bind<IReportSender>().To<EmailReportSender>(),
m => m.Bind<IReporter>().To<Reporter>()
));
kernel.Get<IReporter>().SendReports();
}
示例12: SelfBoundTypeReceivesMethodInjection
public void SelfBoundTypeReceivesMethodInjection()
{
using (var kernel = new StandardKernel())
{
var mock = kernel.Get<RequestsMethodInjection>();
Assert.That(mock, Is.Not.Null);
Assert.That(mock.Child, Is.Not.Null);
}
}
示例13: CreateKernel
/*----------------------------------------------------------------------------------------*/
private static IKernel CreateKernel()
{
IKernel kernel = new StandardKernel();
// The test will not pass with just the StandardKernel, because the NullLoggerFactory
// returns the same instance of NullLogger to use as a flyweight.
kernel.Components.Connect<ILoggerFactory>(new ConsoleLoggerFactory());
return kernel;
}
示例14: OptionalDependenciesForServiceBoundTypesAreIgnoredIfNoBindingsExist
public void OptionalDependenciesForServiceBoundTypesAreIgnoredIfNoBindingsExist()
{
using (var kernel = new StandardKernel())
{
RequestsOptionalService parent = kernel.Get<RequestsOptionalService>();
Assert.That(parent, Is.Not.Null);
Assert.That(parent.Child, Is.Null);
}
}
示例15: ConstructorReceivesInjection
public void ConstructorReceivesInjection()
{
using (var kernel = new StandardKernel())
{
var mock = kernel.Get<RequestsConstructorInjection>();
Assert.That(mock, Is.Not.Null);
Assert.That(mock.Child, Is.Not.Null);
Assert.That(mock.Child, Is.InstanceOfType(typeof(SimpleObject)));
}
}