Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restyle Remove redundant code #50

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 104 additions & 117 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 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 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 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
}
}
102 changes: 45 additions & 57 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();
protected virtual void BeginUow() {
if (UowInfo == null) {
InitRootUow();
}

if (Repository != null)
{
Repository.SetUowInfo(UowInfo);
}
else
{
Repository = GetRepository(UowInfo, CacheService);
}
Repository.SetCacheService(CacheService);
Repository.CacheFolder = CacheFolder;
}
UowInfo.Begin();

protected virtual void InitRootUow()
{
UowInfo ??= new UnitOfWorkInfo(InitDbContext());
_isRoot = true;
}
if (Repository != null) {
Repository.SetUowInfo(UowInfo);
} else {
Repository = GetRepository(UowInfo, CacheService);
}
Repository.SetCacheService(CacheService);
Repository.CacheFolder = CacheFolder;
}

protected virtual async Task CloseUowAsync()
{
if (_isRoot)
{
await UowInfo.CloseAsync();
}
}
protected virtual void InitRootUow() {
UowInfo ??= new UnitOfWorkInfo(InitDbContext());
_isRoot = true;
}

protected virtual async Task CompleteUowAsync(CancellationToken cancellationToken = default)
{
if (_isRoot)
{
await UowInfo.CompleteAsync(cancellationToken);
return;
};
protected virtual async Task CloseUowAsync() {
if (_isRoot) {
await UowInfo.CloseAsync();
}
}

_isRoot = false;
}
protected virtual async
Task CompleteUowAsync(CancellationToken cancellationToken = default) {
if (_isRoot) {
await UowInfo.CompleteAsync(cancellationToken);
return;
};

protected virtual TDbContext InitDbContext()
{
var dbContextType = typeof(TDbContext);
var contextCtorInfo = dbContextType.GetConstructor(new Type[] { });
_isRoot = false;
}

if (contextCtorInfo == null)
{
throw new MixException(
MixErrorStatus.ServerError,
$"{dbContextType}: Contructor 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([]);
}
}
}
Loading
Loading