从不同资源创建视图对象的最佳方法(模式?)

发布于 2024-12-18 03:57:56 字数 941 浏览 0 评论 0原文

目前,我正在从搜索结果(来自不同的单一资源)构造一个视图对象,如下所示:

ViewObject vo = searchResultToViewObjectMapper.map(searchResult);

这工作正常。

但是,现在我想添加一些图片。这些图片是网址,我只能通过搜索结果之外的其他资源来确定它们的位置。

我的第一个想法是使用 Builder 模式,它会变成:

ViewObject vo = viewObjectBuilder.build(searchResult);

viewObjectBuilder 会做这样的事情:

private SomeOtherResourceRepository someOtherResourceRepo;

private SomeUrlBuilder someUrlBuilder;

private SearchResultToViewObjectMapper searchResultToViewObjectMapper;

public ViewObject build(SearchResult) {
    ViewObject vo = searchResultToViewObjectMapper.map(searchResult);

    String reference = someOtherResourceRepo.getOtherResource(searchResult);

    String urlToOtherResource = someUrlBuilder.build(reference);
    vo.setUrlToOtherResource(reference);

    return vo;
}

问题是:这是一个好方法吗?或者还有其他(更好)的方法吗?我也很好奇 DDD 方法如何做到这一点。

提前致谢!

Currently I am constructing a view object from a search result (which comes from a different single resource) like this:

ViewObject vo = searchResultToViewObjectMapper.map(searchResult);

This works fine.

However, now I want to add some pictures. These pictures are url's and I can only determine their location via some other resource than the search result comes from.

My first thought would be to use the Builder Pattern, it would become:

ViewObject vo = viewObjectBuilder.build(searchResult);

and the viewObjectBuilder would do something like this:

private SomeOtherResourceRepository someOtherResourceRepo;

private SomeUrlBuilder someUrlBuilder;

private SearchResultToViewObjectMapper searchResultToViewObjectMapper;

public ViewObject build(SearchResult) {
    ViewObject vo = searchResultToViewObjectMapper.map(searchResult);

    String reference = someOtherResourceRepo.getOtherResource(searchResult);

    String urlToOtherResource = someUrlBuilder.build(reference);
    vo.setUrlToOtherResource(reference);

    return vo;
}

The question is: Is this a good way to this? Or are there other (better) ways? I'm also curious how a DDD approach would do this.

Thanks in advance!

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

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

发布评论

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

评论(1

浅暮の光 2024-12-25 03:57:56

如果您在创建对象之前拥有所有可用资源,那么使用工厂将会对您有用 - 只需将它们传递给工厂方法,它就会发挥作用。

如果您正在创建的对象(视图)是分步创建的 - 即首先您只有 searchResult,之后您会挖掘更多内容并获得一些额外的 URL,这些 URL 将添加到视图中,然后您进行更多搜索以获取更多信息,只有这样您才想要获取视图对象,构建器将是更好的解决方案。

Using a factory will work for you if you have all of the resources available before you create your object - just pass them on to the factory method and it will do the magic.

If the object you are creating (the view) is created in steps - i.e. first you only have the searchResult, after this you dig more and get some extra URLs which are added to the view, and then you do some more searching for more information, and only then you want to get the view object, a builder would be a better solution.

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