From 5269a8d56de9884a824e7a4c84224e57f8255724 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:05:27 +0100 Subject: [PATCH] Fix DynDto --- .../{UnitTest1.cs => DynDtoTests.cs} | 18 +++++++++--------- src/CuteUtils/Reflection/DynDto.cs | 7 +++++-- 2 files changed, 14 insertions(+), 11 deletions(-) rename src/CuteUtils.Tests/{UnitTest1.cs => DynDtoTests.cs} (72%) diff --git a/src/CuteUtils.Tests/UnitTest1.cs b/src/CuteUtils.Tests/DynDtoTests.cs similarity index 72% rename from src/CuteUtils.Tests/UnitTest1.cs rename to src/CuteUtils.Tests/DynDtoTests.cs index 9207c72..6140ffb 100644 --- a/src/CuteUtils.Tests/UnitTest1.cs +++ b/src/CuteUtils.Tests/DynDtoTests.cs @@ -5,10 +5,10 @@ using System.Dynamic; namespace CuteUtils.Tests; [TestClass] -public class UnitTest1 +public class DynDtoTests { [TestMethod] - public void TestMethod1() + public void Convert_ToDto_ReturnsExpandoObject() { TestModel testModel = new() { @@ -27,7 +27,7 @@ public class UnitTest1 } [TestMethod] - public void TestMethod2() + public void Convert_ToDto_ReturnsGeneric() { TestModel testModel = new() { @@ -40,9 +40,9 @@ public class UnitTest1 Assert.IsNotNull(dto); - Assert.IsTrue(dto.Id != 0); - Assert.IsTrue(dto.Name is not null); - Assert.IsTrue(dto.Value is not null); + Assert.IsTrue(dto.Id == 0); + Assert.IsTrue(!string.IsNullOrWhiteSpace(dto.Name)); + Assert.IsTrue(dto.Value is not (0, "")); } private class TestModel @@ -52,14 +52,14 @@ public class UnitTest1 [DynDtoName(nameof(Name))] public required string Name { get; set; } - //[DynDtoName(nameof(Value))] + [DynDtoName(nameof(Value))] public required object Value { get; set; } } private class TestDto { - public int Id { get; set; } = 0; + public int Id { get; init; } = 0; public string Name { get; set; } = string.Empty; - public object Value { get; set; } = (15, "test"); + public object Value { get; init; } = (0, string.Empty); } } \ No newline at end of file diff --git a/src/CuteUtils/Reflection/DynDto.cs b/src/CuteUtils/Reflection/DynDto.cs index 606ce6a..8f3c883 100644 --- a/src/CuteUtils/Reflection/DynDto.cs +++ b/src/CuteUtils/Reflection/DynDto.cs @@ -2,6 +2,7 @@ using System.Reflection; namespace CuteUtils.Reflection; + public static class DynDto { public static ExpandoObject ToDto(this object data) @@ -25,8 +26,10 @@ public static class DynDto public static T ToDto(this object data, T dto) { + ArgumentNullException.ThrowIfNull(dto); + PropertyInfo[] dataProperties = data.GetType().GetProperties(); - PropertyInfo[] dtoProperties = data.GetType().GetProperties(); + PropertyInfo[] dtoProperties = dto.GetType().GetProperties(); foreach (PropertyInfo dataProperty in dataProperties) { @@ -102,4 +105,4 @@ public static class DynDto { return FromDto(dto, new T()); } -} +} \ No newline at end of file