文字列をテンプレートパラメータに変換できます。あるいは、逆シリアル化されたクラスをクラス/関数テンプレートのパラメータとして渡すという概念を達成するための慣用的な方法です。Object.factory(...)を介した文字列型への文字列
概念はDDD, CQRS and Eventsourcingに基づいています。
import std.stdio;
void main()
{
auto aggregate = new UserAggregate();
auto command = new CreateUser();
// in a command bus, aggregates are loaded based on the aggregate type, then commands and events are applied.
aggregate.handle(command);
// NOTE typecast cannot be used as events will be loaded from an event stream and deserialized - only the
// class name (fully qualified module path) is known at runtime.
auto userCreated = Object.factory("app.UserCreated");
// .... deserialization of event ....
aggregate.apply(userCreated); // <-- compile error - type not specific enough and casting is not possible
}
// interfaces
interface IHandleCommand(TCommand : ICommand)
{
void handle(TCommand command);
}
interface IApplyEvent(TEvent : IEvent)
{
void apply(TEvent event);
}
interface ICommand
{
// ....
}
interface IEvent
{
// ....
}
// Implementation
class UserAggregate :
IHandleCommand!CreateUser,
IHandleCommand!ChangeUserStatus,
IApplyEvent!UserCreated,
IApplyEvent!UserStatusChanged
{
void handle(CreateUser createUser)
{
writeln("createUser...");
}
void handle(ChangeUserStatus changeUserStatus)
{
writeln("changeUserStatus...");
}
void apply(UserCreated userCreated)
{
writeln("userCreated...");
}
void apply(UserStatusChanged userStatusChanged)
{
writeln("userStatusChange...");
}
}
// Commands
class CreateUser : ICommand
{
// ....
}
class ChangeUserStatus : ICommand
{
// ....
}
// Events
class UserCreated : IEvent
{
// ....
}
class UserStatusChanged : IEvent
{
// ....
}
使用してください[ショート、自炊、(コンパイル可能)正しい、例】(http://sscce.org/)■。誰もあなたが持っているインターフェースの数を気にせず、実際の問題から気をそらす。 – weltensturm
'void apply(UserCreated userCreated)'を 'void apply(IEvent userCreated)'に変更しようとしましたか? – DejanLekic
@DejanLekic 1つのインタフェースタイプのみを使用して集約でイベントを識別する場合、どのように異なるイベントタイプを扱うでしょうか? – MX4399