Skip to content

Commit

Permalink
[r] types find
Browse files Browse the repository at this point in the history
[add] controller version 1 types
  • Loading branch information
i4004 committed May 5, 2024
1 parent 73e65c4 commit 068c6f4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using Simplify.Web.Meta.Controllers;
using Simplify.Web.Meta.Controllers.Factory;
using Simplify.Web.System;

namespace Simplify.Web.Controllers.V1.Metadata;

public class Controller1MetadataFactory : IControllerMetadataFactory
{
public bool CanHandle(Type controllerType) => controllerType.IsTypeDerivedFrom(Controller1Types.Types);

public IControllerMetadata Create(Type controllerType)
{
throw new NotImplementedException();
}
}
14 changes: 14 additions & 0 deletions src/Simplify.Web/Controllers/V1/Metadata/Controller1Types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Simplify.Web.Controllers.V1.Metadata;

public static class Controller1Types
{
public static Type[] Types =>
[
typeof(Controller),
typeof(AsyncController),
typeof(Controller<>),
typeof(AsyncController<>),
];
}
11 changes: 6 additions & 5 deletions src/Simplify.Web/Meta/Controllers/Loader/MetadataLoader.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Simplify.Web.Controllers.V1.Metadata;
using Simplify.Web.Meta.Controllers.Factory;

namespace Simplify.Web.Meta.Controllers.Loader;
Expand All @@ -14,7 +15,10 @@ public class MetadataLoader(IControllerMetadataFactoryResolver resolver) : IMeta
/// </summary>
public static IMetadataLoader Current
{
get => _loader ??= new MetadataLoader(new ControllerMetadataFactoryResolver(new List<IControllerMetadataFactory>()));
get => _loader ??= new MetadataLoader(new ControllerMetadataFactoryResolver(new List<IControllerMetadataFactory>{
new Controller1MetadataFactory()
}));

set => _loader = value ?? throw new ArgumentNullException(nameof(value));
}

Expand All @@ -23,10 +27,7 @@ public IReadOnlyCollection<IControllerMetadata> Load()
var types = SimplifyWebTypesFinder.FindTypesDerivedFrom<Controller2>();

types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom(typeof(Controller2<>))).ToList();
types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom<Controller>()).ToList();
types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom<AsyncController>()).ToList();
types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom(typeof(Controller<>))).ToList();
types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom(typeof(AsyncController<>))).ToList();
types = types.Concat(SimplifyWebTypesFinder.FindTypesDerivedFrom(Controller1Types.Types)).ToList();

return LoadMetadata(types, SimplifyWebTypesFinder.GetControllerTypesToIgnore());
}
Expand Down
22 changes: 3 additions & 19 deletions src/Simplify.Web/Meta/SimplifyWebTypesFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,14 @@ public static class SimplifyWebTypesFinder
/// <typeparam name="T"></typeparam>
public static IList<Type> FindTypesDerivedFrom<T>() => FindTypesDerivedFrom(typeof(T));

public static IList<Type> FindTypesDerivedFrom(params Type[] types) => types.SelectMany(x => FindTypesDerivedFrom(x)).ToList();

/// <summary>
/// Finds all the types derived from specified type in the current domain assemblies.
/// </summary>
/// <param name="type">Type to find types derived from.</param>
public static IList<Type> FindTypesDerivedFrom(Type type) =>
CurrentDomainAssembliesTypes.Where(t =>
{
if (t.IsAbstract)
return false;

if (t.BaseType == null)
return false;

if (t.BaseType.IsTypeOf(type))
return true;

if (t.BaseType.BaseType == null)
return false;

if (t.BaseType.BaseType.IsTypeOf(type))
return true;

return false;
}).ToList();
CurrentDomainAssembliesTypes.Where(t => t.IsTypeDerivedFrom(type)).ToList();

/// <summary>
/// Gets the controller types to ignore.
Expand Down
23 changes: 23 additions & 0 deletions src/Simplify.Web/System/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;

namespace Simplify.Web.System;

Expand All @@ -24,4 +25,26 @@ public static bool IsTypeOf(this Type t, Type type)
return false;
}
}

public static bool IsTypeDerivedFrom(this Type t, params Type[] types) => types.Any(x => t.IsTypeDerivedFrom(x));

public static bool IsTypeDerivedFrom(this Type t, Type type)
{
if (t.IsAbstract)
return false;

if (t.BaseType == null)
return false;

if (t.BaseType.IsTypeOf(type))
return true;

if (t.BaseType.BaseType == null)
return false;

if (t.BaseType.BaseType.IsTypeOf(type))
return true;

return false;
}
}

0 comments on commit 068c6f4

Please sign in to comment.