diff --git a/.vs/Neuron/DesignTimeBuild/.dtbcache.v2 b/.vs/Neuron/DesignTimeBuild/.dtbcache.v2 deleted file mode 100644 index 3baee84..0000000 Binary files a/.vs/Neuron/DesignTimeBuild/.dtbcache.v2 and /dev/null differ diff --git a/.vs/Neuron/FileContentIndex/210ea504-560e-4d60-94cd-02411a0946b4.vsidx b/.vs/Neuron/FileContentIndex/210ea504-560e-4d60-94cd-02411a0946b4.vsidx deleted file mode 100644 index 5e1c784..0000000 Binary files a/.vs/Neuron/FileContentIndex/210ea504-560e-4d60-94cd-02411a0946b4.vsidx and /dev/null differ diff --git a/.vs/Neuron/FileContentIndex/85cc93fd-a7b7-4bd0-b050-09cae2e6290a.vsidx b/.vs/Neuron/FileContentIndex/85cc93fd-a7b7-4bd0-b050-09cae2e6290a.vsidx deleted file mode 100644 index f765c5e..0000000 Binary files a/.vs/Neuron/FileContentIndex/85cc93fd-a7b7-4bd0-b050-09cae2e6290a.vsidx and /dev/null differ diff --git a/.vs/Neuron/FileContentIndex/e268a164-c69f-478d-b06c-24869b03a1d9.vsidx b/.vs/Neuron/FileContentIndex/e268a164-c69f-478d-b06c-24869b03a1d9.vsidx deleted file mode 100644 index f765c5e..0000000 Binary files a/.vs/Neuron/FileContentIndex/e268a164-c69f-478d-b06c-24869b03a1d9.vsidx and /dev/null differ diff --git a/.vs/Neuron/FileContentIndex/read.lock b/.vs/Neuron/FileContentIndex/read.lock deleted file mode 100644 index e69de29..0000000 diff --git a/.vs/Neuron/v17/.futdcache.v1 b/.vs/Neuron/v17/.futdcache.v1 deleted file mode 100644 index 18f695b..0000000 Binary files a/.vs/Neuron/v17/.futdcache.v1 and /dev/null differ diff --git a/.vs/Neuron/v17/TestStore/0/000.testlog b/.vs/Neuron/v17/TestStore/0/000.testlog deleted file mode 100644 index b5756d0..0000000 Binary files a/.vs/Neuron/v17/TestStore/0/000.testlog and /dev/null differ diff --git a/.vs/Neuron/v17/TestStore/0/testlog.manifest b/.vs/Neuron/v17/TestStore/0/testlog.manifest deleted file mode 100644 index e92ede2..0000000 Binary files a/.vs/Neuron/v17/TestStore/0/testlog.manifest and /dev/null differ diff --git a/.vs/ProjectEvaluation/neuron.metadata.v2 b/.vs/ProjectEvaluation/neuron.metadata.v2 deleted file mode 100644 index 8fcc511..0000000 Binary files a/.vs/ProjectEvaluation/neuron.metadata.v2 and /dev/null differ diff --git a/.vs/ProjectEvaluation/neuron.projects.v2 b/.vs/ProjectEvaluation/neuron.projects.v2 deleted file mode 100644 index 229356e..0000000 Binary files a/.vs/ProjectEvaluation/neuron.projects.v2 and /dev/null differ diff --git a/Neuron.Core/NeuronImpl.cs b/Neuron.Core/NeuronImpl.cs index 8d2f530..b10f7ff 100644 --- a/Neuron.Core/NeuronImpl.cs +++ b/Neuron.Core/NeuronImpl.cs @@ -14,6 +14,7 @@ using Neuron.Core.Plugins; using Neuron.Core.Scheduling; using Ninject; +using Ninject.Planning.Bindings.Resolvers; namespace Neuron.Core { @@ -36,7 +37,14 @@ public override void Start() } Kernel.BindSimple(this); Kernel.BindSimple(Configuration); - + + if (!Platform.Configuration.NinjectGenerateDefaultBindings) + { + Kernel.Components.Remove(); + Kernel.Components.Add(); + Kernel.Settings.AllowNullInjection = true; + } + if (Platform.Configuration.OverrideConsoleEncoding) Console.OutputEncoding = Encoding.UTF8; if (Platform.Configuration.FileIo) { @@ -149,7 +157,7 @@ public void LoadIoPlugins() } } } - + public override void Stop() { Kernel.Get().UnloadAll(); diff --git a/Neuron.Core/NullableBindingResolver.cs b/Neuron.Core/NullableBindingResolver.cs new file mode 100644 index 0000000..0530f00 --- /dev/null +++ b/Neuron.Core/NullableBindingResolver.cs @@ -0,0 +1,76 @@ +using Ninject.Activation.Providers; +using Ninject.Activation; +using Ninject.Components; +using Ninject.Infrastructure; +using Ninject.Planning.Bindings; +using Ninject.Planning.Bindings.Resolvers; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Neuron.Core; + +public class NullableBindingResolver : NinjectComponent, IMissingBindingResolver, INinjectComponent, IDisposable +{ + public class NullProvider : IProvider + { + public NullProvider(Type prototype) + { + Type = prototype; + } + + public Type Type { get; private set; } + + public object Create(IContext context) => null; + } + + /// + /// Returns any bindings from the specified collection that match the specified service. + /// + /// + /// The multimap of all registered bindings. + /// + /// + /// The series of matching bindings. + /// + /// + /// The series of matching bindings. + /// + public IEnumerable Resolve(Multimap bindings, IRequest request) + { + var service = request.Service; + if (!TypeIsSelfBindable(service)) + { + return Enumerable.Empty(); + } + + var provider = new NullProvider(service); + + return new Binding[1] + { + new Binding(service) + { + ProviderCallback = (IContext _) => provider + } + }; + } + + /// + /// Returns a value indicating whether the specified service is self-bindable. + /// + /// + /// The service. + /// + /// + /// True if the type is self-bindable; otherwise false. + /// + protected virtual bool TypeIsSelfBindable(Type service) + { + if (!service.IsInterface && !service.IsAbstract && !service.IsValueType && service != typeof(string)) + { + return !service.ContainsGenericParameters; + } + + return false; + } +} diff --git a/Neuron.Core/Platform/PlatformConfiguration.cs b/Neuron.Core/Platform/PlatformConfiguration.cs index 9c9f597..daa8461 100644 --- a/Neuron.Core/Platform/PlatformConfiguration.cs +++ b/Neuron.Core/Platform/PlatformConfiguration.cs @@ -26,6 +26,13 @@ public class PlatformConfiguration /// public bool UseGlobals { get; set; } = true; + /// + /// By default Ninject generate a default binding when no binding is found. + /// This default binding create a new inistance of the requested type. + /// If , Ninject will return when the type is requested. + /// + public bool NinjectGenerateDefaultBindings { get; set; } = true; + /// /// Also writes events to this consumer instead of only the console and possibly a logfile /// diff --git a/Neuron.Tests.Commands/Neuron.Tests.Commands.csproj b/Neuron.Tests.Commands/Neuron.Tests.Commands.csproj index b73cdb6..26565cf 100644 --- a/Neuron.Tests.Commands/Neuron.Tests.Commands.csproj +++ b/Neuron.Tests.Commands/Neuron.Tests.Commands.csproj @@ -14,6 +14,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Neuron.Tests.Configs/Neuron.Tests.Configs.csproj b/Neuron.Tests.Configs/Neuron.Tests.Configs.csproj index dce9dc9..2a76f4f 100644 --- a/Neuron.Tests.Configs/Neuron.Tests.Configs.csproj +++ b/Neuron.Tests.Configs/Neuron.Tests.Configs.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Neuron.Tests.Core/Neuron.Tests.Core.csproj b/Neuron.Tests.Core/Neuron.Tests.Core.csproj index 0e9e982..f859c75 100644 --- a/Neuron.Tests.Core/Neuron.Tests.Core.csproj +++ b/Neuron.Tests.Core/Neuron.Tests.Core.csproj @@ -18,6 +18,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive +