简体   繁体   中英

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).

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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