将域实体属性映射到 Word 书签

发布于 2024-11-27 14:35:18 字数 720 浏览 0 评论 0原文

我们需要打印带有书签的Word文档。我们还有域实体,其属性映射到 Word 文档中的一个(或多个)书签。现在,为了测试,我们有一个 switch 语句(foo 是域实体):

    private static string GetValueForBookmark(Bookmark bookmark, Foo foo)
    {
        // ToDo: Un-switch this.

        switch (bookmark.Name)
        {
            case "dist":
                return foo.Measurment.Distance;
            case "serialNo":
                return foo.SerialNumber;
            default:
                return string.Empty;
        }
    }

我不想在框架中向 Foo 的属性添加属性。这看起来很混乱而且错误,因为域实体不应该知道所有这些一次性应用程序需求。

一种想法是创建书签名称到域实体属性的映射。也许创建一个表示书签名称的枚举,然后向这些表示域实体属性的枚举元素添加属性。属性必须指定这些属性,我不知道是否可以这样做。这里可以使用表达式树吗?

还有其他选择吗?我只是想将域实体属性映射到字符串文字。

解决这个问题的最佳方法是什么?

We need to print Word documents that have bookmarks in them. We also have domain entities whose properties map to one (or more) of the bookmarks in the Word documents. Right now, to test, we have a switch statement (foo is the domain entity):

    private static string GetValueForBookmark(Bookmark bookmark, Foo foo)
    {
        // ToDo: Un-switch this.

        switch (bookmark.Name)
        {
            case "dist":
                return foo.Measurment.Distance;
            case "serialNo":
                return foo.SerialNumber;
            default:
                return string.Empty;
        }
    }

I would hate to add attributes to the properties of Foo in the framework. It seems messy and wrong, since the domain entities shouldn't know about all these one-off application needs.

One idea is to create a mapping of bookmark names to domain entity properties. Maybe create an enum that represents the bookmark names, then add attributes to these enum elements, that represent domain entity properties. The attributes would have to specify these properties, and I don't know if that can be done. Can expression trees be used here?

Is there another option? I simply want to map domain entity properties to string literals.

What is the best way to solve this?

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

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

发布评论

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

评论(1

挽梦忆笙歌 2024-12-04 14:35:18

我只是这样做:

    private static void MapBookmarksToFooProperties()
    {
        if (_bookmarkToFooPropertyMapping != null) { return; }  // We already created the mapping.

        _bookmarkToFooPropertyMapping = new Dictionary<string, Func<Foo, string>>();

        // Associate Word document bookmarks to their properties within a Foo object.

        _bookmarkToFooPropertyMapping.Add("dist", foo => foo.Measurment.Distance);
        _bookmarkToFooPropertyMapping.Add("serialNo", foo => foo.SerialNumber);
        // More mapping here
    }     

然后,访问它就这么简单:

return _bookmarkToFooPropertyMapping[bookmark.Name](foo);

I just went with this:

    private static void MapBookmarksToFooProperties()
    {
        if (_bookmarkToFooPropertyMapping != null) { return; }  // We already created the mapping.

        _bookmarkToFooPropertyMapping = new Dictionary<string, Func<Foo, string>>();

        // Associate Word document bookmarks to their properties within a Foo object.

        _bookmarkToFooPropertyMapping.Add("dist", foo => foo.Measurment.Distance);
        _bookmarkToFooPropertyMapping.Add("serialNo", foo => foo.SerialNumber);
        // More mapping here
    }     

Then, accessing it, is as simple as this:

return _bookmarkToFooPropertyMapping[bookmark.Name](foo);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文