Make IoC Register have where TInterface : class

This commit is contained in:
Pieter-Jan Briers
2022-11-12 12:11:14 +01:00
parent 2017d943fb
commit 2f22de4eff
4 changed files with 14 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ Template for new versions:
* Thanks to new IME support with SDL2, `IClyde.TextInputStart()` and `IClyde.TextInputStop()` must now be appropriately called to start/stop receiving text input when focusing/unfocusing a UI control. This restriction is applied even on the (default) GLFW backend, to enforce consistent usage of these APIs.
* `[GUI]TextEventArgs` have been renamed to `[GUI]TextEnteredEventArgs`, turned into records, and made to carry a `string` rather than a single text `Rune`.
* IoC and `DependencyCollection` `Register` methods now have a `TInterface : class` constraint.
### New features

View File

@@ -87,6 +87,7 @@ namespace Robust.Shared.IoC
/// <inheritdoc />
public void Register<TInterface, TImplementation>(bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
{
Register<TInterface, TImplementation>(() =>
{
@@ -126,6 +127,8 @@ namespace Robust.Shared.IoC
/// <inheritdoc />
public void Register<TInterface, TImplementation>(DependencyFactoryDelegate<TImplementation> factory, bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
{
var interfaceType = typeof(TInterface);
CheckRegisterInterface(interfaceType, typeof(TImplementation), overwrite);
@@ -206,6 +209,7 @@ namespace Robust.Shared.IoC
/// <inheritdoc />
public void RegisterInstance<TInterface>(object implementation, bool overwrite = false, bool deferInject = false)
where TInterface : class
{
RegisterInstance(typeof(TInterface), implementation, overwrite, deferInject);
}

View File

@@ -50,7 +50,8 @@ namespace Robust.Shared.IoC
/// or if an already instantiated interface (by <see cref="DependencyCollection.BuildGraph"/>) is attempting to be overwritten.
/// </exception>
void Register<TInterface, [MeansImplicitUse] TImplementation>(bool overwrite = false)
where TImplementation : class, TInterface;
where TImplementation : class, TInterface
where TInterface : class;
/// <summary>
/// Registers an interface to an implementation, to make it accessible to <see cref="DependencyCollection.Resolve{T}"/>
@@ -68,7 +69,8 @@ namespace Robust.Shared.IoC
/// or if an already instantiated interface (by <see cref="DependencyCollection.BuildGraph"/>) is attempting to be overwritten.
/// </exception>
void Register<TInterface, TImplementation>(DependencyFactoryDelegate<TImplementation> factory, bool overwrite = false)
where TImplementation : class, TInterface;
where TImplementation : class, TInterface
where TInterface : class;
/// <summary>
@@ -113,7 +115,8 @@ namespace Robust.Shared.IoC
/// If this is false, dependencies will be immediately injected. If the registered type requires dependencies
/// that don't exist yet because you have not called BuildGraph, set this to true.
/// </param>
void RegisterInstance<TInterface>(object implementation, bool overwrite = false, bool deferInject = false);
void RegisterInstance<TInterface>(object implementation, bool overwrite = false, bool deferInject = false)
where TInterface : class;
/// <summary>
/// Registers an interface to an existing instance of an implementation,

View File

@@ -101,6 +101,7 @@ namespace Robust.Shared.IoC
/// </exception>
public static void Register<TInterface, [MeansImplicitUse] TImplementation>(bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -141,6 +142,7 @@ namespace Robust.Shared.IoC
/// </exception>
public static void Register<TInterface, TImplementation>(DependencyFactoryDelegate<TImplementation> factory, bool overwrite = false)
where TImplementation : class, TInterface
where TInterface : class
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);
@@ -166,6 +168,7 @@ namespace Robust.Shared.IoC
/// that don't exist yet because you have not called BuildGraph, set this to true.
/// </param>
public static void RegisterInstance<TInterface>(object implementation, bool overwrite = false, bool deferInject = false)
where TInterface : class
{
DebugTools.Assert(_container.IsValueCreated, NoContextAssert);