diff --git a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj index a90692f..ba10afc 100644 --- a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj +++ b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).AspNetCore + + all @@ -8,6 +12,7 @@ + StrongOf.AspNetCore true readme.md LICENSE.txt diff --git a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj index 4eb8ca7..8e8d250 100644 --- a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj +++ b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).FluentValidation + + all @@ -8,6 +12,7 @@ + StrongOf.FluentValidation true readme.md LICENSE.txt diff --git a/src/StrongOf.Json/StrongOf.Json.csproj b/src/StrongOf.Json/StrongOf.Json.csproj index 065027c..0a724f5 100644 --- a/src/StrongOf.Json/StrongOf.Json.csproj +++ b/src/StrongOf.Json/StrongOf.Json.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).Json + + all @@ -8,6 +12,7 @@ + StrongOf.Json true readme.md LICENSE.txt diff --git a/src/StrongOf/Factories/StrongOfInstanceFactory.cs b/src/StrongOf/Factories/StrongOfInstanceFactory.cs index 3d63933..7234bed 100644 --- a/src/StrongOf/Factories/StrongOfInstanceFactory.cs +++ b/src/StrongOf/Factories/StrongOfInstanceFactory.cs @@ -6,15 +6,25 @@ namespace StrongOf.Factories; /// /// A factory class for creating Lambda Expressions. /// -internal static class StrongOfInstanceFactory +public static class StrongOfInstanceFactory { /// - /// Creates a LambdaExpression with one parameter. + /// Creates a lambda expression representing the instantiation of a constructor with one parameter. /// - /// The type of the object to be created. + /// The type of object to be constructed. /// The type of the parameter for the constructor. - /// A LambdaExpression that represents the creation of an object of type TStrong with a constructor parameter of type TTarget. + /// A lambda expression representing the constructor invocation. + /// Thrown when no constructor is found for the specified type with the given parameter type. + /// + /// The following example demonstrates how to use the CreateWithOneParameterExpression method. + /// + /// var expression = LambdaExpressionHelper.CreateWithOneParameterExpression<TestClass, int>(); + /// Func<int, TestClass> func = (Func<int, TestClass>)expression.Compile(); + /// TestClass instance = func.Invoke(42); + /// + /// public static LambdaExpression CreateWithOneParameterExpression() + where TStrong : class, IStrongOf { Type ctorParameterType = typeof(TTarget); ParameterExpression ctorParameter = Expression.Parameter(ctorParameterType); @@ -36,6 +46,7 @@ public static LambdaExpression CreateWithOneParameterExpressionThe type of the parameter for the constructor. /// A Func delegate that represents the creation of an object of type TStrong with a constructor parameter of type TTarget. public static Func CreateWithOneParameterDelegate() + where TStrong : class, IStrongOf { return (Func)CreateWithOneParameterExpression().Compile(); } diff --git a/src/StrongOf/StrongOf.csproj b/src/StrongOf/StrongOf.csproj index be03d13..95f7bff 100644 --- a/src/StrongOf/StrongOf.csproj +++ b/src/StrongOf/StrongOf.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix) + + all @@ -8,6 +12,7 @@ + StrongOf true readme.md LICENSE.txt diff --git a/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs b/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs new file mode 100644 index 0000000..1d82372 --- /dev/null +++ b/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs @@ -0,0 +1,46 @@ +using System.Linq.Expressions; +using System.Reflection; +using StrongOf.Factories; +using Xunit; + +namespace StrongOf.UnitTests.Factories; + +public class StrongOfInstanceFactoryTests +{ + private sealed class TestInt32Of(int Value) : StrongInt32(Value) { } + + [Fact] + public void CreateWithOneParameterExpression_SuccessfullyCreatesLambdaExpression() + { + // Arrange + Type expectedStrong = typeof(TestInt32Of); + Type expectedType = typeof(int); + ConstructorInfo? expectedCtor = expectedStrong.GetConstructor([expectedType]); + + ArgumentNullException.ThrowIfNull(expectedCtor); + + // Act + LambdaExpression lambdaExpression = StrongOfInstanceFactory.CreateWithOneParameterExpression(); + + // Assert + Assert.NotNull(lambdaExpression); + + // Assert type + Assert.IsAssignableFrom(lambdaExpression); + Assert.IsAssignableFrom>>(lambdaExpression); + + // Assert instance + Func func = (Func)lambdaExpression.Compile(); + TestInt32Of newInstance = func.Invoke(10); + + Assert.NotNull(newInstance); + Assert.IsType(newInstance); + + // Assert params + Expression? ctorParameter = ((NewExpression)lambdaExpression.Body).Arguments.First(); + Assert.Equal(expectedType, ctorParameter.Type); + + ConstructorInfo? ctor = ((NewExpression)lambdaExpression.Body).Constructor; + Assert.Equal(expectedCtor, ctor); + } +}