From a681bbbaf309ce706fddaaf6552fe6fc40845ef9 Mon Sep 17 00:00:00 2001 From: "jozsef.b@aycode.com" <9Rj@D}fVwBaN> Date: Wed, 8 Nov 2023 09:35:34 +0100 Subject: [PATCH] first commit --- .gitattributes | 63 +++ .gitignore | 375 ++++++++++++++++++ AyCode.Core.Server/AyCode.Core.Server.csproj | 9 + AyCode.Core.sln | 73 ++++ AyCode.Core/AyCode.Core.csproj | 9 + AyCode.Database/AyCode.Database.csproj | 23 ++ AyCode.Database/DbContexts/DbContextBase.cs | 85 ++++ .../Extensions/DbContextExtension.cs | 126 ++++++ .../AyCode.Entities.Server.csproj | 9 + AyCode.Entities/AyCode.Entities.csproj | 9 + AyCode.Entities/Interfaces/IEntity.cs | 19 + AyCode.Entities/Interfaces/IEntityGuid.cs | 5 + AyCode.Entities/Interfaces/IEntityInt.cs | 5 + .../AyCode.Interfaces.Server.csproj | 9 + AyCode.Interfaces/AyCode.Interfaces.csproj | 9 + .../TimeStampInfo/ITimeStampCreated.cs | 6 + .../TimeStampInfo/ITimeStampDisableAutoSet.cs | 5 + .../TimeStampInfo/ITimeStampModified.cs | 6 + .../TimeStampInfo/TimeStampInfo.cs | 12 + .../AyCode.Utils.Server.csproj | 9 + AyCode.Utils/AyCode.Utils.csproj | 9 + 21 files changed, 875 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 AyCode.Core.Server/AyCode.Core.Server.csproj create mode 100644 AyCode.Core.sln create mode 100644 AyCode.Core/AyCode.Core.csproj create mode 100644 AyCode.Database/AyCode.Database.csproj create mode 100644 AyCode.Database/DbContexts/DbContextBase.cs create mode 100644 AyCode.Database/Extensions/DbContextExtension.cs create mode 100644 AyCode.Entities.Server/AyCode.Entities.Server.csproj create mode 100644 AyCode.Entities/AyCode.Entities.csproj create mode 100644 AyCode.Entities/Interfaces/IEntity.cs create mode 100644 AyCode.Entities/Interfaces/IEntityGuid.cs create mode 100644 AyCode.Entities/Interfaces/IEntityInt.cs create mode 100644 AyCode.Interfaces.Server/AyCode.Interfaces.Server.csproj create mode 100644 AyCode.Interfaces/AyCode.Interfaces.csproj create mode 100644 AyCode.Interfaces/TimeStampInfo/ITimeStampCreated.cs create mode 100644 AyCode.Interfaces/TimeStampInfo/ITimeStampDisableAutoSet.cs create mode 100644 AyCode.Interfaces/TimeStampInfo/ITimeStampModified.cs create mode 100644 AyCode.Interfaces/TimeStampInfo/TimeStampInfo.cs create mode 100644 AyCode.Utils.Server/AyCode.Utils.Server.csproj create mode 100644 AyCode.Utils/AyCode.Utils.csproj diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd8b8e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,375 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +obj/* +bin/* + +**/obj/* +**/bin/* + +obj/ +bin/ + +/obj/* +/bin/* + +/.vs/* +/.vs/** + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*[.json, .xml, .info] + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/AyCode.Core.Server/AyCode.Core.Server.csproj b/AyCode.Core.Server/AyCode.Core.Server.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Core.Server/AyCode.Core.Server.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Core.sln b/AyCode.Core.sln new file mode 100644 index 0000000..16fe191 --- /dev/null +++ b/AyCode.Core.sln @@ -0,0 +1,73 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.7.34221.43 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Core", "AyCode.Core\AyCode.Core.csproj", "{8CCC4969-7306-4747-8A58-80AC5A062EE1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Entities", "AyCode.Entities\AyCode.Entities.csproj", "{A2105535-1397-4307-B61B-E49C983353B9}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Entities.Server", "AyCode.Entities.Server\AyCode.Entities.Server.csproj", "{FB027D80-8949-403B-9A86-8E99F305016E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Database", "AyCode.Database\AyCode.Database.csproj", "{CAB60420-9F66-42D9-B67E-8E837DBA1F30}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Interfaces", "AyCode.Interfaces\AyCode.Interfaces.csproj", "{DC42F79D-EEF0-4F32-8608-230F24C6F22A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Interfaces.Server", "AyCode.Interfaces.Server\AyCode.Interfaces.Server.csproj", "{0B5AC35E-3E71-42DC-B503-80D6D3089F91}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Core.Server", "AyCode.Core.Server\AyCode.Core.Server.csproj", "{F8ECCA33-B5EA-490D-B1A1-D33B5E4238A5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Utils", "AyCode.Utils\AyCode.Utils.csproj", "{35D47907-CE4F-435B-823E-A02BE59C16D7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AyCode.Utils.Server", "AyCode.Utils.Server\AyCode.Utils.Server.csproj", "{EBC6371C-9454-473D-9547-DF9DECEB2D2A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {8CCC4969-7306-4747-8A58-80AC5A062EE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8CCC4969-7306-4747-8A58-80AC5A062EE1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8CCC4969-7306-4747-8A58-80AC5A062EE1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8CCC4969-7306-4747-8A58-80AC5A062EE1}.Release|Any CPU.Build.0 = Release|Any CPU + {A2105535-1397-4307-B61B-E49C983353B9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2105535-1397-4307-B61B-E49C983353B9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2105535-1397-4307-B61B-E49C983353B9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2105535-1397-4307-B61B-E49C983353B9}.Release|Any CPU.Build.0 = Release|Any CPU + {FB027D80-8949-403B-9A86-8E99F305016E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FB027D80-8949-403B-9A86-8E99F305016E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FB027D80-8949-403B-9A86-8E99F305016E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FB027D80-8949-403B-9A86-8E99F305016E}.Release|Any CPU.Build.0 = Release|Any CPU + {CAB60420-9F66-42D9-B67E-8E837DBA1F30}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CAB60420-9F66-42D9-B67E-8E837DBA1F30}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CAB60420-9F66-42D9-B67E-8E837DBA1F30}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CAB60420-9F66-42D9-B67E-8E837DBA1F30}.Release|Any CPU.Build.0 = Release|Any CPU + {DC42F79D-EEF0-4F32-8608-230F24C6F22A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC42F79D-EEF0-4F32-8608-230F24C6F22A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC42F79D-EEF0-4F32-8608-230F24C6F22A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC42F79D-EEF0-4F32-8608-230F24C6F22A}.Release|Any CPU.Build.0 = Release|Any CPU + {0B5AC35E-3E71-42DC-B503-80D6D3089F91}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0B5AC35E-3E71-42DC-B503-80D6D3089F91}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0B5AC35E-3E71-42DC-B503-80D6D3089F91}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0B5AC35E-3E71-42DC-B503-80D6D3089F91}.Release|Any CPU.Build.0 = Release|Any CPU + {F8ECCA33-B5EA-490D-B1A1-D33B5E4238A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F8ECCA33-B5EA-490D-B1A1-D33B5E4238A5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F8ECCA33-B5EA-490D-B1A1-D33B5E4238A5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F8ECCA33-B5EA-490D-B1A1-D33B5E4238A5}.Release|Any CPU.Build.0 = Release|Any CPU + {35D47907-CE4F-435B-823E-A02BE59C16D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35D47907-CE4F-435B-823E-A02BE59C16D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35D47907-CE4F-435B-823E-A02BE59C16D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35D47907-CE4F-435B-823E-A02BE59C16D7}.Release|Any CPU.Build.0 = Release|Any CPU + {EBC6371C-9454-473D-9547-DF9DECEB2D2A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EBC6371C-9454-473D-9547-DF9DECEB2D2A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EBC6371C-9454-473D-9547-DF9DECEB2D2A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EBC6371C-9454-473D-9547-DF9DECEB2D2A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E97347E0-52CA-4086-A3F4-CE65ABDE9964} + EndGlobalSection +EndGlobal diff --git a/AyCode.Core/AyCode.Core.csproj b/AyCode.Core/AyCode.Core.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Core/AyCode.Core.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Database/AyCode.Database.csproj b/AyCode.Database/AyCode.Database.csproj new file mode 100644 index 0000000..9c2703f --- /dev/null +++ b/AyCode.Database/AyCode.Database.csproj @@ -0,0 +1,23 @@ + + + + net7.0 + enable + enable + + + + + + + + + + + + + + + + + diff --git a/AyCode.Database/DbContexts/DbContextBase.cs b/AyCode.Database/DbContexts/DbContextBase.cs new file mode 100644 index 0000000..4a81766 --- /dev/null +++ b/AyCode.Database/DbContexts/DbContextBase.cs @@ -0,0 +1,85 @@ +using Microsoft.EntityFrameworkCore; +using AyCode.Database.Extensions; +using AyCode.Interfaces.TimeStampInfo; + +namespace AyCode.Database.DbContexts; + +public class DbContextBase : DbContext +{ + public string Name { get; set; } + + public Guid SessionId { get; protected set; } = Guid.NewGuid(); + + public DbContextBase() : this(string.Empty) + { + //DbInterception.Add(new UtcDateTimeDbCommandInterceptor()); + } + + public DbContextBase(string name) : base() + { + Name = name; + } + + public DbContextBase(DbContextOptions options, string name) : base(options) + { + Name = name; + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseLazyLoadingProxies(true); + } + + public override int SaveChanges() + { + var entries = ChangeTracker.Entries().Where(e => + e.Entity is not ITimeStampDisableAutoSet && (e is { State: EntityState.Added, Entity: ITimeStampCreated } || e.State is EntityState.Modified or EntityState.Added && e.Entity is ITimeStampModified)); + + var utcNow = DateTime.UtcNow; + foreach (var entityEntry in entries) + { + //entityEntry.DebugView.LongView + if (entityEntry.Entity is ITimeStampModified timeStampModified) + timeStampModified.Modified = utcNow; + + if (entityEntry.State != EntityState.Added) continue; + + if (entityEntry.Entity is ITimeStampCreated timeStampCreated) + timeStampCreated.Created = utcNow; + } + + return base.SaveChanges(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + //modelBuilder.ComplexType(); + //modelBuilder.ComplexType(); + + //modelBuilder.Conventions.Delete(); + + //modelBuilder.Conventions.Delete(); + //modelBuilder.Conventions.Delete(); + //modelBuilder.Conventions.Delete(); + + //var dateTimeConverter = new ValueConverter(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); + //modelBuilder.UseValueConverterForType(dateTimeConverter); + + modelBuilder.UseDateTimeUtc(); + + modelBuilder.EntitiesOfType((type, builder) => + { + builder.Property(nameof(ITimeStampCreated.Created)).IsRequired(); //.HasDefaultValueSql("getutcdate()"); + }); + + modelBuilder.EntitiesOfType((type, builder) => + { + builder.Property(nameof(ITimeStampModified.Modified)).IsRequired(); //HasComputedColumnSql("getutcdate()"); + }); + } + + public override string ToString() + { + return $"{GetType().Name}, {Name} - ({SessionId})"; + } +} \ No newline at end of file diff --git a/AyCode.Database/Extensions/DbContextExtension.cs b/AyCode.Database/Extensions/DbContextExtension.cs new file mode 100644 index 0000000..dc87900 --- /dev/null +++ b/AyCode.Database/Extensions/DbContextExtension.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Security.Principal; +using System.Text; +using System.Threading.Tasks; +using AyCode.Entities.Interfaces; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +namespace AyCode.Database.Extensions +{ + public static class DbContextExtension + { + + public static IEntityType? GetEntityType(this DbContext ctx) + { + return ctx.Model.FindEntityType(typeof(TEntity)); + } + + public static IDictionary GetKeysValue(this DbContext ctx, TEntity entity) where TEntity : class, IEntity + { + if (ctx == null) + throw new ArgumentNullException(nameof(ctx)); + + if (entity == null) + throw new ArgumentNullException(nameof(entity)); + + + var entry = ctx.Entry(entity); + var primaryKey = entry.Metadata.FindPrimaryKey(); + var keys = primaryKey.Properties.ToDictionary(x => x.Name, x => x.PropertyInfo.GetValue(entity)); + + return keys; + } + + public static IEnumerable GetModifiedProperties(this DbContext ctx, object entity) + { + if (ctx == null) + throw new ArgumentNullException(nameof(ctx)); + + if (entity == null) + throw new ArgumentNullException(nameof(entity)); + + + var entry = ctx.Entry(entity); + var originalValues = entry.OriginalValues; + var currentValues = entry.CurrentValues; + + foreach (var prop in originalValues.Properties) + { + if (object.Equals(originalValues[prop.Name], currentValues[prop.Name]) == false) + { + yield return prop; + } + } + } + + public static void ResetValues(this DbContext ctx, object entity) + { + if (ctx == null) + throw new ArgumentNullException(nameof(ctx)); + + if (entity == null) + throw new ArgumentNullException(nameof(entity)); + + + var entry = ctx.Entry(entity); + var originalValues = entry.OriginalValues; + var currentValues = entry.CurrentValues; + + foreach (var prop in originalValues.Properties) + { + currentValues[prop.Name] = originalValues[prop.Name]; + } + entry.State = EntityState.Unchanged; + } + + public static ModelBuilder UseValueConverterForType(this ModelBuilder modelBuilder, ValueConverter converter) + { + return modelBuilder.UseValueConverterForType(typeof(T), converter); + } + + public static ModelBuilder UseValueConverterForType(this ModelBuilder modelBuilder, Type type, ValueConverter converter) + { + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + foreach (var property in entityType.ClrType.GetProperties()) + { + if (property.PropertyType == type) + modelBuilder.Entity(entityType.Name).Property(property.Name).HasConversion(converter); + } + } + return modelBuilder; + } + + public static void UseDateTimeUtc(this ModelBuilder modelBuilder) + { + var dateTimeConverter = new ValueConverter(v => v, v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); + + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + { + foreach (var property in entityType.GetProperties()) + { + if (property.ClrType == typeof(DateTime) || property.ClrType == typeof(DateTime?)) + property.SetValueConverter(dateTimeConverter); + } + } + } + public static ModelBuilder EntitiesOfType(this ModelBuilder modelBuilder, Action entityTypeBuilder) where T : class + => modelBuilder.EntitiesOfType(typeof(T), entityTypeBuilder); + + public static ModelBuilder EntitiesOfType(this ModelBuilder modelBuilder, Type type, Action entityTypeBuilder) + { + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) + if (type.IsAssignableFrom(entityType.ClrType)) + entityTypeBuilder(entityType, modelBuilder.Entity(entityType.ClrType)); + + return modelBuilder; + } + } + +} diff --git a/AyCode.Entities.Server/AyCode.Entities.Server.csproj b/AyCode.Entities.Server/AyCode.Entities.Server.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Entities.Server/AyCode.Entities.Server.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Entities/AyCode.Entities.csproj b/AyCode.Entities/AyCode.Entities.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Entities/AyCode.Entities.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Entities/Interfaces/IEntity.cs b/AyCode.Entities/Interfaces/IEntity.cs new file mode 100644 index 0000000..ea8045d --- /dev/null +++ b/AyCode.Entities/Interfaces/IEntity.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AyCode.Entities.Interfaces +{ + public interface IEntity + { + } + + public interface IEntity : IEntity + { + [Key] T Id { get; set; } + //T SetId(T id); + } +} diff --git a/AyCode.Entities/Interfaces/IEntityGuid.cs b/AyCode.Entities/Interfaces/IEntityGuid.cs new file mode 100644 index 0000000..22c9ef2 --- /dev/null +++ b/AyCode.Entities/Interfaces/IEntityGuid.cs @@ -0,0 +1,5 @@ +namespace AyCode.Entities.Interfaces; + +public interface IEntityGuid : IEntity +{ +} \ No newline at end of file diff --git a/AyCode.Entities/Interfaces/IEntityInt.cs b/AyCode.Entities/Interfaces/IEntityInt.cs new file mode 100644 index 0000000..a187df6 --- /dev/null +++ b/AyCode.Entities/Interfaces/IEntityInt.cs @@ -0,0 +1,5 @@ +namespace AyCode.Entities.Interfaces; + +public interface IEntityInt : IEntity +{ +} \ No newline at end of file diff --git a/AyCode.Interfaces.Server/AyCode.Interfaces.Server.csproj b/AyCode.Interfaces.Server/AyCode.Interfaces.Server.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Interfaces.Server/AyCode.Interfaces.Server.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Interfaces/AyCode.Interfaces.csproj b/AyCode.Interfaces/AyCode.Interfaces.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Interfaces/AyCode.Interfaces.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Interfaces/TimeStampInfo/ITimeStampCreated.cs b/AyCode.Interfaces/TimeStampInfo/ITimeStampCreated.cs new file mode 100644 index 0000000..ea0763b --- /dev/null +++ b/AyCode.Interfaces/TimeStampInfo/ITimeStampCreated.cs @@ -0,0 +1,6 @@ +namespace AyCode.Interfaces.TimeStampInfo; + +public interface ITimeStampCreated +{ + DateTime Created { get; set; } +} \ No newline at end of file diff --git a/AyCode.Interfaces/TimeStampInfo/ITimeStampDisableAutoSet.cs b/AyCode.Interfaces/TimeStampInfo/ITimeStampDisableAutoSet.cs new file mode 100644 index 0000000..2b4ebf8 --- /dev/null +++ b/AyCode.Interfaces/TimeStampInfo/ITimeStampDisableAutoSet.cs @@ -0,0 +1,5 @@ +namespace AyCode.Interfaces.TimeStampInfo; + +public interface ITimeStampDisableAutoSet +{ +} \ No newline at end of file diff --git a/AyCode.Interfaces/TimeStampInfo/ITimeStampModified.cs b/AyCode.Interfaces/TimeStampInfo/ITimeStampModified.cs new file mode 100644 index 0000000..6799897 --- /dev/null +++ b/AyCode.Interfaces/TimeStampInfo/ITimeStampModified.cs @@ -0,0 +1,6 @@ +namespace AyCode.Interfaces.TimeStampInfo; + +public interface ITimeStampModified +{ + DateTime Modified { get; set; } +} \ No newline at end of file diff --git a/AyCode.Interfaces/TimeStampInfo/TimeStampInfo.cs b/AyCode.Interfaces/TimeStampInfo/TimeStampInfo.cs new file mode 100644 index 0000000..82c9878 --- /dev/null +++ b/AyCode.Interfaces/TimeStampInfo/TimeStampInfo.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AyCode.Interfaces.TimeStampInfo +{ + public interface ITimeStampInfo : ITimeStampCreated, ITimeStampModified + { + } +} diff --git a/AyCode.Utils.Server/AyCode.Utils.Server.csproj b/AyCode.Utils.Server/AyCode.Utils.Server.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Utils.Server/AyCode.Utils.Server.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + + diff --git a/AyCode.Utils/AyCode.Utils.csproj b/AyCode.Utils/AyCode.Utils.csproj new file mode 100644 index 0000000..cfadb03 --- /dev/null +++ b/AyCode.Utils/AyCode.Utils.csproj @@ -0,0 +1,9 @@ + + + + net7.0 + enable + enable + + +