Skip to content

Commit

Permalink
Don't use reflection-based ParameterInfo for service injection
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden committed Apr 24, 2024
1 parent 288d8aa commit 2b8a773
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 19 deletions.
3 changes: 2 additions & 1 deletion Bunkum.AutoDiscover/AutoDiscoverService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Bunkum.Core;
using Bunkum.Core.Database;
using Bunkum.Core.Services;
using Bunkum.Listener.Request;
Expand All @@ -20,7 +21,7 @@ internal AutoDiscoverService(Logger logger, AutoDiscoverConfig config) : base(lo
}

/// <inheritdoc/>
public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
if (parameter.ParameterType == typeof(AutoDiscoverConfig))
{
Expand Down
40 changes: 40 additions & 0 deletions Bunkum.Core/BunkumParameterInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Reflection;

namespace Bunkum.Core;

/// <summary>
/// A parameter used for injection information.
/// </summary>
/// <seealso cref="ParameterInfo"/>
public class BunkumParameterInfo
{
/// <summary>
/// Instantiates a set of parameter info.
/// </summary>
/// <param name="parameterType">The parameter's type.</param>
/// <param name="name">The parameter's name.</param>
public BunkumParameterInfo(Type parameterType, string name)
{
this.ParameterType = parameterType;
this.Name = name;
}

/// <summary>
/// Instantiates a set of parameter info from existing reflection data.
/// </summary>
/// <param name="parameter">The reflection data to use.</param>
public BunkumParameterInfo(ParameterInfo parameter)
{
this.ParameterType = parameter.ParameterType;
this.Name = parameter.Name;
}

/// <summary>
/// The type of the parameter.
/// </summary>
public Type ParameterType { get; }
/// <summary>
/// The name of the parameter.
/// </summary>
public string? Name { get; }
}
2 changes: 1 addition & 1 deletion Bunkum.Core/Endpoints/Middlewares/MainMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Response GenerateResponseFromEndpoint(object? val, EndpointAttribute att
while (arg == null)
{
if (!services.MoveNext()) break;
arg = services.Current.AddParameterToEndpoint(context, param, database);
arg = services.Current.AddParameterToEndpoint(context, new BunkumParameterInfo(param), database);
}

services.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion Bunkum.Core/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public override void Initialize()
}

/// <inheritdoc />
public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
if(ParameterBasedFrom<IToken<IUser>>(parameter))
return this.AuthenticateToken(context, database);
Expand Down
2 changes: 1 addition & 1 deletion Bunkum.Core/Services/EndpointService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected EndpointService(Logger logger) : base(logger)
{}

/// <inheritdoc/>
public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
if (parameter.ParameterType.IsAssignableTo(this.GetType()))
return this;
Expand Down
19 changes: 16 additions & 3 deletions Bunkum.Core/Services/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,28 @@ public virtual void Initialize()
/// </summary>
public virtual void AfterRequestHandled(ListenerContext context, Response response, MethodInfo method, Lazy<IDatabaseContext> database)
{}

/// <summary>
/// Called when the endpoint is looking for a custom parameter. You can provide one using this method.
/// </summary>
/// <param name="context">The context of the request.</param>
/// <param name="parameter">Information about the parameter being processed.</param>
/// <param name="database">The database.</param>
/// <returns>Null if this service has no suitable object for this parameter, otherwise the object to add.</returns>
[Obsolete($"Using {nameof(ParameterInfo)} is no longer supported. Please switch to using {nameof(BunkumParameterInfo)} (another overload of this method)")]
public virtual object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
{
return this.AddParameterToEndpoint(context, new BunkumParameterInfo(parameter), database);
}

/// <summary>
/// Called when the endpoint is looking for a custom parameter. You can provide one using this method.
/// </summary>
/// <param name="context">The context of the request.</param>
/// <param name="parameter">Information about the parameter being processed.</param>
/// <param name="database">The database.</param>
/// <returns>Null if this service has no suitable object for this parameter, otherwise the object to add.</returns>
public virtual object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
return null;
}
Expand All @@ -55,7 +68,7 @@ public virtual void AfterRequestHandled(ListenerContext context, Response respon
/// <param name="paramInfo">The parameter in question.</param>
/// <typeparam name="TExtendable">The type, usually an interface or abstract class.</typeparam>
/// <returns>true if the parameter's type extends the other type; false if not.</returns>
protected static bool ParameterBasedFrom<TExtendable>(ParameterInfo paramInfo)
protected static bool ParameterBasedFrom<TExtendable>(BunkumParameterInfo paramInfo)
=> paramInfo.ParameterType.IsAssignableTo(typeof(TExtendable));

/// <summary>
Expand All @@ -64,6 +77,6 @@ protected static bool ParameterBasedFrom<TExtendable>(ParameterInfo paramInfo)
/// <param name="paramInfo">The parameter in question.</param>
/// <typeparam name="TOther">The type to check against.</typeparam>
/// <returns>true if the parameter's type is equal to the other type; false if not.</returns>
protected static bool ParameterEqualTo<TOther>(ParameterInfo paramInfo)
protected static bool ParameterEqualTo<TOther>(BunkumParameterInfo paramInfo)
=> paramInfo.ParameterType == typeof(TOther);
}
4 changes: 2 additions & 2 deletions Bunkum.Core/Services/StorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ internal StorageService(Logger logger, IDataStore dataStore) : base(logger)
{
this._dataStore = dataStore;
}

public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
if(ParameterBasedFrom<IDataStore>(parameter))
return this._dataStore;
Expand Down
12 changes: 4 additions & 8 deletions Bunkum.HealthChecks/HealthService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Bunkum.Core;
using Bunkum.Core.Database;
using Bunkum.Core.Services;
using Bunkum.Listener.Request;
Expand All @@ -15,15 +16,10 @@ public HealthService(Logger logger, IEnumerable<IHealthCheck> checks) : base(log
this._healthChecks = checks.ToList();
}

public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
// Only pass into endpoint inside Bunkum
Assembly declaringAssembly = parameter.Member.DeclaringType!.Assembly;
if (declaringAssembly.Equals(Assembly.GetAssembly(typeof(HealthService))))
{
if (parameter.ParameterType.IsAssignableFrom(typeof(HealthReport)))
return this.GenerateReport();
}
if (parameter.ParameterType.IsAssignableFrom(typeof(HealthReport)))
return this.GenerateReport();

return base.AddParameterToEndpoint(context, parameter, database);
}
Expand Down
2 changes: 1 addition & 1 deletion BunkumGeminiSampleApplication/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TimeService : Service
internal TimeService(Logger logger) : base(logger)
{}

public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
this.Logger.LogDebug(BunkumCategory.Service, $"TimeService is attempting to pass something in for `{parameter.ParameterType.Name} {parameter.Name}`");
if (parameter.ParameterType == typeof(DateTimeOffset))
Expand Down
2 changes: 1 addition & 1 deletion BunkumSampleApplication/Services/TimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TimeService : Service
internal TimeService(Logger logger) : base(logger)
{}

public override object? AddParameterToEndpoint(ListenerContext context, ParameterInfo parameter, Lazy<IDatabaseContext> database)
public override object? AddParameterToEndpoint(ListenerContext context, BunkumParameterInfo parameter, Lazy<IDatabaseContext> database)
{
this.Logger.LogDebug(BunkumCategory.Service, $"TimeService is attempting to pass something in for `{parameter.ParameterType.Name} {parameter.Name}`");
if (parameter.ParameterType == typeof(DateTimeOffset))
Expand Down

0 comments on commit 2b8a773

Please sign in to comment.