Skip to content

Commit

Permalink
Migrate Euonia.Business
Browse files Browse the repository at this point in the history
  • Loading branch information
Codespilot committed Jul 8, 2023
1 parent f92b731 commit 1491a07
Show file tree
Hide file tree
Showing 61 changed files with 4,561 additions and 48 deletions.
105 changes: 57 additions & 48 deletions Euonia.sln

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Source/Euonia.Business/Abstracts/IBusinessObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.ComponentModel;

namespace Nerosoft.Euonia.Business;

public interface IBusinessObject : IUseBusinessContext, INotifyPropertyChanged, INotifyPropertyChanging
{
FieldDataManager FieldManager { get; }

bool FieldExists(IPropertyInfo property);

object ReadProperty(IPropertyInfo propertyInfo);

TValue ReadProperty<TValue>(PropertyInfo<TValue> propertyInfo);

void LoadProperty(IPropertyInfo propertyInfo, object newValue);

void LoadProperty<TValue>(PropertyInfo<TValue> propertyInfo, TValue newValue);
}
5 changes: 5 additions & 0 deletions Source/Euonia.Business/Abstracts/ICommandObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Nerosoft.Euonia.Business;

public interface ICommandObject : IBusinessObject
{
}
26 changes: 26 additions & 0 deletions Source/Euonia.Business/Abstracts/IEditableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Nerosoft.Euonia.Business;

public interface IEditableObject
{
/// <summary>
/// Gets the current object state.
/// </summary>
ObjectEditState State { get; }

/// <summary>
/// Gets a value indicate if the object can save.
/// </summary>
bool IsSavable { get; }

bool IsInsert { get; }

bool IsUpdate { get; }

bool IsDelete { get; }

void MarkAsInsert();

void MarkAsUpdate();

void MarkAsDelete();
}
37 changes: 37 additions & 0 deletions Source/Euonia.Business/Abstracts/IHasRuleCheck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
namespace Nerosoft.Euonia.Business;

public interface IHasRuleCheck
{
/// <summary>
/// Gets a value indicate that whether the object is currently valid or not.
/// </summary>
/// <returns><c>True</c> if the object is currently valid, otherwise <c>False</c>.</returns>
bool IsValid { get; }

/// <summary>
/// Indicates that a rule has completed.
/// </summary>
/// <param name="property"></param>
void RuleCheckComplete(IPropertyInfo property);

/// <summary>
/// Indicates that a rule has completed.
/// </summary>
/// <param name="property"></param>
void RuleCheckComplete(string property);

/// <summary>
/// Indicates that all rules have completed.
/// </summary>
void AllRulesComplete();

void SuspendRuleChecking();

void ResumeRuleChecking();

/// <summary>
/// Gets the broken rules for this object
/// </summary>
/// <returns></returns>
BrokenRuleCollection GetBrokenRules();
}
12 changes: 12 additions & 0 deletions Source/Euonia.Business/Abstracts/IOperableProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Nerosoft.Euonia.Business;

public interface IOperableProperty
{
object GetProperty(IPropertyInfo propertyInfo);

void SetProperty(IPropertyInfo propertyInfo, object newValue);
}
6 changes: 6 additions & 0 deletions Source/Euonia.Business/Abstracts/IReadOnlyObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Nerosoft.Euonia.Business;

public interface IReadOnlyObject : IBusinessObject
{

}
45 changes: 45 additions & 0 deletions Source/Euonia.Business/Abstracts/ISavable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Nerosoft.Euonia.Business;

public interface ISavable
{
/// <summary>
/// Event raised when an object has been saved.
/// </summary>
event EventHandler<SavedEventArgs> Saved;

/// <summary>
///
/// </summary>
/// <param name="newObject"></param>
void SaveComplete(object newObject);

/// <summary>
/// Saves the object to the database.
/// </summary>
/// <param name="forceUpdate">true to force the save to be an update.</param>
/// <param name="cancellationToken"></param>
/// <returns>A new object containing the saved values.</returns>
Task<object> SaveAsync(bool forceUpdate = false, CancellationToken cancellationToken = default);
}

public interface ISavable<T> where T : class
{
/// <summary>
/// Saves the object to the database.
/// </summary>
/// <param name="forceUpdate">true to force the save to be an update.</param>
/// <param name="cancellationToken"></param>
/// <returns>A new object containing the saved values.</returns>
Task<T> SaveAsync(bool forceUpdate = false, CancellationToken cancellationToken = default);

/// <summary>
///
/// </summary>
/// <param name="newObject"></param>
void SaveComplete(T newObject);

/// <summary>
/// Event raised when an object has been saved.
/// </summary>
event EventHandler<SavedEventArgs> Saved;
}
14 changes: 14 additions & 0 deletions Source/Euonia.Business/Abstracts/ITrackableObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Nerosoft.Euonia.Business;

