如何编写转换器类?如何高效编写映射规则? [英] How to write a converter class? How to efficiently write mapping rules?

查看:28
本文介绍了如何编写转换器类?如何高效编写映射规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的A类,成员很多,我有一个很大的B类可以在拥有 A 对象时构建.当有一个 A 对象时可以构建一个 B 对象.我需要它们,因为 A 是一种具有验证功能的 ViewModelB为图形描述,易于绘制.

I have a quite big class A with lots of members, and i have a quite big class B that can be built when having an A object. An A object can be built when having a B object. I need both of them, as A is a kind of ViewModel, which has validation and B is a graphical description, which can be plotted easily.

如何进行这种转换?

这是一个例子,说明我想做什么:

here is an example, to illustrate what i want to do:

class A
{
    string s;
    string t;
    string u;
    string v;

    enum a;
    enum b;
    enum c;
    enum d;
    enum e;

    Dictionary<enum, string> dict;
}


class B
{
    string someString; // is essentially A.a + A.b + A.c + A.s with some rules.
    int someValue; // is essentially dict.TryGetValue(enum.Entry);
    string anotherString;
    // ... and lots of others
}

当然做一些映射很简单,建立一个B对象,并且通过反转来编写普通的 B => A 映射并不是很难构建 A => B

Of course it is simple to do some mapping, and build up a B object, and it is not very hard to write plain B => A mapping by inverting the rules of building A => B

所以问题是:

  • 是否有任何众所周知的模式可以实现这一目标?
  • 是否有默认的 C# 方式来做这些事情?

简单地写下这样的东西似乎不合适,它以数百行代码结束.

Writing down plain something like that seems not to fit, it ends up in hundreds of lines of code.

我想到了一些零件的转换器类,比如 SomeStringConverter, SomeValueConverter, ...

I thought about some kind of converter classes for the parts, like SomeStringConverter, SomeValueConverter, ...

  • 我如何将 A 的所需成员与进行映射的规则.
  • 我如何编写这些规则以尽可能简化做 A => B 和 B => A 的方式.

这里的模式是指最佳实践"而不是GoF 设计模式"

Pattern here is meant as "Best practises" and not as "GoF design patterns"

B 类中的 SomeString 是某种选择器",它选择绘图选项,它总是 25 个字符长,A 类中的枚举选择那些字符,但在大多数情况下不是 1 对 1.

SomeString in the B class is some kind of "selector", it selects drawing options, it is always 25 characters long and the enums in class A choose those characters, but not 1 on 1 in most cases.

例如:A.a = "Filled", A.b = "SingleCoordinate", A.c = "DrawHints"将导致类似 SomeString =

Lets say for example: A.a = "Filled", A.b = "SingleCoordinate", A.c = "DrawHints" will result in something like SomeString =

 "Y**D***RR****---***---***"

即组合对于获得这样的字符串很重要,但是从组合您可以导出必须在 A 对象中设置的枚举.

i.e. the combination is important to get such an string, but from the combiniation you can derive the enums that have to be set in an A object.

编辑 2:

我对两种方式都使用我的映射规则的方式特别感兴趣,即Aa = "Filled" 结合 Ab = "SingleCoordinate" 结合 Ac = "DrawHints" 将导致(部分字符串)"Y**D***RR",并且该部分字符串也意味着那A.a 必须设置为已填充"等.

I'm especially interested in ways of using my mapping rules for both ways, i.e. A.a = "Filled" combined with A.b = "SingleCoordinate" combinded with A.c = "DrawHints" will result in (partial string) "Y**D***RR", and that partial string also means that A.a has to be set to "Filled" and so on.

推荐答案

我做了什么来解决这个问题:

What I have done to solve this problem:

  1. 编写了一个 BiMap(在有关该问题的旧 SO 的帮助下).

  1. Wrote a BiMap (with the help of older SOs on that issue).

将映射插入其中(Key 中的所有值组合,Value"中的结果键,以及指定字符串索引的 BitArray由该映射定义.

Inserted the Mapping into it (all value combinations in Key, the resulting key in the "Value", together with a BitArray which specifies the indices of the string that are defined by that mapping.

写了一些代码来计算由此产生的整体字符串,作为映射只会给出部分字符串.

Wrote some code to calculate a resulting overall string from it, as the mapping will just give partial strings.

一对一的映射很简单.

这样我就可以以两种方式使用映射.给定一个字符串,我仍然有一个相当昂贵的搜索(因为我必须使用 BitArray 作为掩码来计算存储的字符串)

That way I could use the mappings in both ways. Given a string, I still have a quite expensive search (as I have to compute the stored string utilizing the BitArray as a mask)

目前它似乎工作得很好,但我还没有完成.

At the moment it seems to work very good, but I'm not finished yet.

感谢大家提出的非常好的想法和方法!也许 AutoMapper 可以做到但是我目前没有太多时间阅读和尝试一项新技术.

Thank you all for your very good ideas and approaches! Perhaps AutoMapper can do this, but I do not have much time at the moment to read and try out a new technology.

如果有人可以提供一些有关如何在 Automapper 上执行此操作的相关示例,我会接受它作为答案(因为我已经喜欢 AutoMapper).

例如让我们说:3个枚举,5个值到一个固定长度的字符串一个固定长度的字符串到 3 个有 5 个值的枚举(与上面相反).

Examples like lets say: 3 enums with 5 values to one fixed length string one fixed length string to 3 enum with 5 values (the reverse of the above).

例如:

A.a && B.o && C.y ==> "**A**********************"
A.a && B.p && C.y ==> "**B**********************"
A.b && B.o && C.y ==> "*****X*******************"
A.b && B.o && C.z ==> "*****W*******************"

Automapper 能做到吗?

Can Automapper achieve such things?

这篇关于如何编写转换器类?如何高效编写映射规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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