Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review ninjecet #3

Merged
merged 2 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed .vs/Neuron/DesignTimeBuild/.dtbcache.v2
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Empty file.
Binary file removed .vs/Neuron/v17/.futdcache.v1
Binary file not shown.
Binary file removed .vs/Neuron/v17/TestStore/0/000.testlog
Binary file not shown.
Binary file removed .vs/Neuron/v17/TestStore/0/testlog.manifest
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/neuron.metadata.v2
Binary file not shown.
Binary file removed .vs/ProjectEvaluation/neuron.projects.v2
Binary file not shown.
12 changes: 10 additions & 2 deletions Neuron.Core/NeuronImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Neuron.Core.Plugins;
using Neuron.Core.Scheduling;
using Ninject;
using Ninject.Planning.Bindings.Resolvers;

namespace Neuron.Core
{
Expand All @@ -36,7 +37,14 @@ public override void Start()
}
Kernel.BindSimple<NeuronBase>(this);
Kernel.BindSimple(Configuration);


if (!Platform.Configuration.NinjectGenerateDefaultBindings)
{
Kernel.Components.Remove<IMissingBindingResolver, SelfBindingResolver>();
Kernel.Components.Add<IMissingBindingResolver, NullableBindingResolver>();
Kernel.Settings.AllowNullInjection = true;
}

if (Platform.Configuration.OverrideConsoleEncoding) Console.OutputEncoding = Encoding.UTF8;
if (Platform.Configuration.FileIo)
{
Expand Down Expand Up @@ -149,7 +157,7 @@ public void LoadIoPlugins()
}
}
}

public override void Stop()
{
Kernel.Get<PluginManager>().UnloadAll();
Expand Down
76 changes: 76 additions & 0 deletions Neuron.Core/NullableBindingResolver.cs
Original file line number Diff line number Diff line change
@@ -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;
}

/// <summary>
/// Returns any bindings from the specified collection that match the specified service.
/// </summary>
/// <param name="bindings">
/// The multimap of all registered bindings.
/// </param>
/// <param name="request">
/// The series of matching bindings.
/// </param>
/// <returns>
/// The series of matching bindings.
/// </returns>
public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, IRequest request)
{
var service = request.Service;
if (!TypeIsSelfBindable(service))
{
return Enumerable.Empty<IBinding>();
}

var provider = new NullProvider(service);

return new Binding[1]
{
new Binding(service)
{
ProviderCallback = (IContext _) => provider
}
};
}

/// <summary>
/// Returns a value indicating whether the specified service is self-bindable.
/// </summary>
/// <param name="service">
/// The service.
/// </param>
/// <returns>
/// True if the type is self-bindable; otherwise false.
/// </returns>
protected virtual bool TypeIsSelfBindable(Type service)
{
if (!service.IsInterface && !service.IsAbstract && !service.IsValueType && service != typeof(string))
{
return !service.ContainsGenericParameters;
}

return false;
}
}
7 changes: 7 additions & 0 deletions Neuron.Core/Platform/PlatformConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public class PlatformConfiguration
/// </summary>
public bool UseGlobals { get; set; } = true;

/// <summary>
/// By default Ninject generate a default binding when no binding is found.
/// This default binding create a new inistance of the requested type.
/// If <see langword="false"/>, Ninject will return <see langword="null"/> when the type is requested.
/// </summary>
public bool NinjectGenerateDefaultBindings { get; set; } = true;

/// <summary>
/// Also writes events to this consumer instead of only the console and possibly a logfile
/// </summary>
Expand Down
4 changes: 4 additions & 0 deletions Neuron.Tests.Commands/Neuron.Tests.Commands.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

<ItemGroup>
<PackageReference Include="xunit" Version="2.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Neuron.Tests.Configs/Neuron.Tests.Configs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions Neuron.Tests.Core/Neuron.Tests.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

<ItemGroup>
<PackageReference Include="xunit" Version="2.1.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading