Skip to content

Commit

Permalink
Rename to ErrorOr
Browse files Browse the repository at this point in the history
  • Loading branch information
SeriaWei committed Nov 14, 2024
1 parent f0c4235 commit eef8f8f
Show file tree
Hide file tree
Showing 73 changed files with 265 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Easy.RepositoryPattern
namespace Easy
{
public class Error : Violation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Easy.RepositoryPattern
namespace Easy
{
public class ServiceResult
public class ErrorOr
{
private readonly List<Violation> _ruleViolations;
public ServiceResult()
public ErrorOr()
{
_ruleViolations = new List<Violation>();
}
Expand Down Expand Up @@ -138,53 +139,53 @@ public string InfoMessage
}
}

public static implicit operator ServiceResult(Error error)
public static implicit operator ErrorOr(Error error)
{
var result = new ServiceResult();
var result = new ErrorOr();
result._ruleViolations.Add(error);
return result;
}
public static implicit operator ServiceResult(Warning warning)
public static implicit operator ErrorOr(Warning warning)
{
var result = new ServiceResult();
var result = new ErrorOr();
result._ruleViolations.Add(warning);
return result;
}
public static implicit operator ServiceResult(Info info)
public static implicit operator ErrorOr(Info info)
{
var result = new ServiceResult();
var result = new ErrorOr();
result._ruleViolations.Add(info);
return result;
}
}
public class ServiceResult<T> : ServiceResult
public class ErrorOr<T> : ErrorOr
{
public ServiceResult() { }
public ServiceResult(T result)
public ErrorOr() { }
public ErrorOr(T result)
{
Result = result;
}
public T Result { get; set; }

public static implicit operator ServiceResult<T>(T result)
public static implicit operator ErrorOr<T>(T result)
{
return new ServiceResult<T>(result);
return new ErrorOr<T>(result);
}
public static implicit operator ServiceResult<T>(Error error)
public static implicit operator ErrorOr<T>(Error error)
{
var result = new ServiceResult<T>();
var result = new ErrorOr<T>();
result.AddError(error);
return result;
}
public static implicit operator ServiceResult<T>(Warning warning)
public static implicit operator ErrorOr<T>(Warning warning)
{
var result = new ServiceResult<T>();
var result = new ErrorOr<T>();
result.AddWarning(warning);
return result;
}
public static implicit operator ServiceResult<T>(Info info)
public static implicit operator ErrorOr<T>(Info info)
{
var result = new ServiceResult<T>();
var result = new ErrorOr<T>();
result.AddInfo(info);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Easy.RepositoryPattern
namespace Easy
{
public class Info : Violation
{
Expand Down
6 changes: 3 additions & 3 deletions src/EasyFrameWork/Modules/MutiLanguage/ILanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace Easy.Modules.MutiLanguage
{
public interface ILanguageService
{
ServiceResult<LanguageEntity> Add(LanguageEntity entity);
ServiceResult<LanguageEntity> AddOrUpdate(LanguageEntity entity);
ErrorOr<LanguageEntity> Add(LanguageEntity entity);
ErrorOr<LanguageEntity> AddOrUpdate(LanguageEntity entity);
IEnumerable<LanguageEntity> Get(Expression<Func<LanguageEntity, bool>> expression, Pagination pagination);
IEnumerable<LanguageEntity> GetCultures(string lanKey);
LanguageEntity Get(string lanKey, string cultureName);
ServiceResult<LanguageEntity> Update(LanguageEntity entity);
ErrorOr<LanguageEntity> Update(LanguageEntity entity);
string[] GetCultureCodes();
}
}
10 changes: 5 additions & 5 deletions src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ private ConcurrentDictionary<string, ConcurrentDictionary<string, LanguageEntity
});
}

private ServiceResult<LanguageEntity> Save(LanguageEntity item)
private ErrorOr<LanguageEntity> Save(LanguageEntity item)
{
var result = new ServiceResult<LanguageEntity>();
var result = new ErrorOr<LanguageEntity>();
var localeFile = Path.Combine(GetLocaleDirectory(), $"{item.CultureName}.yml");
try
{
Expand All @@ -122,7 +122,7 @@ private ServiceResult<LanguageEntity> Save(LanguageEntity item)
return result;
}

public ServiceResult<LanguageEntity> Add(LanguageEntity item)
public ErrorOr<LanguageEntity> Add(LanguageEntity item)
{
return Save(item);
}
Expand All @@ -146,13 +146,13 @@ public IEnumerable<LanguageEntity> GetCultures(string lanKey)
}
}
}
public ServiceResult<LanguageEntity> Update(LanguageEntity item)
public ErrorOr<LanguageEntity> Update(LanguageEntity item)
{
return Save(item);
}


