Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何将SignaturePropertyCriteria附加到ValueObject_.net_Validation_S#arp Architecture - Fatal编程技术网

.net 如何将SignaturePropertyCriteria附加到ValueObject

.net 如何将SignaturePropertyCriteria附加到ValueObject,.net,validation,s#arp-architecture,.net,Validation,S#arp Architecture,我使用Sharp Architecture 1.6并遇到EntityDuplicateChecker的一个问题 我有一个实体,它有两个属性作为域签名,(int)CustomerId和一个表示一周(包括一年和一周数)的ValueObject 因此,在DB术语中,在CustomerId、Year和WeekNumber三列上有一个域签名 EntityDuplicateChecker仅支持对实体类型、值类型、字符串、日期和枚举的引用 是否有处理此场景的良好实践 欢迎提出任何想法。您能将DomainSig

我使用Sharp Architecture 1.6并遇到EntityDuplicateChecker的一个问题

我有一个实体,它有两个属性作为域签名,(int)CustomerId和一个表示一周(包括一年和一周数)的ValueObject

因此,在DB术语中,在CustomerId、Year和WeekNumber三列上有一个域签名

EntityDuplicateChecker仅支持对实体类型、值类型、字符串、日期和枚举的引用

是否有处理此场景的良好实践


欢迎提出任何想法。

您能将DomainSignature属性放在一个日期字段中,该字段存储该周的第一个星期一(例如,当Year=2010,WeekNumber=40时,则日期为2010-10-04)?然后,您需要Year和WeekNumber属性设置器中的逻辑来根据需要更改日期字段,以及date属性设置器中的逻辑来更改Year和WeekNumber字段


我最终做了自己的DuplicateEntityChecker,并将ComponentRegister改为使用它,而不是SharpArch one

我的班级看起来像这样

using NHibernate;
using SharpArch.Core.DomainModel;
using System.Reflection;
using System;
using NHibernate.Criterion;
using SharpArch.Core;
using SharpArch.Core.PersistenceSupport;
using System.Linq;
using SharpArch.Data.NHibernate;

namespace your.namespace.Data.NHibernate
{
public class EntityDuplicateChecker : IEntityDuplicateChecker
{
    /// <summary>
    /// Provides a behavior specific repository for checking if a duplicate exists of an existing entity.
    /// </summary>
    public bool DoesDuplicateExistWithTypedIdOf<IdT>(IEntityWithTypedId<IdT> entity) {
        Check.Require(entity != null, "Entity may not be null when checking for duplicates");

        ISession session = GetSessionFor(entity);

        FlushMode previousFlushMode = session.FlushMode;

        // We do NOT want this to flush pending changes as checking for a duplicate should 
        // only compare the object against data that's already in the database
        session.FlushMode = FlushMode.Never;

        Criteria = session.CreateCriteria(entity.GetType())
            .Add(Expression.Not(Expression.Eq("Id", entity.Id)))
            .SetMaxResults(1);

        AppendSignaturePropertyCriteriaTo<IdT>(Criteria, entity);
        bool doesDuplicateExist = Criteria.List().Count > 0;
        session.FlushMode = previousFlushMode;
        return doesDuplicateExist;
    }

    public ICriteria Criteria { get; protected set; }

    private void AppendSignaturePropertyCriteriaTo<IdT>(ICriteria criteria, IEntityWithTypedId<IdT> entity) {
        foreach (PropertyInfo signatureProperty in entity.GetSignatureProperties()) {
            Type propertyType = signatureProperty.PropertyType;
            object propertyValue = signatureProperty.GetValue(entity, null);

            if (propertyType.IsEnum) {
                criteria.Add(
                    Expression.Eq(ResolvePropertyName(string.Empty, signatureProperty), (int)propertyValue));
            }
            else if (propertyType.GetInterfaces()
                .Any(x => x.IsGenericType && 
                     x.GetGenericTypeDefinition() == typeof(IEntityWithTypedId<>))) {
                AppendEntityCriteriaTo<IdT>(criteria, signatureProperty, propertyValue);
            }
            else if (propertyType == typeof(DateTime)) {
                AppendDateTimePropertyCriteriaTo(criteria, string.Empty, signatureProperty, propertyValue);
            }
            else if (propertyType == typeof(String)) {
                AppendStringPropertyCriteriaTo(criteria, string.Empty, signatureProperty, propertyValue);
            }
            else if (propertyType.IsValueType) {
                AppendValuePropertyCriteriaTo(criteria, string.Empty, signatureProperty, propertyValue);
            }
            else if(typeof(ValueObject).IsAssignableFrom(propertyType)) {
                AppendValueObjectSignaturePropertyCriteriaTo(criteria, signatureProperty.Name + ".", propertyValue as ValueObject);
            }
            else {
                throw new ApplicationException("Can't determine how to use " + entity.GetType() + "." +
                    signatureProperty.Name + " when looking for duplicate entries. To remedy this, " +
                    "you can create a custom validator or report an issue to the S#arp Architecture " +
                    "project, detailing the type that you'd like to be accommodated.");
            }
        }
    }


