Skip to content

Commit

Permalink
Define Error, Warning, Info in service result.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wayne-KTCSZ committed Nov 14, 2024
1 parent 218d04b commit 7777c70
Show file tree
Hide file tree
Showing 45 changed files with 332 additions and 189 deletions.
2 changes: 1 addition & 1 deletion src/EasyFrameWork/Modules/MutiLanguage/LanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private ServiceResult<LanguageEntity> Save(LanguageEntity item)
}
catch (Exception ex)
{
result.AddRuleViolation(ex.Message);
result.AddError(ex.Message);
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EasyFrameWork/Modules/Role/RoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ServiceResult<RoleEntity> Add(RoleEntity roleEntity, List<PermissionDescr
return BeginTransaction(() =>
{
var result = Add(roleEntity);
if (result.HasViolation) return result;
if (result.HasError) return result;
var permissions = new List<Permission>();
permissionDescriptors.Where(m => m.Checked ?? false).Each(m =>
{
Expand Down
2 changes: 1 addition & 1 deletion src/EasyFrameWork/Modules/User/Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override ServiceResult<UserEntity> Add(UserEntity item)
throw new Exception(_localize.Get("{0} is already exists").FormatWith(item.Email));
}
var result = base.Add(item);
if (!result.HasViolation)
if (!result.HasError)
{
if (item.Roles != null)
{
Expand Down
8 changes: 4 additions & 4 deletions src/EasyFrameWork/Mvc/Controllers/BasicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ public virtual IActionResult Create(TEntity entity)
if (ModelState.IsValid)
{
var result = Service.Add(entity);
if (result.HasViolation)
if (result.HasError)
{
foreach (var item in result.RuleViolations)
{
ModelState.AddModelError(item.ParameterName, item.ErrorMessage);
ModelState.AddModelError(item.ParameterName, item.Message);
}
return View(entity);
}
Expand Down Expand Up @@ -92,11 +92,11 @@ public virtual IActionResult Edit(TEntity entity)
if (ModelState.IsValid)
{
var result = Service.Update(entity);
if (result.HasViolation)
if (result.HasError)
{
foreach (var item in result.RuleViolations)
{
ModelState.AddModelError(item.ParameterName, item.ErrorMessage);
ModelState.AddModelError(item.ParameterName, item.Message);
}
return View(entity);
}
Expand Down
23 changes: 23 additions & 0 deletions src/EasyFrameWork/RepositoryPattern/Error.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* http://www.zkea.net/
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

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

namespace Easy.RepositoryPattern
{
public class Error : Violation
{
public Error(string message) : base(string.Empty, message)
{
}

public Error(string parameterName, string message) : base(parameterName, message)
{
}
}
}
22 changes: 22 additions & 0 deletions src/EasyFrameWork/RepositoryPattern/Info.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* http://www.zkea.net/
* Copyright (c) ZKEASOFT. All rights reserved.
* http://www.zkea.net/licenses */

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

namespace Easy.RepositoryPattern
{
public class Info : Violation
{
public Info(string message) : base(string.Empty, message)
{
}
public Info(string parameterName, string message) : base(parameterName, message)
{
}
}
}
12 changes: 6 additions & 6 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.HasViolation)
if (result is ServiceResult sResult && sResult.HasError)
{
transaction.Rollback();
}
Expand Down Expand Up @@ -112,7 +112,7 @@ protected virtual ServiceResult<T> Validate(T item)
{
if (!v.Validate(p.GetValue(item)))
{
serviceResult.RuleViolations.Add(new RuleViolation(p.Name, v.ErrorMessage));
serviceResult.AddError(p.Name, v.ErrorMessage);
}
});
}
Expand All @@ -126,7 +126,7 @@ protected virtual ServiceResult<T> Validate(T item)
public virtual ServiceResult<T> Add(T item)
{
var result = Validate(item);
if (result.HasViolation)
if (result.HasError)
{
return result;
}
Expand Down Expand Up @@ -154,7 +154,7 @@ public virtual ServiceResult<T> AddRange(params T[] items)
foreach (var item in items)
{
var itemResult = Validate(item);
if (itemResult.HasViolation)
if (itemResult.HasError)
{
return itemResult;
}
Expand Down Expand Up @@ -287,7 +287,7 @@ public virtual async Task<int> CountAsync(Expression<Func<T, bool>> filter)
public virtual ServiceResult<T> Update(T item)
{
var result = Validate(item);
if (result.HasViolation)
if (result.HasError)
{
return result;
}
Expand All @@ -310,7 +310,7 @@ public virtual ServiceResult<T> UpdateRange(params T[] items)
foreach (var item in items)
{
var itemResult = Validate(item);
if (itemResult.HasViolation)
if (itemResult.HasError)
{
return itemResult;
}
Expand Down
157 changes: 141 additions & 16 deletions src/EasyFrameWork/RepositoryPattern/ServiceResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

namespace Easy.RepositoryPattern
Expand All @@ -12,43 +13,148 @@ public class ServiceResult
{
public ServiceResult()
{
RuleViolations = new List<RuleViolation>();
RuleViolations = new List<Violation>();
}
public List<RuleViolation> RuleViolations
public List<Violation> RuleViolations
{
get;
private set;
}
public void AddRuleViolation(string message)
public void AddError(string message)
{
RuleViolations.Add(new RuleViolation(string.Empty, message));
RuleViolations.Add(new Error(string.Empty, message));
}
public void AddRuleViolation(string name, string message)
public void AddError(string name, string message)
{
RuleViolations.Add(new RuleViolation(name, message));
RuleViolations.Add(new Error(name, message));
}
public bool HasViolation
public void AddWarning(string message)
{
get { return RuleViolations != null && RuleViolations.Count > 0; }
RuleViolations.Add(new Warning(string.Empty, message));
}
public void AddWarning(string name, string message)
{
RuleViolations.Add(new Warning(name, message));
}
public void AddInfo(string message)
{
RuleViolations.Add(new Info(string.Empty, message));
}
public void AddInfo(string name, string message)
{
RuleViolations.Add(new Info(name, message));
}
public bool HasError
{
get { return RuleViolations.Any(m => m is Error); }
}

public IEnumerable<Error> Errors
{
get
{
foreach (var item in RuleViolations)
{
if (item is Error error)
{
yield return error;
}
}
}
}

public string ErrorMessage
{
get
{
string msg = string.Empty;
if (RuleViolations != null && RuleViolations.Count > 0)
var msg = new StringBuilder();

foreach (Violation item in Errors)
{
if (item is Error)
{
msg.AppendLine(item.Message);
}
}
return msg.ToString();
}
}

public IEnumerable<Warning> Warnings
{
get
{
foreach (var item in RuleViolations)
{
if (item is Warning warning)
{
yield return warning;
}
}
}
}
public string WarningMessage
{
get
{
var msg = new StringBuilder();
foreach (Violation item in RuleViolations)
{
if (item is Warning)
{
msg.AppendLine(item.Message);
}
}
return msg.ToString();
}
}
public IEnumerable<Info> Infos
{
get
{
foreach (var item in RuleViolations)
{
if (item is Info info)
{
yield return info;
}
}
}
}
public string InfoMessage
{
get
{
var msg = new StringBuilder();
foreach (Violation item in RuleViolations)
{
foreach (RuleViolation item in RuleViolations)
if (item is Info)
{
if (msg != string.Empty)
msg += "\r\n";
msg += item.ErrorMessage;
msg.AppendLine(item.Message);
}
}
return msg;
return msg.ToString();
}
}

public static implicit operator ServiceResult(Error error)
{
var result = new ServiceResult();
result.RuleViolations.Add(error);
return result;
}
public static implicit operator ServiceResult(Warning warning)
{
var result = new ServiceResult();
result.RuleViolations.Add(warning);
return result;
}
public static implicit operator ServiceResult(Info info)
{
var result = new ServiceResult();
result.RuleViolations.Add(info);
return result;
}
}
public class ServiceResult<T> : ServiceResult
{
Expand All @@ -57,10 +163,29 @@ public ServiceResult(T result)
{
Result = result;
}
public T Result { get; set; }

public static implicit operator ServiceResult<T>(T result)
{
return new ServiceResult<T>(result);
}
public T Result { get; set; }
public static implicit operator ServiceResult<T>(Error error)
{
var result = new ServiceResult<T>();
result.RuleViolations.Add(error);
return result;
}
public static implicit operator ServiceResult<T>(Warning warning)
{
var result = new ServiceResult<T>();
result.RuleViolations.Add(warning);
return result;
}
public static implicit operator ServiceResult<T>(Info info)
{
var result = new ServiceResult<T>();
result.RuleViolations.Add(info);
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@

namespace Easy.RepositoryPattern
{
public class RuleViolation
public abstract class Violation
{
public RuleViolation(string parameterName, string errorMessage)
public Violation(string parameterName, string message)
{
ParameterName = parameterName;
ErrorMessage = errorMessage;
Message = message;
}
public string ParameterName
{
get;
private set;
}
public string ErrorMessage
public string Message
{
get;
private set;
Expand Down
Loading

0 comments on commit 7777c70

Please sign in to comment.