using System.IO; using Artemis.Core.Services; using Artemis.Storage; using Artemis.Storage.Migrations.Interfaces; using Artemis.Storage.Repositories.Interfaces; using LiteDB; using Ninject.Activation; using Ninject.Extensions.Conventions; using Ninject.Modules; using Ninject.Planning.Bindings.Resolvers; using Serilog; namespace Artemis.Core.Ninject { /// /// The main of the Artemis Core that binds all services /// public class CoreModule : NinjectModule { /// public override void Load() { if (Kernel == null) throw new ArtemisCoreException("Failed to bind Ninject Core module, kernel is null."); Kernel.Components.Remove(); // Bind all services as singletons Kernel.Bind(x => { x.FromThisAssembly() .IncludingNonPublicTypes() .SelectAllClasses() .InheritedFrom() .BindAllInterfaces() .Configure(c => c.InSingletonScope()); }); // Bind all protected services as singletons Kernel.Bind(x => { x.FromThisAssembly() .IncludingNonPublicTypes() .SelectAllClasses() .InheritedFrom() .BindAllInterfaces() .Configure(c => c.When(HasAccessToProtectedService).InSingletonScope()); }); Kernel.Bind().ToMethod(_ => StorageManager.CreateRepository(Constants.DataFolder)).InSingletonScope(); Kernel.Bind().ToSelf().InSingletonScope(); // Bind all migrations as singletons Kernel.Bind(x => { x.FromAssemblyContaining() .IncludingNonPublicTypes() .SelectAllClasses() .InheritedFrom() .BindAllInterfaces() .Configure(c => c.InSingletonScope()); }); // Bind all repositories as singletons Kernel.Bind(x => { x.FromAssemblyContaining() .IncludingNonPublicTypes() .SelectAllClasses() .InheritedFrom() .BindAllInterfaces() .Configure(c => c.InSingletonScope()); }); Kernel.Bind().ToProvider(); Kernel.Bind().ToProvider(); Kernel.Bind().ToSelf(); } private bool HasAccessToProtectedService(IRequest r) { return r.ParentRequest != null && !r.ParentRequest.Service.Assembly.Location.StartsWith(Path.Combine(Constants.DataFolder, "plugins")); } } }