本文整理汇总了C#中Ninject.StandardKernel.Rebind方法的典型用法代码示例。如果您正苦于以下问题:C# StandardKernel.Rebind方法的具体用法?C# StandardKernel.Rebind怎么用?C# StandardKernel.Rebind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ninject.StandardKernel
的用法示例。
在下文中一共展示了StandardKernel.Rebind方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InstallAndRemovePackagesTest
public void InstallAndRemovePackagesTest()
{
using (var kernel = new StandardKernel(new DefaultBindingsModule()))
{
kernel.Rebind<ILog>().To<NullLog>().InSingletonScope();
kernel.Rebind<IClientConfiguration>()
.ToMethod(x => new MockConfiguration(new[] { "Artemida.Tests.TestPackage.Core", "Artemida.Tests.TestPackage.FeatureA", "Artemida.Tests.TestPackage.FeatureB" }))
.InSingletonScope();
var packagesManager = kernel.Get<IPackagesManager>();
var manager = kernel.Get<IClientManager>();
var packages = packagesManager.GetInstalledPackages();
Assert.AreEqual(0, packages.Count);
manager.RefreshPackages();
packages = packagesManager.GetInstalledPackages();
Assert.AreEqual(3, packages.Count);
var config = (MockConfiguration)kernel.Get<IClientConfiguration>();
config.SetPackages(new[] { "Artemida.Tests.TestPackage.Core", "Artemida.Tests.TestPackage.FeatureA" });
manager.RefreshPackages();
packages = packagesManager.GetInstalledPackages();
Assert.AreEqual(2, packages.Count);
}
}
示例2: CreateContainer
public static IKernel CreateContainer()
{
var container = new StandardKernel();
container.Bind(x => x.FromAssemblyContaining<Startup>().SelectAllClasses().BindToSelf());
container.Bind(x => x.FromAssemblyContaining<IFileSystem>().SelectAllClasses().BindAllInterfaces());
container.Bind(x =>
{
x.FromAssemblyContaining<Startup>()
.SelectAllClasses()
.Excluding<CachingMediaSource>()
.BindAllInterfaces();
});
container.Rebind<MediaSourceList>()
.ToMethod(x =>
{
var mediaSources = x.Kernel.GetAll<IMediaSource>();
var sources = new MediaSourceList();
sources.AddRange(mediaSources.Select(s => x.Kernel.Get<CachingMediaSource>().WithSource(s)));
return sources;
});
container.Rebind<ServerConfiguration>()
.ToMethod(x => x.Kernel.Get<ServerConfigurationLoader>().GetConfiguration())
.InSingletonScope();
return container;
}
示例3: BuildDefaultKernelForTests
public static IKernel BuildDefaultKernelForTests(bool copyToServiceLocator = true)
{
var Kernel =
new StandardKernel(
new StandardModule(),
new pMixinsStandardModule());
Kernel.Rebind<IVisualStudioEventProxy>().To<TestVisualStudioEventProxy>().InSingletonScope();
Kernel.Rebind<IVisualStudioWriter>().To<TestVisualStudioWriter>();
Kernel.Rebind<IMicrosoftBuildProjectAssemblyReferenceResolver>()
.To<TestMicrosoftBuildProjectAssemblyReferenceResolver>().InSingletonScope();
Kernel.Rebind<ITaskFactory>().To<TestTaskFactoryWrapper>();
Kernel.Bind<ICodeBehindFileHelper>().To<DummyCodeBehindFileHelper>();
if (copyToServiceLocator)
ServiceLocator.Kernel = Kernel;
LoggingActivity.Initialize(Kernel.Get<IVisualStudioWriter>());
//Make sure the VisualStudioOpenDocumentManager loads early
Kernel.Get<IVisualStudioOpenDocumentManager>();
return Kernel;
}
示例4: RegisterDependencies
private static void RegisterDependencies()
{
IKernel kernel = new StandardKernel(new DefaultModule());
kernel.Rebind<DbContext>().To<LinkknilContext>().InTransientScope();
kernel.Rebind<IEntityWatcher>().To<CustomEntityWatcher>();
kernel.Bind<IFileService>().To<AliyunFileService>();
//kernel.Rebind<IFileService>().To<WebFileService>();
DependencyResolver.SetResolver(new NInjectDependencyResolver(kernel));
}
示例5: CanReplaceBinding
public void CanReplaceBinding()
{
var kernel = new StandardKernel();
kernel.Bind<IVegetable>().To<Carrot>();
kernel.Rebind<IVegetable>().To<GreenBean>();
Assert.That(kernel.Get<IVegetable>(), Is.InstanceOf<GreenBean>());
}
示例6: GetTestService
protected CodeCampService GetTestService()
{
var kernel = new StandardKernel();
Bootstrapper.Configure(kernel);
kernel.Rebind<CCDB>().ToConstant(dbContext);
return kernel.Get<CodeCampService>();
}
示例7: CreateKernel
protected override IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Rebind<CoffeeRequestRepository>().ToConstant(new CoffeeRequestRepository()).InSingletonScope();
return kernel;
}
示例8: Main
static void Main(string[] args)
{
//container is called kernal (because it represents the kernal / core of the application
//var kernal = new StandardKernel();
//kernal.Bind<ICreditCard>().To<MasterCard>();
var kernal = new StandardKernel(new MyModule());
//kernal.Rebind<ICreditCard>().To<MasterCard>();
//dont have to register all the types.. ninject will automatically register concrete types so we can ask for any concrete type and ninject will know how to create it.. without us having to speciy it specificallly in the container.. makes it a little easier so we don't have to configure every single class that we are going to use.
//(dont need to register shopper type because its automatic)
//whenever we ask for an ICreditCard were going to get back a MasterCard.
var shopper = kernal.Get<Shopper>();
shopper.Charge();
Console.WriteLine(shopper.ChargesForCurrentCard);
kernal.Rebind<ICreditCard>().To<MasterCard>();
var shopper2 = kernal.Get<Shopper>();
shopper2.Charge();
Console.WriteLine(shopper2.ChargesForCurrentCard);
//Shopper shopper3 = new Shopper();
//shopper3.Charge();
//Console.WriteLine(shopper3.ChargesForCurrentCard);
Console.Read();
}
示例9: AwardScholarship
/// <summary>
/// Award scholarship
/// </summary>
public void AwardScholarship()
{
// Wrote the resolver myself
var resolver = new Resolver();
var rankBy1 = resolver.ChooseRanking("undergrad");
var award1 = new Award(rankBy1);
award1.AwardScholarship("100");
var rankBy2 = resolver.ChooseRanking("grad");
var award2 = new Award(rankBy2);
award2.AwardScholarship("200");
// using Ninject instead of the custom resolver I wrote.
var kernelContainer = new StandardKernel();
kernelContainer.Bind<IRanking>().To<RankByGPA>();
var award3 = kernelContainer.Get<Award>();
award3.AwardScholarship("101");
kernelContainer.Rebind<IRanking>().To<RankByInnovation>();
var award4 = kernelContainer.Get<Award>();
award4.AwardScholarship("201");
// using Unity instead of custom resolver.
var unityContainer = new UnityContainer();
unityContainer.RegisterType<IRanking, RankByGPA>();
var award5 = unityContainer.Resolve<Award>();
award5.AwardScholarship("102");
unityContainer = new UnityContainer();
unityContainer.RegisterType<IRanking, RankByInnovation>();
var award6 = unityContainer.Resolve<Award>();
award6.AwardScholarship("202");
}
示例10: Configure
public static void Configure(StandardKernel kernel)
{
kernel.Bind(x => x.FromAssembliesMatching("Alejandria.*")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(c => c.InTransientScope()));
kernel.Bind(x => x.FromAssembliesMatching("Framework.*")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(c => c.InTransientScope()));
kernel.Bind(x => x.FromAssembliesMatching("Alejandria.*")
.SelectAllInterfaces()
.EndingWith("Factory")
.BindToFactory()
.Configure(c => c.InSingletonScope()));
kernel.Bind(x => x.FromThisAssembly()
.SelectAllInterfaces()
.Including<IRunAfterLogin>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope()));
kernel.Bind<IIocContainer>().To<NinjectIocContainer>().InSingletonScope();
kernel.Rebind<IClock>().To<Clock>().InSingletonScope();
//kernel.Bind<IMessageBoxDisplayService>().To<MessageBoxDisplayService>().InSingletonScope();
}
示例11: IntegrationTestBase
// Technically we should probably reset this on each test, but we'll always get the same answer and it takes a while to do so ...
// [TestCaseSource] runs before [TestFixtureSetUp] and we need this before even that
// FRAGILE: Duplicate of Web/App_Start/NinjectWebCommon.cs
public IntegrationTestBase()
{
// Kick off Ninject
IKernel kernel = new StandardKernel();
string path = new Uri(Path.GetDirectoryName(typeof(IntegrationTestBase).Assembly.CodeBase) ?? "").LocalPath;
string thisNamespace = typeof(IntegrationTestBase).FullName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0]; // FRAGILE: ASSUME: All our code is in this namespace
kernel.Bind(x => x
.FromAssembliesInPath(path) // Blows with "not marked as serializable": , a => a.FullName.StartsWith( assemblyPrefix ) )
.Select(type => type.IsClass && !type.IsAbstract && type.FullName.StartsWith(thisNamespace)) // .SelectAllClasses() wires up everyone else's stuff too
.BindDefaultInterface()
);
// Add other bindings as necessary
kernel.Rebind<IBetaSigmaPhiContext>().ToMethod(_ => (IBetaSigmaPhiContext)kernel.GetService(typeof(BetaSigmaPhiContext)));
this.InitializeOtherTypes(kernel);
// Initialize the service locator
ServiceLocator.Initialize(kernel.GetService);
// Use ServiceLocator sparingly to start us off
this.SqlHelper = ServiceLocator.GetService<ISqlHelper>();
// Start a transaction so we won't persist data changes made during tests
this.transaction = new TransactionScope();
}
示例12: Main
public static void Main(string[] args)
{
var kernel = new StandardKernel(new sharperbot.Module(), new wwhomper.Module());
kernel.Rebind<ITrashGearStrategy>().To<BasicTrashGearStrategy>();
var p = kernel.Get<Program>();
}
示例13: Configure
public static void Configure(StandardKernel kernel)
{
kernel.Bind(x => x.FromAssembliesMatching("GestionAdministrativa.*")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(c => c.InTransientScope()));
kernel.Bind(x => x.FromAssembliesMatching("Framework.*")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(c => c.InTransientScope()));
kernel.Bind(x => x.FromAssembliesMatching("GestionAdministrativa.*")
.SelectAllInterfaces()
.EndingWith("Factory")
.BindToFactory()
.Configure(c => c.InSingletonScope()));
kernel.Bind(x => x.FromThisAssembly()
.SelectAllInterfaces()
.Including<IRunAfterLogin>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope()));
kernel.Bind<IIocContainer>().To<NinjectIocContainer>().InSingletonScope();
kernel.Rebind<IClock>().To<Clock>().InSingletonScope();
// kernel.Bind<IMessageBoxDisplayService>().To<MessageBoxDisplayService>().InSingletonScope();
//Custom Factories
//kernel.Rebind<ICajaMovientoFactory>().To<CajaMovimientoFactory>();
//Overide defaults bindings.
kernel.Unbind<IGestionAdministrativaContext>();
kernel.Bind<IGestionAdministrativaContext>().To<GestionAdministrativaContext>().InSingletonScope();
kernel.Unbind<IFormRegistry>();
kernel.Bind<IFormRegistry>().To<FormRegistry>().InSingletonScope();
kernel.Unbind<IEncryptionService>();
kernel.Bind<IEncryptionService>().To<EncryptionService>().InSingletonScope();
//kernel.Bind<IRepository<TituloStockMigracion>>().To<EFRepository<TituloStockMigracion>>()
// .WithConstructorArgument("dbContext", (p) =>
// {
// var dbContext = new MigracionLaPazEntities();
// // Do NOT enable proxied entities, else serialization fails
// dbContext.Configuration.ProxyCreationEnabled = false;
// // Load navigation properties explicitly (avoid serialization trouble)
// dbContext.Configuration.LazyLoadingEnabled = false;
// // Because Web API will perform validation, we don't need/want EF to do so
// dbContext.Configuration.ValidateOnSaveEnabled = false;
// return dbContext;
// });
}
示例14: CreateKernel
private static IKernel CreateKernel()
{
var kernel = new StandardKernel(new ServerModule(), new AuthServerModule(), new WcfServiceModule());
kernel.Rebind<NexusConnectionInfo>().ToConstant(GetNexusConnectionInfo());
kernel.Bind<OsWcfConfiguration>().ToConstant(GetWcfConfiguration());
return kernel;
}
示例15: ComparerToCheckIfObjectsShouldBeTheSameShouldBePluggable
public void ComparerToCheckIfObjectsShouldBeTheSameShouldBePluggable()
{
var comparer = Substitute.For<IEqualityComparer<object>>();
var kernel = new StandardKernel(new ObjectDifferModule());
kernel.Rebind<IEqualityComparer<object>>().ToConstant(comparer).Named("SameObjectComparer");
var differ = kernel.Get<IDiffer>();
var a = differ.Diff(new List<int> { 1, 2, 3 }, new List<int> { 2, 3, 4 });
comparer.Received().Equals(Arg.Any<object>(), Arg.Any<object>());
}