#50: add property CreateMethods

pull/53/head
Simon G 4 years ago
parent edfba57809
commit bacb5979da
  1. 2
      LightweightIocContainer/Factories/CustomTypedFactory.cs
  2. 8
      LightweightIocContainer/Factories/TypedFactory.cs
  3. 36
      LightweightIocContainer/Factories/TypedFactoryBase.cs
  4. 8
      LightweightIocContainer/Interfaces/Factories/ITypedFactory.cs
  5. 2
      LightweightIocContainer/Registrations/RegistrationBase.cs

@ -9,7 +9,7 @@ namespace LightweightIocContainer.Factories
/// <summary> /// <summary>
/// <see cref="ITypedFactory"/> implementation for custom implemented factories /// <see cref="ITypedFactory"/> implementation for custom implemented factories
/// </summary> /// </summary>
public class CustomTypedFactory : ITypedFactory public class CustomTypedFactory<TFactory> : TypedFactoryBase<TFactory>
{ {
} }

@ -17,7 +17,7 @@ namespace LightweightIocContainer.Factories
/// Class to help implement an abstract typed factory /// Class to help implement an abstract typed factory
/// </summary> /// </summary>
/// <typeparam name="TFactory">The type of the abstract factory</typeparam> /// <typeparam name="TFactory">The type of the abstract factory</typeparam>
public class TypedFactory<TFactory> : ITypedFactory<TFactory> public class TypedFactory<TFactory> : TypedFactoryBase<TFactory>, ITypedFactory<TFactory>
{ {
private const string CLEAR_MULTITON_INSTANCE_METHOD_NAME = "ClearMultitonInstance"; private const string CLEAR_MULTITON_INSTANCE_METHOD_NAME = "ClearMultitonInstance";
@ -41,10 +41,6 @@ namespace LightweightIocContainer.Factories
{ {
Type factoryType = typeof(TFactory); Type factoryType = typeof(TFactory);
List<MethodInfo> 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); AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Factory"), AssemblyBuilderAccess.Run);
ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Factory"); ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("Factory");
TypeBuilder typeBuilder = moduleBuilder.DefineType($"TypedFactory.{factoryType.Name}"); 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.Stfld, containerFieldBuilder); //set `_container` field
constructorGenerator.Emit(OpCodes.Ret); constructorGenerator.Emit(OpCodes.Ret);
foreach (MethodInfo createMethod in createMethods) foreach (MethodInfo createMethod in CreateMethods)
{ {
//create a method that looks like this //create a method that looks like this
//public `createMethod.ReturnType` Create(`createMethod.GetParameters()`) //public `createMethod.ReturnType` Create(`createMethod.GetParameters()`)

@ -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
{
/// <summary>
/// Base class for the <see cref="ITypedFactory"/>
/// </summary>
public abstract class TypedFactoryBase<TFactory> : ITypedFactory
{
/// <summary>
/// The create methods of this <see cref="ITypedFactory"/>
/// </summary>
public List<MethodInfo> CreateMethods
{
get
{
Type factoryType = typeof(TFactory);
List<MethodInfo> 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;
}
}
}
}

@ -2,6 +2,9 @@
// Created: 2019-05-20 // Created: 2019-05-20
// Copyright(c) 2019 SimonG. All Rights Reserved. // Copyright(c) 2019 SimonG. All Rights Reserved.
using System.Collections.Generic;
using System.Reflection;
namespace LightweightIocContainer.Interfaces.Factories namespace LightweightIocContainer.Interfaces.Factories
{ {
/// <summary> /// <summary>
@ -9,7 +12,10 @@ namespace LightweightIocContainer.Interfaces.Factories
/// </summary> /// </summary>
public interface ITypedFactory public interface ITypedFactory
{ {
/// <summary>
/// The create methods of this <see cref="ITypedFactory"/>
/// </summary>
List<MethodInfo> CreateMethods { get; }
} }
/// <summary> /// <summary>

@ -125,7 +125,7 @@ namespace LightweightIocContainer.Registrations
/// <returns>The current instance of this <see cref="IRegistrationBase"/></returns> /// <returns>The current instance of this <see cref="IRegistrationBase"/></returns>
public IRegistrationBase WithFactory<TFactoryInterface, TFactoryImplementation>() where TFactoryImplementation : TFactoryInterface public IRegistrationBase WithFactory<TFactoryInterface, TFactoryImplementation>() where TFactoryImplementation : TFactoryInterface
{ {
Factory = new CustomTypedFactory(); Factory = new CustomTypedFactory<TFactoryInterface>();
_container.Register<TFactoryInterface, TFactoryImplementation>(); _container.Register<TFactoryInterface, TFactoryImplementation>();
return this; return this;

Loading…
Cancel
Save