From bacb5979dade233e23176034b0febda6e6976b0c Mon Sep 17 00:00:00 2001 From: Simon G Date: Fri, 3 Dec 2021 12:16:08 +0100 Subject: [PATCH] #50: add property CreateMethods --- .../Factories/CustomTypedFactory.cs | 2 +- .../Factories/TypedFactory.cs | 8 ++--- .../Factories/TypedFactoryBase.cs | 36 +++++++++++++++++++ .../Interfaces/Factories/ITypedFactory.cs | 8 ++++- .../Registrations/RegistrationBase.cs | 2 +- 5 files changed, 47 insertions(+), 9 deletions(-) create mode 100644 LightweightIocContainer/Factories/TypedFactoryBase.cs diff --git a/LightweightIocContainer/Factories/CustomTypedFactory.cs b/LightweightIocContainer/Factories/CustomTypedFactory.cs index e7538b2..67fa08c 100644 --- a/LightweightIocContainer/Factories/CustomTypedFactory.cs +++ b/LightweightIocContainer/Factories/CustomTypedFactory.cs @@ -9,7 +9,7 @@ namespace LightweightIocContainer.Factories /// /// implementation for custom implemented factories /// - public class CustomTypedFactory : ITypedFactory + public class CustomTypedFactory : TypedFactoryBase { } diff --git a/LightweightIocContainer/Factories/TypedFactory.cs b/LightweightIocContainer/Factories/TypedFactory.cs index 05137b2..90808c3 100644 --- a/LightweightIocContainer/Factories/TypedFactory.cs +++ b/LightweightIocContainer/Factories/TypedFactory.cs @@ -17,7 +17,7 @@ namespace LightweightIocContainer.Factories /// Class to help implement an abstract typed factory /// /// The type of the abstract factory - public class TypedFactory : ITypedFactory + public class TypedFactory : TypedFactoryBase, ITypedFactory { private const string CLEAR_MULTITON_INSTANCE_METHOD_NAME = "ClearMultitonInstance"; @@ -41,10 +41,6 @@ namespace LightweightIocContainer.Factories { Type factoryType = typeof(TFactory); - List createMethods = factoryType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList(); - if (!createMethods.Any()) - throw new InvalidFactoryRegistrationException($"Factory {factoryType.Name} has no create methods."); - AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Factory"), AssemblyBuilderAccess.Run); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Factory"); TypeBuilder typeBuilder = moduleBuilder.DefineType($"TypedFactory.{factoryType.Name}"); @@ -62,7 +58,7 @@ namespace LightweightIocContainer.Factories constructorGenerator.Emit(OpCodes.Stfld, containerFieldBuilder); //set `_container` field constructorGenerator.Emit(OpCodes.Ret); - foreach (MethodInfo createMethod in createMethods) + foreach (MethodInfo createMethod in CreateMethods) { //create a method that looks like this //public `createMethod.ReturnType` Create(`createMethod.GetParameters()`) diff --git a/LightweightIocContainer/Factories/TypedFactoryBase.cs b/LightweightIocContainer/Factories/TypedFactoryBase.cs new file mode 100644 index 0000000..73b77db --- /dev/null +++ b/LightweightIocContainer/Factories/TypedFactoryBase.cs @@ -0,0 +1,36 @@ +// Author: Gockner, Simon +// Created: 2021-12-03 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using LightweightIocContainer.Exceptions; +using LightweightIocContainer.Interfaces.Factories; + +namespace LightweightIocContainer.Factories +{ + /// + /// Base class for the + /// + public abstract class TypedFactoryBase : ITypedFactory + { + /// + /// The create methods of this + /// + public List CreateMethods + { + get + { + Type factoryType = typeof(TFactory); + + List createMethods = factoryType.GetMethods().Where(m => m.ReturnType != typeof(void)).ToList(); + if (!createMethods.Any()) + throw new InvalidFactoryRegistrationException($"Factory {factoryType.Name} has no create methods."); + + return createMethods; + } + } + } +} \ No newline at end of file diff --git a/LightweightIocContainer/Interfaces/Factories/ITypedFactory.cs b/LightweightIocContainer/Interfaces/Factories/ITypedFactory.cs index 25470df..f053255 100644 --- a/LightweightIocContainer/Interfaces/Factories/ITypedFactory.cs +++ b/LightweightIocContainer/Interfaces/Factories/ITypedFactory.cs @@ -2,6 +2,9 @@ // Created: 2019-05-20 // Copyright(c) 2019 SimonG. All Rights Reserved. +using System.Collections.Generic; +using System.Reflection; + namespace LightweightIocContainer.Interfaces.Factories { /// @@ -9,7 +12,10 @@ namespace LightweightIocContainer.Interfaces.Factories /// public interface ITypedFactory { - + /// + /// The create methods of this + /// + List CreateMethods { get; } } /// diff --git a/LightweightIocContainer/Registrations/RegistrationBase.cs b/LightweightIocContainer/Registrations/RegistrationBase.cs index 666c6ba..940e3a7 100644 --- a/LightweightIocContainer/Registrations/RegistrationBase.cs +++ b/LightweightIocContainer/Registrations/RegistrationBase.cs @@ -125,7 +125,7 @@ namespace LightweightIocContainer.Registrations /// The current instance of this public IRegistrationBase WithFactory() where TFactoryImplementation : TFactoryInterface { - Factory = new CustomTypedFactory(); + Factory = new CustomTypedFactory(); _container.Register(); return this;