自定义业务对象比较器

发布于 2024-10-09 22:45:01 字数 324 浏览 0 评论 0原文

我需要实现比较两个业务对象并返回差异列表(过去值、新值、isDifferenceBetter)的机制。

因为并非类的所有字段都必须进行比较,并且一个字段需要与不同的函数进行比较,然后另一个字段(有时 < 更好,有时 > 更好...)我发现我需要实现自定义属性并给出它与该对象中必须进行比较的每个字段。

该属性必须具有: - 姓名 - 委托或某物指向将应用于比较的函数(到目前为止不知道如何做)

那么有人可以建议我如果这是一个好主意吗?也许还有其他想法。

使用属性,我将能够使用反射来迭代具有属性的每个字段并调用所需的委托。

谢谢你的帮助 再见

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-10-16 22:45:01

请参阅下面我的示例。
也许,它可以帮助您:


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;
            }
        }
    }
}

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;
            }
        }
    }
}

酒几许 2024-10-16 22:45:01

搜索一下自跟踪对象以及 ORM(如 NHibernate)检查对象是否有脏字段的方式

Do some search about self tracking objects and the way ORMs(like NHibernate) checking an object for dirty fields

贪恋 2024-10-16 22:45:01

当然可能,但也许您应该考虑更抽象的术语。也许一对属性 [LowerValueIsBetter] 和 [HigherValueIsBetter] 将使您能够以更有凝聚力的方式表达此信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文