如何从单个方法填充多种类型的业务对象?

发布于 2024-09-11 04:30:12 字数 218 浏览 2 评论 0原文

我有四个不同的 Business 对象,每个对象都调用其相应的 FillBusinessObject 方法来一一填充所有单独的对象属性。现在我希望创建一个通用方法,它应该能够填充每种类型的业务对象。我创建了一个基类,所有业务对象都从该基类继承,但我无法弄清楚如何从通用方法填充各个对象属性。

这可能吗(如果是,那么如何)或者我在梦境世界中?

PS 我不想采取像 LINQ 这样的不同路线。

I have four different Business objects and each calls its corresponding FillBusinessObject method to fill all the individual object properties one by one. Now I wish to create a common method which should be able to fill each type of business object. I've created a base class from which all business objects inherit but I am not able to figure out how to fill individual object properties from a common method.

Is this possible (if yes, then how) or am I in dreamworld?

P.S. I don't wish to take a different route like LINQ.

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

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

发布评论

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

评论(2

太阳公公是暖光 2024-09-18 04:30:12

看来你把事情过于复杂化了。

您可以编写一个方法来填充属于公共基类的部分,然后为每种类型调用专用方法。

Looks like you're over-complicating things.

You could write a method that fills the part that belongs to a common base-class and then calls a specialize method for each type.

夜血缘 2024-09-18 04:30:12

像这样的事情怎么样:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[] args )
        {
            // Create some business objects and ask them to initialize
            // themselves.
            //

            var bo1 = new BusinessObject1();
            var bo2 = new BusinessObject2();

            bo1.Fill();
            bo2.Fill();
        }

        public abstract class BusinessObjectBase
        {
            public int x { get; private set; }

            public virtual void Fill()
            {
                x = 123;
            }
        }

        public class BusinessObject1 : BusinessObjectBase
        {
            public int y { get; private set; }

            public override void Fill()
            {
                // Let base class fill itself.
                base.Fill();

                // Now we fill our own properties.
                this.y = 456;
            }
        }

        public class BusinessObject2 : BusinessObjectBase
        {
            public int z { get; private set; }

            public override void Fill()
            {
                // Let base class fill itself.
                base.Fill();

                // Now we fill our own properties.
                this.z = 456;
            }
        }
    }
}

How about something like this:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[] args )
        {
            // Create some business objects and ask them to initialize
            // themselves.
            //

            var bo1 = new BusinessObject1();
            var bo2 = new BusinessObject2();

            bo1.Fill();
            bo2.Fill();
        }

        public abstract class BusinessObjectBase
        {
            public int x { get; private set; }

            public virtual void Fill()
            {
                x = 123;
            }
        }

        public class BusinessObject1 : BusinessObjectBase
        {
            public int y { get; private set; }

            public override void Fill()
            {
                // Let base class fill itself.
                base.Fill();

                // Now we fill our own properties.
                this.y = 456;
            }
        }

        public class BusinessObject2 : BusinessObjectBase
        {
            public int z { get; private set; }

            public override void Fill()
            {
                // Let base class fill itself.
                base.Fill();

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