使用 .NET Activator.CreateInstance 而不使用 DTO
我有一个包含多个自定义表单元素的表单类。
我有一个 Entity 对象,它为元素提供特定属性,这些属性是从 XML 文件中解析的。所有元素都将实体作为其构造函数中的参数,但之后在其构造函数中需要不同的参数。
对于元素创建,我当前使用如下 switch 语句。但是,我想将其转换为使用 Activator.CreateInstance。但是,对于构造函数中的不同参数,我知道处理此问题的唯一方法是创建一个包含所有参数的 DTO,将其传递到构造函数中,然后让每个构造函数请求它需要的任何信息。我想要一个替代方案,因为据我所知,DTO 在现代实现中是不受欢迎的。
switch (entity.GetPropertyValue("Class"))
{
case "FormCheckBox":
newElement = new FormCheckBox(entity, BaseElementHeight);
break;
case "RowSeparator":
newElement = new RowSeperator(entity, RowHeight, _mainCanvas);
break;
case "FormLabel":
newElement = new FormLabel(entity, BaseElementHeight);
break;
case "FormEditBox":
newElement = new FormEditBox(entity, _mainCanvas);//, BaseElementHeight, 600);
break;
case "FormComboBox":
newElement = new FormComboBox(entity, BaseElementHeight);
break;
case "FormTextBox":
newElement = new FormTextBox(entity, BaseElementHeight, TextFontSize, MaxFontBoxSize);
break;
default:
return null;
}
有人对此有想法和/或想法吗?
I have a Form class that contains several custom Form Elements.
I have an Entity object, that provides specific attributes for the element, these are parsed from on XML file. All elements take the Entity as a parameter in their constructor, but then require different parameters in their constructor after that.
For the element creation I am current using a switch statement as below. However, I want to convert this to use Activator.CreateInstance. However, with different parameters in the constructor the only way I know of to handle this is to create a DTO that contains all parameters, pass it in the constructor and then have each constructor request whatever information it requires. I want an alternative as it is my understanding that DTO is frowned upon in modern implmentations.
switch (entity.GetPropertyValue("Class"))
{
case "FormCheckBox":
newElement = new FormCheckBox(entity, BaseElementHeight);
break;
case "RowSeparator":
newElement = new RowSeperator(entity, RowHeight, _mainCanvas);
break;
case "FormLabel":
newElement = new FormLabel(entity, BaseElementHeight);
break;
case "FormEditBox":
newElement = new FormEditBox(entity, _mainCanvas);//, BaseElementHeight, 600);
break;
case "FormComboBox":
newElement = new FormComboBox(entity, BaseElementHeight);
break;
case "FormTextBox":
newElement = new FormTextBox(entity, BaseElementHeight, TextFontSize, MaxFontBoxSize);
break;
default:
return null;
}
Does anyone have thoughts and/or ideas on this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与其将所有内容都注入到实体的构造函数中,为什么不使用命令模式或
双调度模式
来管理这个。
不要将实体作为构造函数参数传递,而是将其设为属性,或者仅传递所需的属性。
这使实体完全脱离 UI。
rather than inject every thing into the entity's ctor, why not use the command pattern or
double dispatch pattern
to manage this.
Instead of passing the entity as a ctor argument, make it a property, or just pass the properties that are needed.
This keeps the entity out of the UI completely.
构造函数中是否需要传递参数?如果不是,我建议通过命名约定创建对象(正如您的情况表明这是可能的),然后始终使用约定,通过反射设置每个属性。由于 XML 中的值始终是字符串,因此您可能希望将其在 Convert.ChangeType() 中传递到目标属性类型,以获得良好的工作分配。
Is it necessary to pass the parameter on the constructor? If not I would suggest to create the object by naming convention ( as your case show that is possible ) and then , by using always a convention, via reflection set each property. Since the value you have in the XML is always a string, you probably want to pass it in a Convert.ChangeType() to the target property type to have a graceful working assignment.