forked from Cysharp/MagicOnion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Cysharp#187 from Cysharp/ImproveFilterApi
feat: Improve Filter APIs
- Loading branch information
Showing
17 changed files
with
518 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using MagicOnion.Server.Hubs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace MagicOnion.Server | ||
{ | ||
/// <summary> | ||
/// A MagicOnion filter that provided another filter via <see cref="IServiceLocator"/>. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] | ||
public class FromServiceFilterAttribute : Attribute, | ||
IMagicOnionFilterFactory<MagicOnionFilterAttribute>, | ||
IMagicOnionFilterFactory<StreamingHubFilterAttribute> | ||
{ | ||
public Type Type { get; } | ||
|
||
public int Order { get; set; } | ||
|
||
public FromServiceFilterAttribute(Type type) | ||
{ | ||
if (!typeof(MagicOnionFilterAttribute).IsAssignableFrom(type) && | ||
!typeof(StreamingHubFilterAttribute).IsAssignableFrom(type)) | ||
{ | ||
throw new ArgumentException($"{type.FullName} doesn't inherit from MagicOnionFilterAttribute or StreamingHubFilterAttribute.", nameof(type)); | ||
} | ||
|
||
Type = type; | ||
} | ||
|
||
MagicOnionFilterAttribute IMagicOnionFilterFactory<MagicOnionFilterAttribute>.CreateInstance(IServiceLocator serviceLocator) | ||
{ | ||
if (!typeof(MagicOnionFilterAttribute).IsAssignableFrom(Type)) throw new InvalidOperationException($"Type '{Type.FullName}' doesn't inherit from {nameof(MagicOnionFilterAttribute)}."); | ||
return CreateInstance<MagicOnionFilterAttribute>(serviceLocator); | ||
} | ||
|
||
StreamingHubFilterAttribute IMagicOnionFilterFactory<StreamingHubFilterAttribute>.CreateInstance(IServiceLocator serviceLocator) | ||
{ | ||
if (!typeof(StreamingHubFilterAttribute).IsAssignableFrom(Type)) throw new InvalidOperationException($"Type '{Type.FullName}' doesn't inherit from {nameof(StreamingHubFilterAttribute)}."); | ||
return CreateInstance<StreamingHubFilterAttribute>(serviceLocator); | ||
} | ||
|
||
protected T CreateInstance<T>(IServiceLocator serviceLocator) | ||
{ | ||
return (T)serviceLocator.GetService(Type); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using MagicOnion.Server.Hubs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace MagicOnion.Server | ||
{ | ||
/// <summary> | ||
/// A MagicOnion filter that creates another filter of type. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)] | ||
public class FromTypeFilterAttribute : Attribute, | ||
IMagicOnionFilterFactory<MagicOnionFilterAttribute>, | ||
IMagicOnionFilterFactory<StreamingHubFilterAttribute> | ||
{ | ||
public Type Type { get; } | ||
|
||
public int Order { get; set; } | ||
|
||
public object[] Arguments { get; set; } = Array.Empty<object>(); | ||
|
||
public FromTypeFilterAttribute(Type type) | ||
{ | ||
if (!typeof(MagicOnionFilterAttribute).IsAssignableFrom(type) && | ||
!typeof(StreamingHubFilterAttribute).IsAssignableFrom(type)) | ||
{ | ||
throw new ArgumentException($"{type.FullName} doesn't inherit from MagicOnionFilterAttribute or StreamingHubFilterAttribute.", nameof(type)); | ||
} | ||
|
||
Type = type; | ||
} | ||
|
||
MagicOnionFilterAttribute IMagicOnionFilterFactory<MagicOnionFilterAttribute>.CreateInstance(IServiceLocator serviceLocator) | ||
{ | ||
if (!typeof(MagicOnionFilterAttribute).IsAssignableFrom(Type)) throw new InvalidOperationException($"Type '{Type.FullName}' doesn't inherit from {nameof(MagicOnionFilterAttribute)}."); | ||
return CreateInstance<MagicOnionFilterAttribute>(serviceLocator); | ||
} | ||
|
||
StreamingHubFilterAttribute IMagicOnionFilterFactory<StreamingHubFilterAttribute>.CreateInstance(IServiceLocator serviceLocator) | ||
{ | ||
if (!typeof(StreamingHubFilterAttribute).IsAssignableFrom(Type)) throw new InvalidOperationException($"Type '{Type.FullName}' doesn't inherit from {nameof(StreamingHubFilterAttribute)}."); | ||
return CreateInstance<StreamingHubFilterAttribute>(serviceLocator); | ||
} | ||
|
||
protected T CreateInstance<T>(IServiceLocator serviceLocator) | ||
{ | ||
var filterType = Type; | ||
var ctors = filterType.GetConstructors(); | ||
var ctor = ctors.Select(x => (Ctor: x, Parameters: x.GetParameters())) | ||
.OrderByDescending(x => x.Parameters.Length) | ||
.First(); | ||
|
||
var @params = ctor.Parameters | ||
.Select((x, i) => (Arguments.Length > i) ? Arguments[i] : serviceLocator.GetService(x.ParameterType)) | ||
.ToArray(); | ||
|
||
return (T)Activator.CreateInstance(filterType, @params); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace MagicOnion.Server | ||
{ | ||
public interface IMagicOnionFilterFactory<T> | ||
{ | ||
T CreateInstance(IServiceLocator serviceLocator); | ||
int Order { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.