从基础对象转换为派生对象,同时拥有庞大的构造函数定义 [英] Converting from base to derived object while having a huge constructor definition

查看:38
本文介绍了从基础对象转换为派生对象,同时拥有庞大的构造函数定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类 BaseClass

class BaseClass
{
    public int PropertyOne { get; }

    public string PropertyTwo { get; }

    public BaseClass(int propertyOne, string propertyTwo)
    {
        PropertyOne = propertyOne;
        PropertyTwo = propertyTwo;
    }
}

还有一个子类DerivedClass

class DerivedClass : BaseClass
{
    public DerivedClass(int propertyOne, string propertyTwo) : base(propertyOne, propertyTwo)
    {
    }

    public void DerivedMethod()
    {

    }

    public static DerivedClass ConvertFromBaseClass(BaseClass baseClass)
    {
        return new DerivedClass(baseClass.PropertyOne, baseClass.PropertyTwo); // <-- ?
    }
}

是否可以在不显式调用构造函数的情况下实现 DerivedClass.ConvertFromBaseClass,也许使用反射?实际上,这些类有 50 多个参数传递给构造函数,因此在转换器方法中编写构造函数调用非常繁琐且容易出错.

Is it possible to implement DerivedClass.ConvertFromBaseClass without explicitly calling the constructor, perhaps using reflection? In a reality those classes have 50+ arguments passed into constructor and therefore it's extremely tideous and error-prone to write the constructor call in the converter method.

推荐答案

根据情况,您可以重构代码以改用 Mixins.

Depending on the situation, you may be able to restructure your code to use Mixins instead.

首先从基类中提取一个接口.Resharper 只需按一下按钮即可完成此操作:

First you extract an interface from base class. Resharper can do this at the push of a button:

class BaseClass : Interface
{
    public int PropertyOne { get; }

    public string PropertyTwo { get; }

    public BaseClass(int propertyOne, string propertyTwo)
    {
        PropertyOne = propertyOne;
        PropertyTwo = propertyTwo;
    }
}

interface Interface
{
    int PropertyOne { get; }

    string PropertyTwo { get; }
 }

然后不是从基类继承,而是在派生类中添加一个字段,您使用该字段实现接口.Resharper 也可以在实例中执行此操作:

Then instead of inheriting from the base class, you add a field in derived class which you implement the interface using. Resharper can also do this in an instance:

class DerivedClass : Interface
{
    private readonly Interface _interface;

    public DerivedClass(Interface interface) => _interface = Interface;

    public int PropertyOne => _interface.PropertyOne;

    public string PropertyTwo => _interface.PropertyTwo;
}

然后您只需将基类的实例传入构造函数即可.

Then you simply pass in an instance of base class into the constructor.

这样做有两个好处.首先,这意味着您不必重新实现构造函数和转换器中的逻辑来处理所有 50 个参数.Resharper 实际上可以为您完成所有逻辑.

There's two advantages to this. Firstly it means you don't have to reimplement the logic in the constructor and the converter to deal with all 50 parameters. Resharper can in fact do all the logic for you.

其次,这意味着您不必在每次从基类创建派生类型时复制 50 个参数(32 位运行时大约 200 个字节,64 位运行时大约 400 个字节).

Secondly, it means that you don't have to copy 50 parameters (approx 200 bytes in 32 bit runtime, 400 bytes in 64 bit) each time you create a derived type from the base class.

这可能是也可能不是合适的模式,这取决于您的情况

This may or may not be a suitable pattern, depending on your situation

这篇关于从基础对象转换为派生对象,同时拥有庞大的构造函数定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