ValueInjecter - 递归映射

发布于 2024-10-29 21:50:49 字数 2443 浏览 1 评论 0原文

我对自动映射还很陌生,甚至对 ValueInjecter 还比较陌生。如果这是一个简单的问题,我很抱歉,但我似乎无法在任何地方找到答案。

我将如何进行递归映射?例如,这是我当前的工厂方法,它将一个主题(从我的数据服务)映射到一个主题视图模型:

private SubjectViewModel createSubject(DataService.SubjectResult s) {
    SubjectViewModel result = new SubjectViewModel();
    result.SubjectName = s.SubjectName;
    result.id = s.id;

    foreach (DataService.SubjectResult sChild in s.Children)
        result.ChildSubjects.Add(createSubject(sChild));

    return result;
}

我猜这对于 ValueInjecter 来说是一种面包和黄油类型的情况,所以如果有人可以指出我正确的方向我会很感激!

这是一个假的,但完整的代码示例:

[TestClass]
public class UnitTest1 {

    [TestMethod]
    public void TestMethod1() {
        Subject S = new Subject() {
            id = 1,
            SubjectName = "S1", Children = { new Subject() { id = 2, SubjectName = "S1a" },
                                             new Subject() { id = 3, SubjectName = "S1b" }}
        };

        SubjectViewModel VM = new SubjectViewModel();
        VM.InjectFrom<CollectionToCollection>(S);

        Assert.AreEqual(2, VM.Children.Count);
    }
}


public class Subject {
    public Subject() {
        Children = new List<Subject>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<Subject> Children { get; private set; }
}

public class SubjectViewModel {
    public SubjectViewModel() {
        Children = new List<SubjectViewModel>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<SubjectViewModel> Children { get; set; }
}

public class CollectionToCollection : Omu.ValueInjecter.ConventionInjection {
    protected override bool Match(ConventionInfo c) {
        return c.SourceProp.Name == "Children";
    }

    protected override object SetValue(ConventionInfo c) {
        List<SubjectViewModel> result = new List<SubjectViewModel>();
        foreach (Subject S in (c.SourceProp.Value as IEnumerable<Subject>))
            result.Add(new SubjectViewModel());
        return result;
    }
}

编辑 - 我知道这是一种幼稚的方法,不是递归的。现在我只是想在没有例外的情况下走到这一步

现在我得到:

System.ArgumentException:对象 类型 'System.Collections.Generic.List`1[TestValueInjector.SubjectViewModel]' 无法转换为类型 '系统.字符串'。

I'm pretty new to auto-mapping, and even newer to ValueInjecter. I'm sorry if this is an easy question, but I can't seem to find the answer anywhere.

How would I do a recursive mapping? For example, here is my current factory method that maps a Subject (from my data service) to a SubjectViewModel:

private SubjectViewModel createSubject(DataService.SubjectResult s) {
    SubjectViewModel result = new SubjectViewModel();
    result.SubjectName = s.SubjectName;
    result.id = s.id;

    foreach (DataService.SubjectResult sChild in s.Children)
        result.ChildSubjects.Add(createSubject(sChild));

    return result;
}

I'm guessing this is a bread-and-butter type of situation for ValueInjecter, so if someone could just point me in the right direction I'd appreciate it!

Here's a fake, but complete code sample:

[TestClass]
public class UnitTest1 {

    [TestMethod]
    public void TestMethod1() {
        Subject S = new Subject() {
            id = 1,
            SubjectName = "S1", Children = { new Subject() { id = 2, SubjectName = "S1a" },
                                             new Subject() { id = 3, SubjectName = "S1b" }}
        };

        SubjectViewModel VM = new SubjectViewModel();
        VM.InjectFrom<CollectionToCollection>(S);

        Assert.AreEqual(2, VM.Children.Count);
    }
}


public class Subject {
    public Subject() {
        Children = new List<Subject>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<Subject> Children { get; private set; }
}

public class SubjectViewModel {
    public SubjectViewModel() {
        Children = new List<SubjectViewModel>();
    }

    public string SubjectName { get; set; }
    public int id { get; set; }

    public List<SubjectViewModel> Children { get; set; }
}

public class CollectionToCollection : Omu.ValueInjecter.ConventionInjection {
    protected override bool Match(ConventionInfo c) {
        return c.SourceProp.Name == "Children";
    }

    protected override object SetValue(ConventionInfo c) {
        List<SubjectViewModel> result = new List<SubjectViewModel>();
        foreach (Subject S in (c.SourceProp.Value as IEnumerable<Subject>))
            result.Add(new SubjectViewModel());
        return result;
    }
}

EDIT - I know this is a naive approach, that's not recursive. Right now I'm just trying to get this far without the exception

Right now I get:

System.ArgumentException: Object of
type
'System.Collections.Generic.List`1[TestValueInjector.SubjectViewModel]'
cannot be converted to type
'System.String'.

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

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

发布评论

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

评论(1

十二 2024-11-05 21:50:49

您收到此错误的原因是因为您将源属性 Children 与目标对象中的所有属性相匹配,因此为所有匹配项调用 SetValue。

你需要的是这样的:

protected override bool Match(ConventionInfo c)
{
   return c.SourceProp.Name == c.TargetProp.Name 
          && c.SourceProp.Name == "Children";
}

the reason you were getting this error is because you were matching the source property Children with all the properties from the target object, so SetValue is called for all the matches.

What you needed was this:

protected override bool Match(ConventionInfo c)
{
   return c.SourceProp.Name == c.TargetProp.Name 
          && c.SourceProp.Name == "Children";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文