Commit 40acf97e authored by 阿星Plus's avatar 阿星Plus

工作单元 over.

parent bba639d0
using System.Collections.Generic;
namespace Plus.Domain.Uow
{
/// <summary>
/// ConnectionStringResolveArgs
/// </summary>
public class ConnectionStringResolveArgs : Dictionary<string, object>
{
}
}
\ No newline at end of file
namespace Plus.Domain.Uow
{
/// <summary>
/// Used to get connection string when a database connection is needed.
/// </summary>
public interface IConnectionStringResolver
{
/// <summary>
/// Gets a connection string name (in config file) or a valid connection string.
/// </summary>
/// <param name="args">Arguments that can be used while resolving connection string.</param>
string GetNameOrConnectionString(ConnectionStringResolveArgs args);
}
}
\ No newline at end of file
namespace Plus.Domain.Uow
{
/// <summary>
/// Defines a unit of work.
/// This interface is internally used by ABP.
/// Use <see cref="IUnitOfWorkManager.Begin()"/> to start a new unit of work.
/// 工作单元接口
/// </summary>
public interface IUnitOfWork : IActiveUnitOfWork, IUnitOfWorkCompleteHandle
{
/// <summary>
/// Unique id of this UOW.
/// </summary>
string Id { get; }
/// <summary>
/// Reference to the outer UOW if exists.
/// </summary>
IUnitOfWork Outer { get; set; }
/// <summary>
/// Begins the unit of work with given options.
/// </summary>
/// <param name="options">Unit of work options</param>
void Begin(UnitOfWorkOptions options);
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Plus.Domain.Uow
{
/// <summary>
/// 工作单元的空实现
/// </summary>
public sealed class NullUnitOfWork : UnitOfWorkBase
{
public override void SaveChanges()
{
}
public override Task SaveChangesAsync()
{
return Task.FromResult(0);
}
protected override void BeginUow()
{
}
protected override void CompleteUow()
{
}
protected override Task CompleteUowAsync()
{
return Task.FromResult(0);
}
protected override void DisposeUow()
{
}
public NullUnitOfWork(IConnectionStringResolver connectionStringResolver, IUnitOfWorkDefaultOptions defaultOptions, IUnitOfWorkFilterExecuter filterExecuter)
: base(connectionStringResolver, defaultOptions, filterExecuter)
{
}
}
}
\ No newline at end of file
namespace Plus.Domain.Uow
{
/// <summary>
/// NullUnitOfWorkFilterExecuter
/// </summary>
public class NullUnitOfWorkFilterExecuter : IUnitOfWorkFilterExecuter
{
public void ApplyDisableFilter(IUnitOfWork unitOfWork, string filterName)
{
}
public void ApplyEnableFilter(IUnitOfWork unitOfWork, string filterName)
{
}
public void ApplyFilterParameterValue(IUnitOfWork unitOfWork, string filterName, string parameterName, object value)
{
}
}
}
\ No newline at end of file
namespace Plus.Domain.Uow
{
/// <summary>
/// Standard filters of ABP.
/// </summary>
public static class PlusDataFilters
{
/// <summary>
/// "SoftDelete".
/// Soft delete filter.
/// Prevents getting deleted data from database.
/// See <see cref="ISoftDelete"/> interface.
/// </summary>
public const string SoftDelete = "SoftDelete";
/// <summary>
/// "MustHaveTenant".
/// Tenant filter to prevent getting data that is
/// not belong to current tenant.
/// </summary>
public const string MustHaveTenant = "MustHaveTenant";
/// <summary>
/// "MayHaveTenant".
/// Tenant filter to prevent getting data that is
/// not belong to current tenant.
/// </summary>
public const string MayHaveTenant = "MayHaveTenant";
/// <summary>
/// Standard parameters of ABP.
/// </summary>
public static class Parameters
{
/// <summary>
/// "tenantId".
/// </summary>
public const string TenantId = "tenantId";
/// <summary>
/// "isDeleted".
/// </summary>
public const string IsDeleted = "isDeleted";
}
}
}
\ No newline at end of file
......@@ -22,52 +22,26 @@ namespace Plus.Domain.Uow
private Exception _exception;
public string Id
{
get;
}
private int? _tenantId;
public string Id { get; }
[DoNotWire]
public IUnitOfWork Outer
{
get;
set;
}
public IUnitOfWork Outer { get; set; }
public UnitOfWorkOptions Options
{
get;
private set;
}
public UnitOfWorkOptions Options { get; private set; }
public IReadOnlyList<DataFilterConfiguration> Filters => _filters.ToImmutableList();
public Dictionary<string, object> Items
{
get;
set;
}
public Dictionary<string, object> Items { get; set; }
protected IUnitOfWorkDefaultOptions DefaultOptions
{
get;
}
protected IUnitOfWorkDefaultOptions DefaultOptions { get; }
protected IConnectionStringResolver ConnectionStringResolver
{
get;
}
protected IConnectionStringResolver ConnectionStringResolver { get; }
public bool IsDisposed
{
get;
private set;
}
public bool IsDisposed { get; private set; }
protected IUnitOfWorkFilterExecuter FilterExecuter
{
get;
}
protected IUnitOfWorkFilterExecuter FilterExecuter{ get; }
public event EventHandler Completed;
......@@ -210,6 +184,7 @@ namespace Plus.Domain.Uow
protected virtual void BeginUow()
{
}
protected abstract void CompleteUow();
......@@ -257,7 +232,7 @@ namespace Plus.Domain.Uow
{
if (_isBeginCalledBefore)
{
throw new UPrimeException("This unit of work has started before. Can not call Start method more than once.");
throw new PlusException("This unit of work has started before. Can not call Start method more than once.");
}
_isBeginCalledBefore = true;
}
......@@ -266,32 +241,19 @@ namespace Plus.Domain.Uow
{
if (_isCompleteCalledBefore)
{
throw new UPrimeException("Complete is called before!");
throw new PlusException("Complete is called before!");
}
_isCompleteCalledBefore = true;
}
private void SetFilters(List<DataFilterConfiguration> filterOverrides)
{
int i;
for (i = 0; i < _filters.Count; i++)
for (var i = 0; i < _filters.Count; i++)
{
DataFilterConfiguration dataFilterConfiguration = filterOverrides.FirstOrDefault((DataFilterConfiguration f) => f.FilterName == _filters[i].FilterName);
if (dataFilterConfiguration != null)
var filterOverride = filterOverrides.FirstOrDefault(f => f.FilterName == _filters[i].FilterName);
if (filterOverride != null)
{
_filters[i] = dataFilterConfiguration;
}
}
}
private void ChangeFilterIsEnabledIfNotOverrided(List<DataFilterConfiguration> filterOverrides, string filterName, bool isEnabled)
{
if (!filterOverrides.Any((DataFilterConfiguration f) => f.FilterName == filterName))
{
int num = _filters.FindIndex((DataFilterConfiguration f) => f.FilterName == filterName);
if (num >= 0 && _filters[num].IsEnabled != isEnabled)
{
_filters[num] = new DataFilterConfiguration(filterName, isEnabled);
_filters[i] = filterOverride;
}
}
}
......@@ -301,7 +263,7 @@ namespace Plus.Domain.Uow
DataFilterConfiguration dataFilterConfiguration = _filters.FirstOrDefault((DataFilterConfiguration f) => f.FilterName == filterName);
if (dataFilterConfiguration == null)
{
throw new UPrimeException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
throw new PlusException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
}
return dataFilterConfiguration;
}
......@@ -311,7 +273,7 @@ namespace Plus.Domain.Uow
int num = _filters.FindIndex((DataFilterConfiguration f) => f.FilterName == filterName);
if (num < 0)
{
throw new UPrimeException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
throw new PlusException("Unknown filter name: " + filterName + ". Be sure this filter is registered before.");
}
return num;
}
......@@ -320,5 +282,44 @@ namespace Plus.Domain.Uow
{
return "[UnitOfWork " + Id + "]";
}
public virtual IDisposable SetTenantId(int? tenantId)
{
return SetTenantId(tenantId, true);
}
public virtual IDisposable SetTenantId(int? tenantId, bool switchMustHaveTenantEnableDisable)
{
var oldTenantId = _tenantId;
_tenantId = tenantId;
IDisposable mustHaveTenantEnableChange;
if (switchMustHaveTenantEnableDisable)
{
mustHaveTenantEnableChange = tenantId == null
? DisableFilter(PlusDataFilters.MustHaveTenant)
: EnableFilter(PlusDataFilters.MustHaveTenant);
}
else
{
mustHaveTenantEnableChange = NullDisposable.Instance;
}
var mayHaveTenantChange = SetFilterParameter(PlusDataFilters.MayHaveTenant, PlusDataFilters.Parameters.TenantId, tenantId);
var mustHaveTenantChange = SetFilterParameter(PlusDataFilters.MustHaveTenant, PlusDataFilters.Parameters.TenantId, tenantId ?? 0);
return new DisposeAction(() =>
{
mayHaveTenantChange.Dispose();
mustHaveTenantChange.Dispose();
mustHaveTenantEnableChange.Dispose();
_tenantId = oldTenantId;
});
}
public int? GetTenantId()
{
return _tenantId;
}
}
}
\ No newline at end of file
......@@ -4,6 +4,7 @@ using Plus.Dependency;
using Plus.Domain.Uow;
using Plus.Event.Bus;
using Plus.Modules;
using Plus.Runtime.Validation.Interception;
using System;
using System.IO;
using System.Linq.Expressions;
......
using Plus.Dependency;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace Plus.Runtime.Validation.Interception
{
/// <summary>
/// DataAnnotationsValidator
/// </summary>
public class DataAnnotationsValidator : IMethodParameterValidator, ITransientDependency
{
public virtual IReadOnlyList<ValidationResult> Validate(object validatingObject)
{
return GetDataAnnotationAttributeErrors(validatingObject);
}
protected virtual List<ValidationResult> GetDataAnnotationAttributeErrors(object validatingObject)
{
var validationErrors = new List<ValidationResult>();
var properties = TypeDescriptor.GetProperties(validatingObject).Cast<PropertyDescriptor>();
foreach (var property in properties)
{
var validationAttributes = property.Attributes.OfType<ValidationAttribute>().ToArray();
if (validationAttributes.IsNullOrEmpty())
{
continue;
}
var validationContext = new ValidationContext(validatingObject)
{
DisplayName = property.DisplayName,
MemberName = property.Name
};
foreach (var attribute in validationAttributes)
{
var result = attribute.GetValidationResult(property.GetValue(validatingObject), validationContext);
if (result != null)
{
validationErrors.Add(result);
}
}
}
return validationErrors;
}
}
}
\ No newline at end of file
using Plus.Dependency;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Plus.Runtime.Validation.Interception
{
/// <summary>
/// ValidatableObjectValidator
/// </summary>
public class ValidatableObjectValidator : IMethodParameterValidator, ITransientDependency
{
public virtual IReadOnlyList<ValidationResult> Validate(object validatingObject)
{
List<ValidationResult> list = new List<ValidationResult>();
IValidatableObject validatableObject;
if ((validatableObject = (validatingObject as IValidatableObject)) != null)
{
list.AddRange(validatableObject.Validate(new ValidationContext(validatableObject)));
}
return list;
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment