简体   繁体   English

自定义业务对象比较器

[英]Custom business object comparer

I need to implement mechanism that compares two business objects and return the list of differences (past value, new value, isDifferenceBetter). 我需要实现比较两个业务对象并返回差异列表(过去值,新值,isDifferenceBetter)的机制。

Because not all fields of class has to be compared and one fields need to be compared with different function then the other (sometimes < is better sometimes > is better ... ) I figured out that I need to implelemnt custom attribute and give it to each field that has to be compared in this object. 因为不是所有类的字段都必须进行比较,并且一个字段需要与其他函数进行比较,所以另一个字段(有时<有时更好>有时更好),我发现我需要实现自定义属性并将其赋予此对象中必须比较的每个字段。

This attribute must have: - name - delegate or sth to point to the function which would be applied for comparision (dont know how to do it so far) 此属性必须具有:-名称-委托或sth指向将用于比较的函数(到目前为止尚不知道该怎么做)

So could anyone suggest me if its a good idea? 那么有人可以建议我,如果这是个好主意吗? Maybe any other ideas. 也许还有其他想法。

Using attributes I would be able to use refflection to iterate through each field with attribute and invoke needed delegate. 使用属性,我将能够使用反射来遍历具有属性的每个字段并调用所需的委托。

thanks for help bye 谢谢你再见

See my example below. 请参阅下面的示例。 May be, it can help you: 也许可以,它可以帮助您:


namespace ConsoleApplication5
{
    class FunctionToCompareAttribute : Attribute
    {
        public FunctionToCompareAttribute( String className, String methodName )
        {
            ClassName = className;
            MethodName = methodName;
        }

        public String ClassName
        {
            get;
            private set;
        }

        public String MethodName
        {
            get;
            private set;
        }
    }

    class ComparableAttribute : Attribute
    {
    }

    class CompareResult
    {

    }

    [Comparable]
    class ClassToCompare
    {
        [FunctionToCompare( "ConsoleApplication5.ClassToCompare", "MyCompareFunction" )]
        public String SomeProperty
        {
            get;
            private set;
        }

        public static CompareResult MyCompareFunction( Object left, Object right, String propertyName )
        {
            return null;//Comparsion
        }
    }

    class Program
    {
        static void Main( string[] args )
        {
            var left = new ClassToCompare();
            var right = new ClassToCompare();

            var type = typeof( ClassToCompare );

            var typeAttributes = type.GetCustomAttributes( typeof( ComparableAttribute ), true );

            if ( typeAttributes.Length == 0 )
                return;

            foreach ( var property in type.GetProperties() )
            {
                var attributes = property.GetCustomAttributes( typeof( FunctionToCompareAttribute ), true );

                if ( attributes.Length == 0 )
                    continue;

                var compareAttribute = attributes[ 0 ] as FunctionToCompareAttribute;

                var className = compareAttribute.ClassName;
                var methodName = compareAttribute.MethodName;

                var compareType = Type.GetType( className );

                var method = compareType.GetMethod( methodName, new Type[] { type, type, typeof( String ) } );

                var **result** = method.Invoke( null, new Object[] { left, right, property.Name } ) as CompareResult;
            }
        }
    }
}

搜索一些有关自我跟踪对象以及ORM(例如NHibernate)检查对象是否存在脏字段的方法

Certainly possible but perhaps you should be thinking along more abstract terms. 当然有可能,但也许您应该考虑更抽象的术语。 Maybe a pair of attributes [LowerValueIsBetter] and [HigherValueIsBetter] would enable you to express this information in a more cohesive way. 也许一对属性[LowerValueIsBetter]和[HigherValueIsBetter]可以使您以更紧密的方式表达此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM