-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from cowienduckie/dev
fix: add helpers to convert dtos to domain objects
- Loading branch information
Showing
4 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/BuildingBlocks/Shared/Common/Helpers/DateTimeHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace Shared.Common.Helpers; | ||
|
||
public static class DateTimeHelper | ||
{ | ||
public static DateTime ToDateTime(this object obj) | ||
{ | ||
if (obj is DateTime dateTime) | ||
{ | ||
return dateTime.ToUniversalTime(); | ||
} | ||
|
||
if (obj is string stringValue && DateTime.TryParse(stringValue, out var parsedDateTime)) | ||
{ | ||
return parsedDateTime.ToUniversalTime(); | ||
} | ||
|
||
throw new ArgumentException("Invalid DateTime format"); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/BuildingBlocks/Shared/Common/Helpers/DictionaryHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System.Reflection; | ||
|
||
namespace Shared.Common.Helpers; | ||
|
||
public static class DictionaryHelper | ||
{ | ||
public static IDictionary<TKey, TValue> AsDictionary<TKey, TValue>(this object obj) | ||
{ | ||
return (IDictionary<TKey, TValue>)obj; | ||
} | ||
|
||
public static T GetObject<T>(this IDictionary<string, object?> dictionary) where T : new() | ||
{ | ||
var result = new T(); | ||
|
||
foreach (var kvp in dictionary) | ||
{ | ||
var property = typeof(T).GetProperty(kvp.Key, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); | ||
|
||
if (property == null || !property.CanWrite) | ||
{ | ||
continue; | ||
} | ||
|
||
var value = kvp.Value; | ||
|
||
if (value is not null) | ||
{ | ||
if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?)) | ||
{ | ||
value = value.ToDateTime(); | ||
} | ||
else | ||
{ | ||
value = Convert.ChangeType(value, property.PropertyType); | ||
} | ||
} | ||
|
||
property.SetValue(result, value, null); | ||
} | ||
|
||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters