DDD 和工厂

发布于 2024-12-07 04:50:29 字数 1480 浏览 1 评论 0原文

您好,我有一些关于领域驱动设计和使用工厂/工厂方法的问题。 根据领域驱动设计蓝皮书(Eric EVan 的书),它指出复杂的构造函数应该封装在工厂/工厂方法/构建器中,这样就有一个一致的地方可以检查所有不变量,所以我的问题是关于这一点的:

假设我正在开发一个魔术组织应用程序,您可以在其中对魔术效果进行类似 CRUD 的操作(例如博客上的帖子+几个属性,例如效果持续时间、使用的材料(字符串列表)、与魔术效果相关的模式)以及一些不变量是魔术效果必须始终具有标题、魔术效果的内容、持续时间和可选模式,并且必须由在应用程序中注册的用户发布。

因此,由于我有很多不变量,所以我有一个 EffectBuilder 来构建 MagicEffect 对象并检查所有不变量。

在用户类中做这样的事情可以吗?

public class User {
// Several attributes and business methods

public MagicEffect publishEffect(final String title, final String content, final Long duration, final Collection<String> elements) [
EffectBuilder builder = new EffectBuilder();

builder.withAuthor(this);
builder.withTitle(title);
builder.withContent(content);
builder.withDuration(duration);
builder.withElements(elements);

return builder.build();
}
};

或者我应该做类似的事情:

public class User {
// Several attributes and business methods

public EffectBuilder publishEffect() [
EffectBuilder builder = new EffectBuilder();

builder.withAuthor(this);

return builder;
}
};

在其他地方,

User user = userRepository.findById(userId);
MagicEffect effect = user.publishEffect().withTitle(title).withContent(content).withDuration(duration).withElements(elements).build();

userRepository.save(user);

我的意思是第一个例子,我有一个带有大量参数的巨大方法,但我确保所有不变量在构建时都设置在效果中,在其他情况下,我以编程方式改进代码通过拥有流畅的界面来提高可读性,但我无法确保 100% 的时间都满足不变量。

哪种方法更好?有没有更平衡的方法来做到这一点?

谢谢 巴勃罗

Hi I have a few questions regarding Domain Driven Design and using Factories / Factory Methods.
Per the Domain Driven Design Blue Book (Eric EVan's Book) it states that complex constructors should be encapsulated inside Factories / Factory Methods / Builders so there is a consistent place where you check all the invariants, so my question is regarding this:

Let's say I am developing a magic organizer application where you can make CRUD like operations on magic effects (like a post on a blog + several attributes like effect duration, materials used (list of strings), patter associated with the magic effect) and some of the invariants are that a magic effect must always have a title, a content of the magic effect, a duration and an optional patter and must be published by a user registered in the application.

So since I have quite a few invariants I have a EffectBuilder that builds MagicEffect objects and checks all the invariants.

Is it ok to do something like this in the user class?

public class User {
// Several attributes and business methods

public MagicEffect publishEffect(final String title, final String content, final Long duration, final Collection<String> elements) [
EffectBuilder builder = new EffectBuilder();

builder.withAuthor(this);
builder.withTitle(title);
builder.withContent(content);
builder.withDuration(duration);
builder.withElements(elements);

return builder.build();
}
};

Or should I do something like:

public class User {
// Several attributes and business methods

public EffectBuilder publishEffect() [
EffectBuilder builder = new EffectBuilder();

builder.withAuthor(this);

return builder;
}
};

And somewhere else

User user = userRepository.findById(userId);
MagicEffect effect = user.publishEffect().withTitle(title).withContent(content).withDuration(duration).withElements(elements).build();

userRepository.save(user);

I mean the first example I have a huge method with huge amount of parameters but I make sure all the invariants are set in the effect when it's built, in the other scenario I programatically improve the code readability by having a fluent interface but I canot make sure the invariants are met 100% of the time.

Which is the better approach? Is there a more balanced approach of doing it?

Thanks
Pablo

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

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

发布评论

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

评论(1

也只是曾经 2024-12-14 04:50:29

我认为你的第二种方法更好。 Builder 的重点是避免像第一个示例中那样出现大量参数。构建器不负责在它构建的对象中强制执行不变量。对象本身强制执行它们。我认为拥有一个没有标题或带有默认标题的 EffectBuilder 实例是完全可以的。只要 MagicEffect 本身强制执行“每个效果都应该有一个标题”不变式。

I think that your second approach is better. The whole point of Builder is to avoid large list of parameters like you have in your first example. Builder is not responsible for enforcing invariants in the object that it builds. Object itself enforces them. I think it is perfectly fine to have an instance of EffectBuilder without Title or with a default title. As long as the MagicEffect itself enforces 'Every effect should have a title' invariant.

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