    private void AppendValueObjectSignaturePropertyCriteriaTo(ICriteria criteria, string parentPropertyName, ValueObject valueObject)
    {
        if(valueObject == null)
            return;

        foreach (PropertyInfo signatureProperty in valueObject.GetSignatureProperties())
        {
            Type propertyType = signatureProperty.PropertyType;
            object propertyValue = signatureProperty.GetValue(valueObject, null);

            if (propertyType.IsEnum)
            {
                criteria.Add(
                    Expression.Eq(ResolvePropertyName(parentPropertyName, signatureProperty), (int)propertyValue));
            }
            else if (propertyType == typeof(DateTime))
            {
                AppendDateTimePropertyCriteriaTo(criteria, parentPropertyName, signatureProperty, propertyValue);
            }
            else if (propertyType == typeof(String))
            {
                AppendStringPropertyCriteriaTo(criteria, parentPropertyName, signatureProperty, propertyValue);
            }
            else if (propertyType.IsValueType)
            {
                AppendValuePropertyCriteriaTo(criteria, parentPropertyName, signatureProperty, propertyValue);
            }
            else if (typeof(ValueObject).IsAssignableFrom(propertyType))
            {
                AppendValueObjectSignaturePropertyCriteriaTo(criteria, ResolvePropertyName(parentPropertyName, signatureProperty), propertyValue as ValueObject);
            }
            else
            {
                throw new ApplicationException("Can't determine how to use " + valueObject.GetType() + "." +
                    signatureProperty.Name + " when looking for duplicate entries. To remedy this, " +
                    "you can create a custom validator or report an issue to the S#arp Architecture " +
                    "project, detailing the type that you'd like to be accommodated.");
            }
        }
    }

    private void AppendStringPropertyCriteriaTo(ICriteria criteria, string parentPropertyName,
        PropertyInfo signatureProperty, object propertyValue)
    {
        var propertyName = ResolvePropertyName(parentPropertyName, signatureProperty);

        if (propertyValue != null)
        {
            criteria.Add(
                Expression.InsensitiveLike(propertyName, propertyValue.ToString(), MatchMode.Exact));
        }
        else
        {
            criteria.Add(Expression.IsNull(propertyName));
        }
    }

    private void AppendDateTimePropertyCriteriaTo(ICriteria criteria, string parentPropertyName,
        PropertyInfo signatureProperty, object propertyValue)
    {

        var propertyName = ResolvePropertyName(parentPropertyName, signatureProperty);

        if ((DateTime) propertyValue > UNINITIALIZED_DATETIME)
        {
            criteria.Add(Expression.Eq(propertyName, propertyValue));
        }
        else
        {
            criteria.Add(Expression.IsNull(propertyName));
        }
    }

    private void AppendValuePropertyCriteriaTo(ICriteria criteria, string parentPropertyName,
        PropertyInfo signatureProperty, object propertyValue) {

        var propertyName = ResolvePropertyName(parentPropertyName, signatureProperty);

        if (propertyValue != null) {
            criteria.Add(Expression.Eq(propertyName, propertyValue));
        }
        else {
            criteria.Add(Expression.IsNull(propertyName));
        }
    }

    private static void AppendEntityCriteriaTo<IdT>(ICriteria criteria, 
        PropertyInfo signatureProperty, object propertyValue) {
        if (propertyValue != null) {
            criteria.Add(Expression.Eq(signatureProperty.Name + ".Id",
                ((IEntityWithTypedId<IdT>)propertyValue).Id));
        }
        else {
            criteria.Add(Expression.IsNull(signatureProperty.Name + ".Id"));
        }
    }

    private static string ResolvePropertyName(string parentPropertyName, PropertyInfo signatureProperty)
    {
        if (string.IsNullOrEmpty(parentPropertyName))
            return signatureProperty.Name;

        if (parentPropertyName.EndsWith(".") == false)
            parentPropertyName += ".";

        return string.Format("{0}{1}", parentPropertyName, signatureProperty.Name);
    }