public interface ITrackableObject
{
bool IsValid { get; }

bool IsChanged { get; }

bool IsDeleted { get; }

bool IsNew { get; }

bool IsSavable { get; }
}
8 changes: 8 additions & 0 deletions Source/Euonia.Business/Abstracts/IUseBusinessContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Nerosoft.Euonia.Business;

public interface IUseBusinessContext
{
BusinessContext BusinessContext { get; set; }

IServiceProvider GetServiceProvider();
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryCreateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would create new domain object data.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FactoryCreateAttribute : FactoryMethodAttribute
{
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryDeleteAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would delete domain object data.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FactoryDeleteAttribute : FactoryMethodAttribute
{
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryExecuteAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would execute a defined command.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public class FactoryExecuteAttribute : FactoryMethodAttribute
{
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryFetchAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would find exists domain object data.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FactoryFetchAttribute : FactoryMethodAttribute
{
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryInsertAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would insert a new row with domain object data.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FactoryInsertAttribute : FactoryMethodAttribute
{
}
9 changes: 9 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryMethodAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public abstract class FactoryMethodAttribute : Attribute
{
}
10 changes: 10 additions & 0 deletions Source/Euonia.Business/Attributes/FactoryUpdateAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace Nerosoft.Euonia.Business;

/// <summary>
/// Represent the marked method would update domain object data.
/// And the method could be invoked by <see cref="IObjectFactory"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class FactoryUpdateAttribute : FactoryMethodAttribute
{
}
117 changes: 117 additions & 0 deletions Source/Euonia.Business/Core/BusinessContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using System.Security.Claims;
using System.Security.Principal;
using Microsoft.Extensions.DependencyInjection;

namespace Nerosoft.Euonia.Business;

/// <summary>
///
/// </summary>
public class BusinessContext
{
/// <summary>
/// Initializes a new instance of the <see cref="BusinessContext"/> class.
/// </summary>
/// <param name="contextAccessor"></param>
public BusinessContext(BusinessContextAccessor contextAccessor)
{
ContextAccessor = contextAccessor;
}

internal BusinessContextAccessor ContextAccessor { get; }

/// <summary>
/// Get or set the current ClaimsPrincipal object representing the user's identity.
/// </summary>
public ClaimsPrincipal Principal => (ClaimsPrincipal)User;

/// <summary>
/// Get or set the current <see cref="IPrincipal" /> object representing the user's identity.
/// </summary>
public IPrincipal User { get; set; }

internal IServiceProvider CurrentServiceProvider => ContextAccessor.ServiceProvider;

/// <summary>
/// Get the service object of the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="NullReferenceException"></exception>
public T GetRequiredService<T>()
{
if (CurrentServiceProvider == null)
{
throw new NullReferenceException(nameof(CurrentServiceProvider));
}

var result = CurrentServiceProvider.GetRequiredService<T>();
return result;
}

/// <summary>
/// Get the service object of the specified type.
/// </summary>
/// <param name="serviceType"></param>
/// <returns></returns>
/// <exception cref="NullReferenceException"></exception>
public object GetRequiredService(Type serviceType)
{
if (CurrentServiceProvider == null)
{
throw new NullReferenceException(nameof(CurrentServiceProvider));
}

return CurrentServiceProvider.GetRequiredService(serviceType);
}

/// <summary>
/// Create an instance of the specified type using the constructor that best matches the specified parameters.
/// </summary>
/// <param name="parameters"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T CreateInstance<T>(params object[] parameters)
{
return (T)CreateInstance(typeof(T), parameters);
}

/// <summary>
/// Create an instance of the specified type using the constructor that best matches the specified parameters.
/// </summary>
/// <param name="objectType"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public object CreateInstance(Type objectType, params object[] parameters)
{
object result;
if (CurrentServiceProvider != null)
{
result = ActivatorUtilities.CreateInstance(CurrentServiceProvider, objectType, parameters);
}
else
{
result = Activator.CreateInstance(objectType, parameters);
}

if (result is IUseBusinessContext tmp)
{
tmp.BusinessContext = this;
}

return result;
}

/// <summary>
/// Create an instance of the specified type using the constructor that best matches the specified parameters.
/// </summary>
/// <param name="type"></param>
/// <param name="paramTypes"></param>
/// <returns></returns>
internal object CreateGenericInstance(Type type, params Type[] paramTypes)
{
var genericType = type.GetGenericTypeDefinition();
var gt = genericType.MakeGenericType(paramTypes);
return CreateInstance(gt);
}
}
Loading

0 comments on commit 1491a07

Please sign in to comment.