From 6dc861d462318a7e67ddda9d0cc5725b0f3516e0 Mon Sep 17 00:00:00 2001 From: phat Date: Sun, 6 Oct 2024 23:33:43 +0700 Subject: [PATCH 1/3] Remove redundant code --- .../ViewModel/SimpleViewModelBase.Async.cs | 6 +- .../ViewModel/SimpleViewModelBase.Uow.cs | 2 +- .../ViewModel/ViewModelBase.Async.cs | 142 ------------------ src/mix.heart/ViewModel/ViewModelBase.Uow.cs | 76 ---------- src/mix.heart/ViewModel/ViewModelBase.cs | 75 +-------- 5 files changed, 7 insertions(+), 294 deletions(-) delete mode 100644 src/mix.heart/ViewModel/ViewModelBase.Async.cs delete mode 100644 src/mix.heart/ViewModel/ViewModelBase.Uow.cs diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs index e3a8d58..a8b54c6 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs @@ -15,7 +15,7 @@ public abstract partial class SimpleViewModelBase SaveAsync(CancellationToken cancellationToken = default) + public virtual async Task SaveAsync(CancellationToken cancellationToken = default) { try { @@ -70,7 +70,7 @@ public async Task SaveAsync(CancellationToken cancellationToken = d } } - public async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) + public virtual async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) { try { diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs index 8fae6f3..ce9dd07 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs @@ -67,7 +67,7 @@ protected virtual TDbContext InitDbContext() { throw new MixException( MixErrorStatus.ServerError, - $"{dbContextType}: Contructor Parameterless Notfound"); + $"{dbContextType}: Constructor Parameterless Notfound"); } return (TDbContext)contextCtorInfo.Invoke([]); diff --git a/src/mix.heart/ViewModel/ViewModelBase.Async.cs b/src/mix.heart/ViewModel/ViewModelBase.Async.cs deleted file mode 100644 index 7d9aa36..0000000 --- a/src/mix.heart/ViewModel/ViewModelBase.Async.cs +++ /dev/null @@ -1,142 +0,0 @@ -using Mix.Heart.Enums; -using Mix.Heart.Exceptions; -using Mix.Heart.Helpers; -using Mix.Heart.Models; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; - -namespace Mix.Heart.ViewModel -{ - public abstract partial class ViewModelBase - { - #region Async - - public async Task DeleteAsync(CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - await DeleteHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - } - finally - { - await CloseUowAsync(); - } - } - - protected virtual async Task DeleteHandlerAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - Repository.SetUowInfo(UowInfo); - await Repository.DeleteAsync(Id, cancellationToken); - ModifiedEntities.Add(new(typeof(TEntity), Id, ViewModelAction.Delete)); - await DeleteEntityRelationshipAsync(cancellationToken); - } - - public async Task SaveAsync(CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - if (!IsValid) - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); - } - var entity = await SaveHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally - { - await CloseUowAsync(); - } - } - - public async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - foreach (var property in properties) - { - // check if field name is exist - var lamda = ReflectionHelper.GetLambda(property.PropertyName); - if (lamda != null) - { - ReflectionHelper.SetPropertyValue(this, property); - } - else - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, $"Invalid Property {property.PropertyName}")); - } - } - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - var entity = await ParseEntity(cancellationToken); - await Repository.SaveAsync(entity, cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally - { - await CloseUowAsync(); - } - } - - #region virtual methods - - // Override this method - protected virtual async Task SaveHandlerAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - var entity = await ParseEntity(cancellationToken); - - ModifiedEntities.Add(new(typeof(TEntity), Id, !Id.Equals(default) ? ViewModelAction.Create : ViewModelAction.Update)); - - await Repository.SaveAsync(entity, cancellationToken); - await SaveEntityRelationshipAsync(entity, cancellationToken); - Id = entity.Id; - return entity; - } - - // Override this method - protected virtual Task SaveEntityRelationshipAsync(TEntity parentEntity, CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - return Task.CompletedTask; - } - - protected virtual Task DeleteEntityRelationshipAsync(CancellationToken cancellationToken = default) - { - return Task.CompletedTask; - } - - #endregion - - #endregion - } -} diff --git a/src/mix.heart/ViewModel/ViewModelBase.Uow.cs b/src/mix.heart/ViewModel/ViewModelBase.Uow.cs deleted file mode 100644 index d602e27..0000000 --- a/src/mix.heart/ViewModel/ViewModelBase.Uow.cs +++ /dev/null @@ -1,76 +0,0 @@ -using Mix.Heart.Enums; -using Mix.Heart.Exceptions; -using Mix.Heart.Services; -using Mix.Heart.UnitOfWork; -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace Mix.Heart.ViewModel -{ - public abstract partial class ViewModelBase - { - private bool _isRoot; - - protected virtual void BeginUow() - { - if (UowInfo == null) - { - InitRootUow(); - } - - UowInfo.Begin(); - - if (Repository != null) - { - Repository.SetUowInfo(UowInfo); - } - else - { - Repository = GetRepository(UowInfo, CacheService); - } - Repository.SetCacheService(CacheService); - Repository.CacheFolder = CacheFolder; - } - - protected virtual void InitRootUow() - { - UowInfo ??= new UnitOfWorkInfo(InitDbContext()); - _isRoot = true; - } - - protected virtual async Task CloseUowAsync() - { - if (_isRoot) - { - await UowInfo.CloseAsync(); - } - } - - protected virtual async Task CompleteUowAsync(CancellationToken cancellationToken = default) - { - if (_isRoot) - { - await UowInfo.CompleteAsync(cancellationToken); - return; - }; - - _isRoot = false; - } - - protected virtual TDbContext InitDbContext() - { - var dbContextType = typeof(TDbContext); - var contextCtorInfo = dbContextType.GetConstructor(new Type[] { }); - - if (contextCtorInfo == null) - { - throw new MixException( - MixErrorStatus.ServerError, - $"{dbContextType}: Contructor Parameterless Notfound"); - } - - return (TDbContext)contextCtorInfo.Invoke([]); - } - } -} diff --git a/src/mix.heart/ViewModel/ViewModelBase.cs b/src/mix.heart/ViewModel/ViewModelBase.cs index 837efc4..d4da076 100644 --- a/src/mix.heart/ViewModel/ViewModelBase.cs +++ b/src/mix.heart/ViewModel/ViewModelBase.cs @@ -1,13 +1,8 @@ using Microsoft.EntityFrameworkCore; using Mix.Heart.Entities; using Mix.Heart.Enums; -using Mix.Heart.Exceptions; -using Mix.Heart.Helpers; using Mix.Heart.UnitOfWork; using System; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; namespace Mix.Heart.ViewModel { @@ -55,8 +50,9 @@ public ViewModelBase(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo) #endregion - #region Abstracts - public virtual void InitDefaultValues(string language = null, int? cultureId = null) + #region Overrides + + public override void InitDefaultValues(string language = null, int? cultureId = null) { CreatedDateTime = DateTime.UtcNow; Status = MixContentStatus.Published; @@ -64,70 +60,5 @@ public virtual void InitDefaultValues(string language = null, int? cultureId = n } #endregion - - public virtual async Task Validate(CancellationToken cancellationToken) - { - cancellationToken.ThrowIfCancellationRequested(); - if (!IsValid) - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); - } - } - - public void SetDbContext(TDbContext context) - { - UowInfo = new UnitOfWorkInfo(context); - } - - public virtual TEntity InitModel() - { - Type classType = typeof(TEntity); - return (TEntity)Activator.CreateInstance(classType); - } - - public virtual Task ParseEntity(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - - if (IsDefaultId(Id)) - { - InitDefaultValues(); - } - - var entity = Activator.CreateInstance(); - ReflectionHelper.Map(this as TView, entity); - return Task.FromResult(entity); - } - - public bool IsDefaultId(TPrimaryKey id) - { - return (id.GetType() == typeof(Guid) && Guid.Parse(id.ToString()) == Guid.Empty) - || (id.GetType() == typeof(int) && int.Parse(id.ToString()) == default); - } - - public virtual Task DuplicateAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - return Task.CompletedTask; - } - - public virtual void Duplicate() - { - } - - protected async Task HandleErrorsAsync() - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); - } - - protected virtual async Task HandleExceptionAsync(Exception ex) - { - await Repository.HandleExceptionAsync(ex); - } - - protected virtual void HandleException(Exception ex) - { - Repository.HandleException(ex); - } } } From f437c1de2af42544986fef790c9afe81ab4ffd4e Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 6 Oct 2024 16:34:57 +0000 Subject: [PATCH 2/3] Restyled by astyle --- .../ViewModel/SimpleViewModelBase.Async.cs | 204 +++++++++--------- .../ViewModel/SimpleViewModelBase.Uow.cs | 96 ++++----- src/mix.heart/ViewModel/ViewModelBase.cs | 97 +++++---- 3 files changed, 209 insertions(+), 188 deletions(-) diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs index a8b54c6..7c439a4 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs @@ -11,132 +11,132 @@ namespace Mix.Heart.ViewModel { - public abstract partial class SimpleViewModelBase - { - #region Async +public abstract partial class SimpleViewModelBase +{ + #region Async - public virtual async Task DeleteAsync(CancellationToken cancellationToken = default) + public virtual async Task DeleteAsync(CancellationToken cancellationToken = default) + { + try { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - await DeleteHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - } - finally - { - await CloseUowAsync(); - } + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + await DeleteHandlerAsync(cancellationToken); + await CompleteUowAsync(cancellationToken); } - - protected virtual async Task DeleteHandlerAsync(CancellationToken cancellationToken = default) + catch (Exception ex) { - cancellationToken.ThrowIfCancellationRequested(); - Repository.SetUowInfo(UowInfo); - await Repository.DeleteAsync(Id, cancellationToken); - ModifiedEntities.Add(new(typeof(TEntity), Id, ViewModelAction.Delete)); - await DeleteEntityRelationshipAsync(cancellationToken); + await HandleExceptionAsync(ex); } + finally + { + await CloseUowAsync(); + } + } - public virtual async Task SaveAsync(CancellationToken cancellationToken = default) + protected virtual async Task DeleteHandlerAsync(CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + Repository.SetUowInfo(UowInfo); + await Repository.DeleteAsync(Id, cancellationToken); + ModifiedEntities.Add(new(typeof(TEntity), Id, ViewModelAction.Delete)); + await DeleteEntityRelationshipAsync(cancellationToken); + } + + public virtual async Task SaveAsync(CancellationToken cancellationToken = default) + { + try { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - if (!IsValid) - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); - } - var entity = await SaveHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); + await Validate(cancellationToken); + if (!IsValid) { - await CloseUowAsync(); + await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); } + var entity = await SaveHandlerAsync(cancellationToken); + await CompleteUowAsync(cancellationToken); + return entity.Id; + } + catch (Exception ex) + { + await HandleExceptionAsync(ex); + return default; } + finally + { + await CloseUowAsync(); + } + } - public virtual async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) + public virtual async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) + { + try { - try + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + foreach (var property in properties) { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - foreach (var property in properties) + // check if field name is exist + var lamda = ReflectionHelper.GetLambda(property.PropertyName); + if (lamda != null) { - // check if field name is exist - var lamda = ReflectionHelper.GetLambda(property.PropertyName); - if (lamda != null) - { - ReflectionHelper.SetPropertyValue(this, property); - } - else - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, $"Invalid Property {property.PropertyName}")); - } + ReflectionHelper.SetPropertyValue(this, property); + } + else + { + await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, $"Invalid Property {property.PropertyName}")); } - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - var entity = await ParseEntity(cancellationToken); - await Repository.SaveAsync(entity, cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally - { - await CloseUowAsync(); } - } - - #region virtual methods - - // Override this method - protected virtual async Task SaveHandlerAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); + IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); + await Validate(cancellationToken); var entity = await ParseEntity(cancellationToken); - - ModifiedEntities.Add(new(typeof(TEntity), Id, !Id.Equals(default) ? ViewModelAction.Create : ViewModelAction.Update)); - await Repository.SaveAsync(entity, cancellationToken); - await SaveEntityRelationshipAsync(entity, cancellationToken); - Id = entity.Id; - return entity; + await CompleteUowAsync(cancellationToken); + return entity.Id; } - - // Override this method - protected virtual Task SaveEntityRelationshipAsync(TEntity parentEntity, CancellationToken cancellationToken = default) + catch (Exception ex) { - cancellationToken.ThrowIfCancellationRequested(); - return Task.CompletedTask; + await HandleExceptionAsync(ex); + return default; } - - protected virtual Task DeleteEntityRelationshipAsync(CancellationToken cancellationToken = default) + finally { - return Task.CompletedTask; + await CloseUowAsync(); } + } + + #region virtual methods - #endregion + // Override this method + protected virtual async Task SaveHandlerAsync(CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + var entity = await ParseEntity(cancellationToken); + + ModifiedEntities.Add(new(typeof(TEntity), Id, !Id.Equals(default) ? ViewModelAction.Create : ViewModelAction.Update)); - #endregion + await Repository.SaveAsync(entity, cancellationToken); + await SaveEntityRelationshipAsync(entity, cancellationToken); + Id = entity.Id; + return entity; } + + // Override this method + protected virtual Task SaveEntityRelationshipAsync(TEntity parentEntity, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; + } + + protected virtual Task DeleteEntityRelationshipAsync(CancellationToken cancellationToken = default) + { + return Task.CompletedTask; + } + + #endregion + + #endregion +} } diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs index ce9dd07..1e8bc7b 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs @@ -8,69 +8,69 @@ namespace Mix.Heart.ViewModel { - public abstract partial class SimpleViewModelBase - { - private bool _isRoot; +public abstract partial class SimpleViewModelBase +{ + private bool _isRoot; - protected virtual void BeginUow() + protected virtual void BeginUow() + { + if (UowInfo == null) { - if (UowInfo == null) - { - InitRootUow(); - } - - UowInfo.Begin(); - - if (Repository != null) - { - Repository.SetUowInfo(UowInfo); - } - else - { - Repository = GetRepository(UowInfo, CacheService); - } - Repository.SetCacheService(CacheService); - Repository.CacheFolder = CacheFolder; + InitRootUow(); } - protected virtual void InitRootUow() + UowInfo.Begin(); + + if (Repository != null) { - UowInfo ??= new UnitOfWorkInfo(InitDbContext()); - _isRoot = true; + Repository.SetUowInfo(UowInfo); } - - protected virtual async Task CloseUowAsync() + else { - if (_isRoot) - { - await UowInfo.CloseAsync(); - } + Repository = GetRepository(UowInfo, CacheService); } + Repository.SetCacheService(CacheService); + Repository.CacheFolder = CacheFolder; + } - protected virtual async Task CompleteUowAsync(CancellationToken cancellationToken = default) - { - if (_isRoot) - { - await UowInfo.CompleteAsync(cancellationToken); - return; - }; + protected virtual void InitRootUow() + { + UowInfo ??= new UnitOfWorkInfo(InitDbContext()); + _isRoot = true; + } - _isRoot = false; + protected virtual async Task CloseUowAsync() + { + if (_isRoot) + { + await UowInfo.CloseAsync(); } + } - protected virtual TDbContext InitDbContext() + protected virtual async Task CompleteUowAsync(CancellationToken cancellationToken = default) + { + if (_isRoot) { - var dbContextType = typeof(TDbContext); - var contextCtorInfo = dbContextType.GetConstructor(new Type[] { }); + await UowInfo.CompleteAsync(cancellationToken); + return; + }; + + _isRoot = false; + } - if (contextCtorInfo == null) - { - throw new MixException( - MixErrorStatus.ServerError, - $"{dbContextType}: Constructor Parameterless Notfound"); - } + protected virtual TDbContext InitDbContext() + { + var dbContextType = typeof(TDbContext); + var contextCtorInfo = dbContextType.GetConstructor(new Type[] { }); - return (TDbContext)contextCtorInfo.Invoke([]); + if (contextCtorInfo == null) + { + throw new MixException( + MixErrorStatus.ServerError, + $"{dbContextType}: Constructor Parameterless Notfound"); } + + return (TDbContext)contextCtorInfo.Invoke([]); } } +} diff --git a/src/mix.heart/ViewModel/ViewModelBase.cs b/src/mix.heart/ViewModel/ViewModelBase.cs index d4da076..1ac8a41 100644 --- a/src/mix.heart/ViewModel/ViewModelBase.cs +++ b/src/mix.heart/ViewModel/ViewModelBase.cs @@ -6,59 +6,80 @@ namespace Mix.Heart.ViewModel { - public abstract partial class ViewModelBase : SimpleViewModelBase - where TPrimaryKey : IComparable - where TEntity : class, IEntity - where TDbContext : DbContext - where TView : ViewModelBase - { - #region Properties - - public DateTime CreatedDateTime { get; set; } +public abstract partial class ViewModelBase : SimpleViewModelBase + where TPrimaryKey : IComparable + where TEntity : class, IEntity + where TDbContext : DbContext + where TView : ViewModelBase +{ + #region Properties - public DateTime? LastModified { get; set; } + public DateTime CreatedDateTime { + get; + set; + } - public string CreatedBy { get; set; } + public DateTime? LastModified { + get; + set; + } - public string ModifiedBy { get; set; } + public string CreatedBy { + get; + set; + } - public int Priority { get; set; } + public string ModifiedBy { + get; + set; + } - public MixContentStatus Status { get; set; } = MixContentStatus.Published; + public int Priority { + get; + set; + } - public bool IsDeleted { get; set; } + public MixContentStatus Status { + get; + set; + } = MixContentStatus.Published; - #endregion + public bool IsDeleted { + get; + set; + } - #region Constructors + #endregion - public ViewModelBase() : base() - { - } + #region Constructors - public ViewModelBase(TDbContext context) : base(context) - { - } + public ViewModelBase() : base() + { + } - public ViewModelBase(TEntity entity, UnitOfWorkInfo uowInfo) : base(entity, uowInfo) - { - } + public ViewModelBase(TDbContext context) : base(context) + { + } - public ViewModelBase(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo) - { - } + public ViewModelBase(TEntity entity, UnitOfWorkInfo uowInfo) : base(entity, uowInfo) + { + } - #endregion + public ViewModelBase(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo) + { + } - #region Overrides + #endregion - public override void InitDefaultValues(string language = null, int? cultureId = null) - { - CreatedDateTime = DateTime.UtcNow; - Status = MixContentStatus.Published; - IsDeleted = false; - } + #region Overrides - #endregion + public override void InitDefaultValues(string language = null, int? cultureId = null) + { + CreatedDateTime = DateTime.UtcNow; + Status = MixContentStatus.Published; + IsDeleted = false; } + + #endregion +} } From d41282a4974b1f5746ddb61542bdafa8f9e439e8 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Sun, 6 Oct 2024 16:34:58 +0000 Subject: [PATCH 3/3] Restyled by clang-format --- .../ViewModel/SimpleViewModelBase.Async.cs | 213 ++++++++---------- .../ViewModel/SimpleViewModelBase.Uow.cs | 96 ++++---- src/mix.heart/ViewModel/ViewModelBase.cs | 86 +++---- 3 files changed, 171 insertions(+), 224 deletions(-) diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs index 7c439a4..5725894 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs @@ -9,134 +9,121 @@ using System.Threading; using System.Threading.Tasks; -namespace Mix.Heart.ViewModel -{ -public abstract partial class SimpleViewModelBase -{ - #region Async +namespace Mix.Heart.ViewModel { +public abstract + partial class SimpleViewModelBase { +#region Async - public virtual async Task DeleteAsync(CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - await DeleteHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - } - finally - { - await CloseUowAsync(); - } + public virtual async + Task DeleteAsync(CancellationToken cancellationToken = default) { + try { + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + await DeleteHandlerAsync(cancellationToken); + await CompleteUowAsync(cancellationToken); + } catch (Exception ex) { + await HandleExceptionAsync(ex); + } finally { + await CloseUowAsync(); } + } - protected virtual async Task DeleteHandlerAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - Repository.SetUowInfo(UowInfo); - await Repository.DeleteAsync(Id, cancellationToken); - ModifiedEntities.Add(new(typeof(TEntity), Id, ViewModelAction.Delete)); - await DeleteEntityRelationshipAsync(cancellationToken); - } + protected virtual async + Task DeleteHandlerAsync(CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + Repository.SetUowInfo(UowInfo); + await Repository.DeleteAsync(Id, cancellationToken); + ModifiedEntities.Add(new(typeof(TEntity), Id, ViewModelAction.Delete)); + await DeleteEntityRelationshipAsync(cancellationToken); + } - public virtual async Task SaveAsync(CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - if (!IsValid) - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, Errors.Select(e => e.ErrorMessage).ToArray())); - } - var entity = await SaveHandlerAsync(cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally - { - await CloseUowAsync(); - } + public virtual async Task + SaveAsync(CancellationToken cancellationToken = default) { + try { + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); + await Validate(cancellationToken); + if (!IsValid) { + await HandleExceptionAsync( + new MixException(MixErrorStatus.Badrequest, + Errors.Select(e => e.ErrorMessage).ToArray())); + } + var entity = await SaveHandlerAsync(cancellationToken); + await CompleteUowAsync(cancellationToken); + return entity.Id; + } catch (Exception ex) { + await HandleExceptionAsync(ex); + return default; + } finally { + await CloseUowAsync(); } + } - public virtual async Task SaveFieldsAsync(IEnumerable properties, CancellationToken cancellationToken = default) - { - try - { - cancellationToken.ThrowIfCancellationRequested(); - BeginUow(); - foreach (var property in properties) - { - // check if field name is exist - var lamda = ReflectionHelper.GetLambda(property.PropertyName); - if (lamda != null) - { - ReflectionHelper.SetPropertyValue(this, property); - } - else - { - await HandleExceptionAsync(new MixException(MixErrorStatus.Badrequest, $"Invalid Property {property.PropertyName}")); - } - } - IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); - await Validate(cancellationToken); - var entity = await ParseEntity(cancellationToken); - await Repository.SaveAsync(entity, cancellationToken); - await CompleteUowAsync(cancellationToken); - return entity.Id; - } - catch (Exception ex) - { - await HandleExceptionAsync(ex); - return default; - } - finally - { - await CloseUowAsync(); + public virtual async Task + SaveFieldsAsync(IEnumerable properties, + CancellationToken cancellationToken = default) { + try { + cancellationToken.ThrowIfCancellationRequested(); + BeginUow(); + foreach (var property in properties) { + // check if field name is exist + var lamda = ReflectionHelper.GetLambda(property.PropertyName); + if (lamda != null) { + ReflectionHelper.SetPropertyValue(this, property); + } else { + await HandleExceptionAsync( + new MixException(MixErrorStatus.Badrequest, + $"Invalid Property {property.PropertyName}")); } + } + IsValid = Validator.TryValidateObject(this, ValidateContext, Errors); + await Validate(cancellationToken); + var entity = await ParseEntity(cancellationToken); + await Repository.SaveAsync(entity, cancellationToken); + await CompleteUowAsync(cancellationToken); + return entity.Id; + } catch (Exception ex) { + await HandleExceptionAsync(ex); + return default; + } finally { + await CloseUowAsync(); } + } - #region virtual methods +#region virtual methods - // Override this method - protected virtual async Task SaveHandlerAsync(CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - var entity = await ParseEntity(cancellationToken); + // Override this method + protected virtual async Task + SaveHandlerAsync(CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + var entity = await ParseEntity(cancellationToken); - ModifiedEntities.Add(new(typeof(TEntity), Id, !Id.Equals(default) ? ViewModelAction.Create : ViewModelAction.Update)); + ModifiedEntities.Add(new(typeof(TEntity), Id, + !Id.Equals(default) ? ViewModelAction.Create + : ViewModelAction.Update)); - await Repository.SaveAsync(entity, cancellationToken); - await SaveEntityRelationshipAsync(entity, cancellationToken); - Id = entity.Id; - return entity; - } + await Repository.SaveAsync(entity, cancellationToken); + await SaveEntityRelationshipAsync(entity, cancellationToken); + Id = entity.Id; + return entity; + } - // Override this method - protected virtual Task SaveEntityRelationshipAsync(TEntity parentEntity, CancellationToken cancellationToken = default) - { - cancellationToken.ThrowIfCancellationRequested(); - return Task.CompletedTask; - } + // Override this method + protected virtual Task + SaveEntityRelationshipAsync(TEntity parentEntity, + CancellationToken cancellationToken = default) { + cancellationToken.ThrowIfCancellationRequested(); + return Task.CompletedTask; + } - protected virtual Task DeleteEntityRelationshipAsync(CancellationToken cancellationToken = default) - { - return Task.CompletedTask; - } + protected virtual Task + DeleteEntityRelationshipAsync(CancellationToken cancellationToken = default) { + return Task.CompletedTask; + } - #endregion +#endregion - #endregion +#endregion } } diff --git a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs index 1e8bc7b..2b69595 100644 --- a/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs +++ b/src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs @@ -6,71 +6,59 @@ using System.Threading; using System.Threading.Tasks; -namespace Mix.Heart.ViewModel -{ -public abstract partial class SimpleViewModelBase -{ - private bool _isRoot; +namespace Mix.Heart.ViewModel { +public abstract + partial class SimpleViewModelBase { + private bool _isRoot; - protected virtual void BeginUow() - { - if (UowInfo == null) - { - InitRootUow(); - } - - UowInfo.Begin(); - - if (Repository != null) - { - Repository.SetUowInfo(UowInfo); - } - else - { - Repository = GetRepository(UowInfo, CacheService); - } - Repository.SetCacheService(CacheService); - Repository.CacheFolder = CacheFolder; + protected virtual void BeginUow() { + if (UowInfo == null) { + InitRootUow(); } - protected virtual void InitRootUow() - { - UowInfo ??= new UnitOfWorkInfo(InitDbContext()); - _isRoot = true; - } + UowInfo.Begin(); - protected virtual async Task CloseUowAsync() - { - if (_isRoot) - { - await UowInfo.CloseAsync(); - } + if (Repository != null) { + Repository.SetUowInfo(UowInfo); + } else { + Repository = GetRepository(UowInfo, CacheService); } + Repository.SetCacheService(CacheService); + Repository.CacheFolder = CacheFolder; + } - protected virtual async Task CompleteUowAsync(CancellationToken cancellationToken = default) - { - if (_isRoot) - { - await UowInfo.CompleteAsync(cancellationToken); - return; - }; + protected virtual void InitRootUow() { + UowInfo ??= new UnitOfWorkInfo(InitDbContext()); + _isRoot = true; + } - _isRoot = false; + protected virtual async Task CloseUowAsync() { + if (_isRoot) { + await UowInfo.CloseAsync(); } + } - protected virtual TDbContext InitDbContext() - { - var dbContextType = typeof(TDbContext); - var contextCtorInfo = dbContextType.GetConstructor(new Type[] { }); + protected virtual async + Task CompleteUowAsync(CancellationToken cancellationToken = default) { + if (_isRoot) { + await UowInfo.CompleteAsync(cancellationToken); + return; + }; - if (contextCtorInfo == null) - { - throw new MixException( - MixErrorStatus.ServerError, - $"{dbContextType}: Constructor Parameterless Notfound"); - } + _isRoot = false; + } - return (TDbContext)contextCtorInfo.Invoke([]); + protected virtual TDbContext InitDbContext() { + var dbContextType = typeof(TDbContext); + var contextCtorInfo = dbContextType.GetConstructor(new Type[] {}); + + if (contextCtorInfo == null) { + throw new MixException( + MixErrorStatus.ServerError, + $"{dbContextType}: Constructor Parameterless Notfound"); } + + return (TDbContext)contextCtorInfo.Invoke([]); + } } } diff --git a/src/mix.heart/ViewModel/ViewModelBase.cs b/src/mix.heart/ViewModel/ViewModelBase.cs index 1ac8a41..5d2eb5f 100644 --- a/src/mix.heart/ViewModel/ViewModelBase.cs +++ b/src/mix.heart/ViewModel/ViewModelBase.cs @@ -4,82 +4,54 @@ using Mix.Heart.UnitOfWork; using System; -namespace Mix.Heart.ViewModel -{ -public abstract partial class ViewModelBase : SimpleViewModelBase +namespace Mix.Heart.ViewModel { +public abstract + partial class ViewModelBase + : SimpleViewModelBase where TPrimaryKey : IComparable where TEntity : class, IEntity where TDbContext : DbContext - where TView : ViewModelBase -{ - #region Properties + where TView : ViewModelBase { +#region Properties - public DateTime CreatedDateTime { - get; - set; - } + public DateTime CreatedDateTime { get; set; } - public DateTime? LastModified { - get; - set; - } + public DateTime? LastModified { get; set; } - public string CreatedBy { - get; - set; - } + public string CreatedBy { get; set; } - public string ModifiedBy { - get; - set; - } + public string ModifiedBy { get; set; } - public int Priority { - get; - set; - } + public int Priority { get; set; } - public MixContentStatus Status { - get; - set; - } = MixContentStatus.Published; + public MixContentStatus Status { get; set; } = MixContentStatus.Published; - public bool IsDeleted { - get; - set; - } + public bool IsDeleted { get; set; } - #endregion +#endregion - #region Constructors +#region Constructors - public ViewModelBase() : base() - { - } + public ViewModelBase() : base() {} - public ViewModelBase(TDbContext context) : base(context) - { - } + public ViewModelBase(TDbContext context) : base(context) {} - public ViewModelBase(TEntity entity, UnitOfWorkInfo uowInfo) : base(entity, uowInfo) - { - } + public ViewModelBase(TEntity entity, UnitOfWorkInfo uowInfo) + : base(entity, uowInfo) {} - public ViewModelBase(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo) - { - } + public ViewModelBase(UnitOfWorkInfo unitOfWorkInfo) : base(unitOfWorkInfo) {} - #endregion +#endregion - #region Overrides +#region Overrides - public override void InitDefaultValues(string language = null, int? cultureId = null) - { - CreatedDateTime = DateTime.UtcNow; - Status = MixContentStatus.Published; - IsDeleted = false; - } + public override void InitDefaultValues(string language = null, + int? cultureId = null) { + CreatedDateTime = DateTime.UtcNow; + Status = MixContentStatus.Published; + IsDeleted = false; + } - #endregion +#endregion } }