    private static ISession GetSessionFor(object entity)
    {
        var factoryKey = SessionFactoryAttribute.GetKeyFrom(entity);
        return NHibernateSession.CurrentFor(factoryKey);
    }

    private readonly DateTime UNINITIALIZED_DATETIME = default(DateTime);
}
}
使用NHibernate;
使用SharpArch.Core.DomainModel;
运用系统反思;
使用制度;
使用NHibernate.标准;
使用SharpArch.Core;
使用SharpArch.Core.PersistenceSupport;
使用System.Linq;
使用SharpArch.Data.NHibernate;
名称空间your.namespace.Data.NHibernate
{
公共类EntityDuplicateChecker:IEntityDuplicateChecker
{
/// 
///提供特定于行为的存储库,用于检查现有实体是否存在重复项。
/// 
public bool DoesDuplicateExistWithTypedIdOf(EntityWithTypeDid实体){
Check.Require(实体!=null,“检查重复项时实体不能为null”);
ISession session=GetSessionFor(实体);
FlushMode previousFlushMode=session.FlushMode;
//我们不希望它刷新挂起的更改,因为应该检查重复项
//仅将对象与数据库中已有的数据进行比较
session.FlushMode=FlushMode.Never;
条件=会话.CreateCriteria(entity.GetType())
.Add(Expression.Not(Expression.Eq(“Id”,entity.Id)))
.SetMaxResults(1);
附录财产标准(标准、实体);
bool doesDuplicateExist=Criteria.List().Count>0;
session.FlushMode=上一个FlushMode;
返回不存在重复项;
}
公共ICriteria条件{get;protected set;}
私有无效附件属性标准(ICriteria标准,IEntityWithTypedId实体){
foreach(entity.GetSignatureProperties()中的PropertyInfo signatureProperty){
类型propertyType=signatureProperty.propertyType;
object propertyValue=signatureProperty.GetValue(实体,null);
if(propertyType.IsEnum){
标准。添加(
Eq(ResolvePropertyName(string.Empty,signatureProperty),(int)propertyValue));
}
else if(propertyType.GetInterfaces()
.Any(x=>x.IsGenericType&&
x、 GetGenericTypeDefinition(){
附录标准(标准、签名属性、属性值);
}
else if(propertyType==typeof(DateTime)){
AppendDateTimePropertyCriteriaTo(条件,string.Empty,signatureProperty,propertyValue);
}
else if(propertyType==typeof(String)){
AppendStringPropertyCriteriaTo(条件,string.Empty,signatureProperty,propertyValue);
}
else if(propertyType.IsValueType){
AppendValuePropertyCriteriaTo(条件,string.Empty,signatureProperty,propertyValue);
}
else if(typeof(ValueObject).IsAssignableFrom(propertyType)){
AppendValueObjectSignaturePropertyCriteriaTo(标准,signatureProperty.Name+”,propertyValue作为ValueObject);
}
否则{
抛出新的ApplicationException(“无法确定如何使用”+entity.GetType()+”+
signatureProperty.Name+“查找重复条目时。若要解决此问题,”+
“您可以创建自定义验证程序或向S#arp体系结构报告问题”+
“项目,详细说明您希望容纳的类型。”);
}
}
}
私有无效AppendValueObjectSignaturePropertyCriteriaTo(ICriteria条件,字符串parentPropertyName,ValueObject ValueObject)
{
if(valueObject==null)
返回;
foreach(valueObject.GetSignatureProperties()中的PropertyInfo signatureProperty)
{
类型propertyType=signatureProperty.propertyType;
object propertyValue=signatureProperty.GetValue(valueObject,null);
if(propertyType.IsEnum)
{
标准。添加(
Eq(ResolvePropertyName(parentPropertyName,signatureProperty),(int)propertyValue));
}
else if(propertyType==typeof(DateTime))
{
AppendDateTimePropertyCriteriaTo(标准、parentPropertyName、signatureProperty、propertyValue);
}
else if(propertyType==typeof(String))
{
附录StringPropertyCriteriato(标准、parentPropertyName、signatureProperty、propertyValue);
}
else if(propertyType.IsValueType)
{
AppendValuePropertyCriteriaTo(标准、parentPropertyName、signatureProperty、propertyValue);
}
else if(typeof(ValueObject).IsAssignableFrom(propertyType))
{
AppendValueObjectSignaturePropertyCriteriaTo(标准,ResolvePropertyName(parentPropertyName,signatureProperty),propertyValue作为ValueObject);
}
E