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; // }); }
public void CanRemoveBinding() { var kernel = new StandardKernel(); kernel.Bind<IVegetable>().To<Carrot>(); kernel.Unbind<IVegetable>(); Assert.That(kernel.GetBindings(typeof(IVegetable)).Count(), Is.EqualTo(0)); }
/// <summary> /// Dependency injection configuration. /// </summary> /// <returns>NInject kernel</returns> IKernel PrepareKernel() { IKernel kernel = new StandardKernel(); kernel.Unbind<ModelValidatorProvider>(); // Common kernel.Bind<ISessionFactory>().ToMethod((_) => HibernateUtil.GetSessionFactory()); // Common kernel.Bind<HR.Controllers.HomeController>().ToSelf(); kernel.Bind<HR.Controllers.ImageController>().ToSelf(); // Settings kernel.Bind<HR.Areas.Settings.Controllers.InfoTypeController>().ToSelf(); return kernel; }
static void Main(string[] args) { IKernel kernel = new StandardKernel(); // Example 1 - simple 1 to 1 binding kernel.Bind<IAnimal>().To<Monkey>(); var animal = kernel.Get<IAnimal>(); Console.WriteLine("object is of type {0}", animal.GetType()); kernel.Unbind<IAnimal>(); // Example 2 - simple 1 to 1 binding kernel.Bind<IAnimal>().To<Tiger>(); var animal2 = kernel.Get<IAnimal>(); Console.WriteLine("object is of type {0}", animal2.GetType()); kernel.Unbind<IAnimal>(); // Example 3 - one to many binding string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); kernel.Bind( c => c.FromAssembliesInPath(path).SelectAllClasses().InheritedFrom<IAnimal>().BindAllInterfaces()); kernel.GetAll<IAnimal>().ToList().ForEach( a => Console.WriteLine("{0} implements IAnimal",a.GetType())); kernel.Unbind<IAnimal>(); // Example 4 - inject dependencies into constructor kernel.Bind<IAnimal>().To<Monkey>(); kernel.Bind<ICar>().To<Volvo>(); kernel.Bind( c => c.FromAssembliesInPath(path).SelectAllClasses().InheritedFrom<IServiceA>().BindAllInterfaces()); var serviceA = kernel.Get<IServiceA>(); Console.WriteLine("Service is of type {0} and has an animal of type {1} and a car of type {2}",serviceA.GetType(),serviceA.Animal,serviceA.Car); kernel.Unbind<IAnimal>(); kernel.Unbind<ICar>(); kernel.Unbind<IServiceA>(); // Example 5 - new object versus singleton kernel.Bind<IAnimal>().To<Tiger>(); var tiger1 = kernel.Get<IAnimal>(); var tiger2 = kernel.Get<IAnimal>(); if (tiger1 == tiger2) Console.WriteLine("tiger1 is the same object as tiger2"); else Console.WriteLine("tiger1 is NOT the same object as tiger2"); kernel.Unbind<IAnimal>(); kernel.Bind<IAnimal>().To<Monkey>().InSingletonScope(); tiger1 = kernel.Get<IAnimal>(); tiger2 = kernel.Get<IAnimal>(); if (tiger1 == tiger2) Console.WriteLine("tiger1 is the same object as tiger2"); else Console.WriteLine("tiger1 is NOT the same object as tiger2"); kernel.Unbind<IAnimal>(); // Example 6 - bind to an class that implements two interfaces kernel.Bind<IServiceB, IServiceC>().To<ServiceBC>().InSingletonScope(); var serviceB_impl = kernel.Get<IServiceB>(); var serviceC_impl = kernel.Get<IServiceC>(); Console.ReadKey(); }
internal static void Main(string[] args) { var connectToRunningGame = false; var options = new OptionSet { { "connect", "Internal use only (used by the game client).", v => connectToRunningGame = true } }; try { options.Parse(args); } catch (OptionException ex) { Console.Write("ProtogameAssetManager.exe: "); Console.WriteLine(ex.Message); Console.WriteLine("Try `ProtogameAssetManager.exe --help` for more information."); return; } // Deploy the correct MojoShader DLL. MojoShaderDeploy.Deploy(); var kernel = new StandardKernel(); kernel.Load<Protogame2DIoCModule>(); kernel.Load<ProtogameAssetIoCModule>(); kernel.Load<ProtogameEventsIoCModule>(); kernel.Load<ProtogamePlatformingIoCModule>(); kernel.Load<AssetManagerIoCModule>(); // Only allow the source load strategies. kernel.Unbind<ILoadStrategy>(); kernel.Bind<ILoadStrategy>().To<RawTextureLoadStrategy>(); kernel.Bind<ILoadStrategy>().To<RawModelLoadStrategy>(); kernel.Bind<ILoadStrategy>().To<RawEffectLoadStrategy>(); kernel.Bind<ILoadStrategy>().To<LocalSourceLoadStrategy>(); kernel.Bind<ILoadStrategy>().To<EmbeddedSourceLoadStrategy>(); kernel.Bind<ILoadStrategy>().To<EmbeddedCompiledLoadStrategy>(); var runningFile = new FileInfo(Assembly.GetExecutingAssembly().Location); var workingDirectoryInfo = new DirectoryInfo(Environment.CurrentDirectory); var scannedUnique = new List<string>(); foreach (var file in runningFile.Directory.GetFiles("*.dll")) { if (scannedUnique.Contains(file.FullName)) continue; Console.WriteLine("Scanning " + file.Name); try { RegisterEditorsFromAssembly(Assembly.LoadFrom(file.FullName), kernel); scannedUnique.Add(file.FullName); } catch (BadImageFormatException) { } catch (FileLoadException) { } } foreach (var file in runningFile.Directory.GetFiles("*.exe")) { if (scannedUnique.Contains(file.FullName)) continue; Console.WriteLine("Scanning " + file.Name); try { RegisterEditorsFromAssembly(Assembly.LoadFrom(file.FullName), kernel); scannedUnique.Add(file.FullName); } catch (BadImageFormatException) { } catch (FileLoadException) { } } foreach (var file in workingDirectoryInfo.GetFiles("*.dll")) { if (scannedUnique.Contains(file.FullName)) continue; Console.WriteLine("Scanning " + file.Name); try { RegisterEditorsFromAssembly(Assembly.LoadFrom(file.FullName), kernel); scannedUnique.Add(file.FullName); } catch (BadImageFormatException) { } catch (FileLoadException) { } } foreach (var file in workingDirectoryInfo.GetFiles("*.exe")) { if (scannedUnique.Contains(file.FullName)) continue; Console.WriteLine("Scanning " + file.Name); try { RegisterEditorsFromAssembly(Assembly.LoadFrom(file.FullName), kernel); scannedUnique.Add(file.FullName); } catch (BadImageFormatException) { } catch (FileLoadException) { } } #if FALSE if (connectToRunningGame) { var node = new LocalNode(Architecture.ServerClient, Caching.PushOnChange); node.Bind(IPAddress.Loopback, 9837); node.GetService<IClientConnector>().Connect(IPAddress.Loopback, 9838); var assetManagerProvider = new NetworkedAssetManagerProvider(node, kernel); kernel.Bind<IAssetManagerProvider>().ToMethod(x => assetManagerProvider); } else #endif kernel.Bind<IAssetManagerProvider>().To<LocalAssetManagerProvider>().InSingletonScope(); using (var game = new AssetManagerGame(kernel)) { game.Run(); } }
/// <summary> /// Compiles the built-in embedded resources. /// </summary> private static void BuiltinCompile() { // Create kernel. var kernel = new StandardKernel(); kernel.Load<ProtogameAssetIoCModule>(); kernel.Load<ProtogameScriptIoCModule>(); var services = new GameServiceContainer(); var assetContentManager = new AssetContentManager(services); kernel.Bind<IAssetContentManager>().ToMethod(x => assetContentManager); // Only allow source and raw load strategies. kernel.Unbind<ILoadStrategy>(); kernel.Bind<ILoadStrategy>().To<LocalSourceLoadStrategy>(); var assetModule = new ProtogameAssetIoCModule(); assetModule.LoadRawAssetStrategies(kernel); // Set up remaining bindings. kernel.Bind<IAssetCleanup>().To<DefaultAssetCleanup>(); kernel.Bind<IAssetOutOfDateCalculator>().To<DefaultAssetOutOfDateCalculator>(); kernel.Bind<IAssetCompilationEngine>().To<DefaultAssetCompilationEngine>(); // Rebind for builtin compilation. kernel.Rebind<IRawAssetLoader>().To<BuiltinRawAssetLoader>(); // Set up the compiled asset saver. var compiledAssetSaver = new CompiledAssetSaver(); // Retrieve the asset manager. var assetManager = kernel.Get<LocalAssetManager>(); assetManager.AllowSourceOnly = true; assetManager.SkipCompilation = true; // Retrieve the transparent asset compiler. var assetCompiler = kernel.Get<ITransparentAssetCompiler>(); // Retrieve all of the asset savers. var savers = kernel.GetAll<IAssetSaver>(); var rawLoader = kernel.Get<IRawAssetLoader>(); // For each of the platforms, perform the compilation of assets. foreach (var platformName in new[] { "Android", "iOS", "Linux", "MacOSX", "Ouya", "RaspberryPi", "Windows", "WindowsPhone8", "WindowsStoreApp" }) { Console.WriteLine("Starting compilation for " + platformName); var platform = (TargetPlatform)Enum.Parse(typeof(TargetPlatform), platformName); var outputPath = Environment.CurrentDirectory; assetManager.RescanAssets(); // Create the output directory if it doesn't exist. if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } // Get a list of asset names that we need to recompile for this platform. var assetNames = rawLoader.ScanRawAssets(); foreach (var asset in assetNames.Select(assetManager.GetUnresolved)) { assetCompiler.HandlePlatform(asset, platform, true); foreach (var saver in savers) { var canSave = false; try { canSave = saver.CanHandle(asset); } catch (Exception) { } if (canSave) { try { var result = saver.Handle(asset, AssetTarget.CompiledFile); compiledAssetSaver.SaveCompiledAsset( outputPath, asset.Name, result, result is CompiledAsset, platformName); Console.WriteLine("Compiled " + asset.Name + " for " + platform); break; } catch (Exception ex) { Console.WriteLine("ERROR: Unable to compile " + asset.Name + " for " + platform); Console.WriteLine("ERROR: " + ex.GetType().FullName + ": " + ex.Message); break; } } } assetManager.Dirty(asset.Name); } } }
private static void BulkCompile(List<string> assemblies, List<string> platforms, string output) { // Create kernel. var kernel = new StandardKernel(); kernel.Load<ProtogameAssetIoCModule>(); kernel.Load<ProtogameScriptIoCModule>(); var services = new GameServiceContainer(); var assetContentManager = new AssetContentManager(services); kernel.Bind<IAssetContentManager>().ToMethod(x => assetContentManager); // Only allow source and raw load strategies. kernel.Unbind<ILoadStrategy>(); kernel.Bind<ILoadStrategy>().To<LocalSourceLoadStrategy>(); var assetModule = new ProtogameAssetIoCModule(); assetModule.LoadRawAssetStrategies(kernel); // The assembly load strategy is required for references. // Assets loaded with the assembly load strategy won't have // any savers defined, so they won't ever get processed. kernel.Bind<ILoadStrategy>().To<AssemblyLoadStrategy>(); // Load additional assemblies. foreach (var filename in assemblies) { var file = new FileInfo(filename); try { var assembly = Assembly.LoadFrom(file.FullName); foreach (var type in assembly.GetTypes()) { try { if (type.IsAbstract || type.IsInterface) continue; if (type.Assembly == typeof(FontAsset).Assembly) continue; if (typeof(IAssetLoader).IsAssignableFrom(type)) { Console.WriteLine("Binding IAssetLoader: " + type.Name); kernel.Bind<IAssetLoader>().To(type); } if (typeof(IAssetSaver).IsAssignableFrom(type)) { Console.WriteLine("Binding IAssetSaver: " + type.Name); kernel.Bind<IAssetSaver>().To(type); } if (type.GetInterfaces().Any(x => x.Name == "IAssetCompiler`1")) { Console.WriteLine("Binding IAssetCompiler<>: " + type.Name); kernel.Bind(type.GetInterfaces().First(x => x.Name == "IAssetCompiler`1")).To(type); } if (typeof(ILoadStrategy).IsAssignableFrom(type)) { Console.WriteLine("Binding ILoadStrategy: " + type.Name); kernel.Bind<ILoadStrategy>().To(type); } } catch { // Might not be able to load the assembly, so just skip over it. } } } catch (Exception) { Console.WriteLine("Can't load " + file.Name); } } // Set up remaining bindings. kernel.Bind<IAssetCleanup>().To<DefaultAssetCleanup>(); kernel.Bind<IAssetOutOfDateCalculator>().To<DefaultAssetOutOfDateCalculator>(); kernel.Bind<IAssetCompilationEngine>().To<DefaultAssetCompilationEngine>(); // Get the asset compilation engine. var compilationEngine = kernel.Get<IAssetCompilationEngine>(); compilationEngine.Execute(platforms, output); }