public ServiceResult<LanguageEntity> AddOrUpdate(LanguageEntity entity)
public ErrorOr<LanguageEntity> AddOrUpdate(LanguageEntity entity)
{
LanguageEntity translate = Get(entity.LanKey, entity.CultureName);
if (translate == null)
Expand Down
4 changes: 2 additions & 2 deletions src/EasyFrameWork/Modules/Role/IRoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Easy.Modules.Role
public interface IRoleService : IService<RoleEntity>
{
IList<Permission> GetPermission(int roleId);
ServiceResult<RoleEntity> Add(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors);
ServiceResult<RoleEntity> Update(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors);
ErrorOr<RoleEntity> Add(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors);
ErrorOr<RoleEntity> Update(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors);
}
}
4 changes: 2 additions & 2 deletions src/EasyFrameWork/Modules/Role/RoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override DbSet<RoleEntity> CurrentDbSet
}
}

public ServiceResult<RoleEntity> Add(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors)
public ErrorOr<RoleEntity> Add(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors)
{
return BeginTransaction(() =>
{
Expand Down Expand Up @@ -60,7 +60,7 @@ public IList<Permission> GetPermission(int roleId)
return _permissionService.Get(m => m.RoleId == roleId);
}

public ServiceResult<RoleEntity> Update(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors)
public ErrorOr<RoleEntity> Update(RoleEntity roleEntity, List<PermissionDescriptor> permissionDescriptors)
{
return BeginTransaction(() =>
{
Expand Down
4 changes: 2 additions & 2 deletions src/EasyFrameWork/Modules/User/Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private string ProtectPassWord(string passWord)
}
return passWord;
}
public override ServiceResult<UserEntity> Add(UserEntity item)
public override ErrorOr<UserEntity> Add(UserEntity item)
{
if (item.UserID.IsNullOrEmpty() && item.Email.IsNotNullAndWhiteSpace())
{
Expand Down Expand Up @@ -100,7 +100,7 @@ public override ServiceResult<UserEntity> Add(UserEntity item)
return result;
}

public override ServiceResult<UserEntity> Update(UserEntity item)
public override ErrorOr<UserEntity> Update(UserEntity item)
{
if (item.PassWordNew.IsNotNullAndWhiteSpace())
{
Expand Down
8 changes: 4 additions & 4 deletions src/EasyFrameWork/RepositoryPattern/IService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public interface IService<T> : IDisposable
{
IApplicationContext ApplicationContext { get; set; }
void BeginTransaction(Action action);
ServiceResult<T> Add(T item);
ServiceResult<T> AddRange(params T[] items);
ErrorOr<T> Add(T item);
ErrorOr<T> AddRange(params T[] items);
IQueryable<T> Get();
T GetSingle(Expression<Func<T, bool>> filter);
Task<T> GetSingleAsync(Expression<Func<T, bool>> filter);
Expand All @@ -30,8 +30,8 @@ public interface IService<T> : IDisposable
Task<T> GetAsync(params object[] primaryKey);
int Count(Expression<Func<T, bool>> filter);
Task<int> CountAsync(Expression<Func<T, bool>> filter);
ServiceResult<T> Update(T item);
ServiceResult<T> UpdateRange(params T[] items);
ErrorOr<T> Update(T item);
ErrorOr<T> UpdateRange(params T[] items);
void Remove(params object[] primaryKey);
void Remove(T item);
void Remove(Expression<Func<T, bool>> filter);
Expand Down
18 changes: 9 additions & 9 deletions src/EasyFrameWork/RepositoryPattern/ServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TEntity BeginTransaction<TEntity>(Func<TEntity> action)
try
{
var result = action.Invoke();
if (result is ServiceResult sResult && sResult.HasError)
if (result is ErrorOr sResult && sResult.HasError)
{
transaction.Rollback();
}
Expand All @@ -94,9 +94,9 @@ public TEntity BeginTransaction<TEntity>(Func<TEntity> action)
return action.Invoke();
}
}
protected virtual ServiceResult<T> Validate(T item)
protected virtual ErrorOr<T> Validate(T item)
{
ServiceResult<T> serviceResult = new ServiceResult<T>();
ErrorOr<T> serviceResult = new ErrorOr<T>();
var entryType = typeof(T);
var viewConfig = ServiceLocator.GetViewConfigure(typeof(T));
if (viewConfig != null)
Expand All @@ -123,7 +123,7 @@ protected virtual ServiceResult<T> Validate(T item)
serviceResult.Result = item;
return serviceResult;
}
public virtual ServiceResult<T> Add(T item)
public virtual ErrorOr<T> Add(T item)
{
var result = Validate(item);
if (result.HasError)
Expand All @@ -148,9 +148,9 @@ public virtual ServiceResult<T> Add(T item)
SaveChanges();
return result;
}
public virtual ServiceResult<T> AddRange(params T[] items)
public virtual ErrorOr<T> AddRange(params T[] items)
{
ServiceResult<T> result = new ServiceResult<T>();
ErrorOr<T> result = new ErrorOr<T>();
foreach (var item in items)
{
var itemResult = Validate(item);
Expand Down Expand Up @@ -284,7 +284,7 @@ public virtual async Task<int> CountAsync(Expression<Func<T, bool>> filter)
}
return await Get().CountAsync();
}
public virtual ServiceResult<T> Update(T item)
public virtual ErrorOr<T> Update(T item)
{
var result = Validate(item);
if (result.HasError)
Expand All @@ -305,7 +305,7 @@ public virtual ServiceResult<T> Update(T item)
SaveChanges();
return result;
}
public virtual ServiceResult<T> UpdateRange(params T[] items)
public virtual ErrorOr<T> UpdateRange(params T[] items)
{
foreach (var item in items)
{
Expand All @@ -327,7 +327,7 @@ public virtual ServiceResult<T> UpdateRange(params T[] items)
}
CurrentDbSet.UpdateRange(items);
SaveChanges();
return new ServiceResult<T>();
return new ErrorOr<T>();
}
public void Remove(params object[] primaryKey)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Easy.RepositoryPattern
namespace Easy
{
public class Warning : Violation
{
Expand Down
10 changes: 5 additions & 5 deletions src/ZKEACMS.Article/Service/ArticleApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

using Easy;
using Easy.Mvc.Authorize;
using Easy.RepositoryPattern;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -55,7 +55,7 @@ public ArticleEntity GetByName(string name)
}
return article;
}
public ServiceResult<ArticleEntity> Create(ArticleEntity article)
public ErrorOr<ArticleEntity> Create(ArticleEntity article)
{
var validResult = ValidArticleType(article);
if (validResult.HasError) return validResult;
Expand All @@ -64,7 +64,7 @@ public ServiceResult<ArticleEntity> Create(ArticleEntity article)
return _articleService.Add(article);
}

public ServiceResult<ArticleEntity> Update(ArticleEntity article)
public ErrorOr<ArticleEntity> Update(ArticleEntity article)
{
var validResult = ValidArticleType(article);
if (validResult.HasError) return validResult;
Expand All @@ -73,9 +73,9 @@ public ServiceResult<ArticleEntity> Update(ArticleEntity article)
return _articleService.Update(article);
}

private ServiceResult<ArticleEntity> ValidArticleType(ArticleEntity article)
private ErrorOr<ArticleEntity> ValidArticleType(ArticleEntity article)
{
ServiceResult<ArticleEntity> serviceResult = new ServiceResult<ArticleEntity>();
ErrorOr<ArticleEntity> serviceResult = new ErrorOr<ArticleEntity>();
ArticleType articleType = _articleTypeService.Get(article.ArticleTypeID ?? 0);
if (articleType == null)
{
Expand Down
5 changes: 2 additions & 3 deletions src/ZKEACMS.Article/Service/ArticleDetailWidgetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Easy.Extend;
using System.Collections.Concurrent;
using Easy.Cache;
using Easy.RepositoryPattern;
using Easy.Constant;
using ZKEACMS.StructuredData;
using Easy.Serializer;
Expand Down Expand Up @@ -50,13 +49,13 @@ private void DismissRelatedPageUrls()
_cacheManager.Remove(ArticleDetailWidgetRelatedPageUrls);
}

public override ServiceResult<ArticleDetailWidget> Add(ArticleDetailWidget item)
public override ErrorOr<ArticleDetailWidget> Add(ArticleDetailWidget item)
{
var result = base.Add(item);
DismissRelatedPageUrls();
return result;
}
public override ServiceResult<ArticleDetailWidget> Update(ArticleDetailWidget item)
public override ErrorOr<ArticleDetailWidget> Update(ArticleDetailWidget item)
{
var result = base.Update(item);
DismissRelatedPageUrls();
Expand Down
Loading

0 comments on commit eef8f8f

Please sign in to comment.