From c0b77acb42e732515c67ed95a26947bb92fb6fcc Mon Sep 17 00:00:00 2001 From: jojo aquino Date: Mon, 16 Dec 2024 17:27:27 +0000 Subject: [PATCH] new extension methods --- .../Extensions/ModelBuilderExtensions.cs | 44 +++++++++++++++++++ .../Common/Extensions/DateTimeExtensions.cs | 9 ++++ .../Common/Extensions/StringExtensions.cs | 7 +++ .../EnotaryoPH.Web/Common/Helpers/FullName.cs | 9 ++++ EnotaryoPH/EnotaryoPH.Web/Usings.cs | 4 +- 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 EnotaryoPH/EnotaryoPH.Data/Extensions/ModelBuilderExtensions.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Extensions/DateTimeExtensions.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Extensions/StringExtensions.cs create mode 100644 EnotaryoPH/EnotaryoPH.Web/Common/Helpers/FullName.cs diff --git a/EnotaryoPH/EnotaryoPH.Data/Extensions/ModelBuilderExtensions.cs b/EnotaryoPH/EnotaryoPH.Data/Extensions/ModelBuilderExtensions.cs new file mode 100644 index 0000000..30d4624 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Data/Extensions/ModelBuilderExtensions.cs @@ -0,0 +1,44 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace EnotaryoPH.Data.Extensions +{ + public static class ModelBuilderExtension + { + public static ModelBuilder UseValueConverterForType(this ModelBuilder modelBuilder, ValueConverter converter) => modelBuilder.UseValueConverterForType(typeof(T), converter); + + public static ModelBuilder UseValueConverterForType(this ModelBuilder modelBuilder, Type type, ValueConverter converter) + { + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + // note that entityType.GetProperties() will throw an exception, so we have to use reflection + var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == type); + + foreach (var property in properties) + { + modelBuilder.Entity(entityType.Name).Property(property.Name) + .HasConversion(converter); + } + } + + return modelBuilder; + } + + public class DateTimeKindValueConverter : ValueConverter + { + public DateTimeKindValueConverter(DateTimeKind kind, ConverterMappingHints mappingHints = null) + : base( + v => v.ToUniversalTime(), + v => DateTime.SpecifyKind(v, kind), + mappingHints) + { + } + } + + public static void SetDefaultDateTimeKind(this ModelBuilder modelBuilder, DateTimeKind kind) + { + modelBuilder.UseValueConverterForType(new DateTimeKindValueConverter(kind)); + modelBuilder.UseValueConverterForType(new DateTimeKindValueConverter(kind)); + } + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/DateTimeExtensions.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..f9a9452 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/DateTimeExtensions.cs @@ -0,0 +1,9 @@ +namespace EnotaryoPH.Web.Common.Extensions +{ + public static class DateTimeExtensions + { + public static DateTime ToUTC(this DateTime? dte) => dte.GetValueOrDefault().ToUTC(); + + public static DateTime ToUTC(this DateTime dte) => new(dte.Ticks, DateTimeKind.Utc); + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/StringExtensions.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/StringExtensions.cs new file mode 100644 index 0000000..e4d72ad --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Extensions/StringExtensions.cs @@ -0,0 +1,7 @@ +namespace EnotaryoPH.Web.Common.Extensions +{ + public static class StringExtensions + { + public static string DefaultIfEmpty(this string s, string defaultValue) => !string.IsNullOrWhiteSpace(s) ? s : (defaultValue ?? string.Empty); + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Common/Helpers/FullName.cs b/EnotaryoPH/EnotaryoPH.Web/Common/Helpers/FullName.cs new file mode 100644 index 0000000..92d1490 --- /dev/null +++ b/EnotaryoPH/EnotaryoPH.Web/Common/Helpers/FullName.cs @@ -0,0 +1,9 @@ +using System.Runtime.CompilerServices; + +namespace EnotaryoPH.Web.Common.Helpers +{ + public static class FullName + { + public static string Of(T _, [CallerArgumentExpression("_")] string fullName = "") => fullName; + } +} \ No newline at end of file diff --git a/EnotaryoPH/EnotaryoPH.Web/Usings.cs b/EnotaryoPH/EnotaryoPH.Web/Usings.cs index b5890ea..e29a591 100644 --- a/EnotaryoPH/EnotaryoPH.Web/Usings.cs +++ b/EnotaryoPH/EnotaryoPH.Web/Usings.cs @@ -1 +1,3 @@ -global using EnotaryoPH.Web.Common.Extensions; \ No newline at end of file +global using EnotaryoPH.Web.Common.Extensions; +global using EnotaryoPH.Web.Common.Helpers; +global using EnotaryoPH.Web.Common.Services; \ No newline at end of file