Skip to content

Commit

Permalink
Restyled by clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
restyled-commits committed Oct 6, 2024
1 parent f437c1d commit d41282a
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 224 deletions.
213 changes: 100 additions & 113 deletions src/mix.heart/ViewModel/SimpleViewModelBase.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,134 +9,121 @@
using System.Threading;
using System.Threading.Tasks;

namespace Mix.Heart.ViewModel
{
public abstract partial class SimpleViewModelBase<TDbContext, TEntity, TPrimaryKey, TView>
{
#region Async
namespace Mix.Heart.ViewModel {
public abstract
partial class SimpleViewModelBase<TDbContext, TEntity, TPrimaryKey, TView> {
#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<TPrimaryKey> 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<TPrimaryKey>
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<TPrimaryKey> SaveFieldsAsync(IEnumerable<EntityPropertyModel> properties, CancellationToken cancellationToken = default)
{
try
{
cancellationToken.ThrowIfCancellationRequested();
BeginUow();
foreach (var property in properties)
{
// check if field name is exist
var lamda = ReflectionHelper.GetLambda<TEntity>(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<TPrimaryKey>
SaveFieldsAsync(IEnumerable<EntityPropertyModel> properties,
CancellationToken cancellationToken = default) {
try {
cancellationToken.ThrowIfCancellationRequested();
BeginUow();
foreach (var property in properties) {
// check if field name is exist
var lamda = ReflectionHelper.GetLambda<TEntity>(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<TEntity> SaveHandlerAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();
var entity = await ParseEntity(cancellationToken);
// Override this method
protected virtual async Task<TEntity>
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
}
}
96 changes: 42 additions & 54 deletions src/mix.heart/ViewModel/SimpleViewModelBase.Uow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,59 @@
using System.Threading;
using System.Threading.Tasks;

namespace Mix.Heart.ViewModel
{
public abstract partial class SimpleViewModelBase<TDbContext, TEntity, TPrimaryKey, TView>
{
private bool _isRoot;
namespace Mix.Heart.ViewModel {
public abstract
partial class SimpleViewModelBase<TDbContext, TEntity, TPrimaryKey, TView> {
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([]);
}
}
}
Loading

0 comments on commit d41282a

Please sign in to comment.