【发布时间】:2015-08-11 22:52:29
【问题描述】:
我有一个名为MudEngine.Core 的项目的解决方案。这个项目包含一些基本类,然后是我的领域对象抽象的所有接口。 IWorld 和 IRealm 等接口。
域接口示例
public interface IWorld : IGameComponent, ICloneableComponent<IWorld>
{
/// <summary>
/// Gets how many hours it takes to complete one full day in this world.
/// </summary>
int HoursPerDay { get; }
/// <summary>
/// Gets or sets the game day to real hour ratio.
/// </summary>
double GameDayToRealHourRatio { get; set; }
/// <summary>
/// Adds a collection of realms to world, initializing them as they are added.
/// </summary>
/// <param name="realms">The realms.</param>
/// <returns>
/// Returns an awaitable Task
/// </returns>
IRealm[] GetRealmsInWorld();
/// <summary>
/// Initializes and then adds the given realm to this world instance.
/// </summary>
/// <param name="realm">The realm to add.</param>
/// <returns>Returns an awaitable Task</returns>
Task AddRealmToWorld(IRealm realm);
/// <summary>
/// Creates and initializes a new instance of a realm.
/// </summary>
/// <param name="name">The name of the realm.</param>
/// <param name="owner">The world that owns this realm.</param>
/// <returns>Returns an initialized instance of IRealm</returns>
Task<IRealm> CreateRealm(string name, IWorld owner);
/// <summary>
/// Adds a collection of realms to world, initializing them as they are added.
/// </summary>
/// <param name="realms">The realms.</param>
/// <returns>Returns an awaitable Task</returns>
Task AddRealmsToWorld(IEnumerable<IRealm> realms);
/// <summary>
/// Removes the given realm from this world instance, deleting the realm in the process.
/// If it must be reused, you may clone the realm and add the clone to another world.
/// </summary>
/// <param name="realm">The realm to remove.</param>
/// <returns>Returns an awaitable Task</returns>
Task RemoveRealmFromWorld(IRealm realm);
/// <summary>
/// Removes a collection of realms from this world instance.
/// If any of the realms don't exist in the world, they will be ignored.
/// The realms will be deleted during the process.
/// If they must be reused, you may clone the realm and add the clone to another world.
/// </summary>
/// <param name="realms">The realms collection.</param>
/// <returns>Returns an awaitable Task</returns>
Task RemoveRealmsFromWorld(IEnumerable<IRealm> realms);
}
我还有一个名为 MudEngine.Mud 的项目,它是所有接口的默认实现。
MudEngine.Core 项目包括我的工厂的接口。像IWorldFactory 和IRealmFactory 这样的工厂。 IWorld 接口有一个创建方法,它使用给定的IRealmFactory 创建领域并返回它们。
示例工厂接口
/// <summary>
/// Provides methods for creating an instance of an IRealm implementation
/// </summary>
public interface IRealmFactory
{
/// <summary>
/// Creates and initializes a new instance of a realm.
/// </summary>
/// <param name="name">The name of the realm.</param>
/// <param name="owner">The world that owns this realm.</param>
/// <returns>Returns an initialized instance of IRealm</returns>
Task<IRealm> CreateRealm(string name, IWorld owner);
/// <summary>
/// Creates and initializes a new instance of a realm.
/// </summary>
/// <param name="name">The name of the realm.</param>
/// <param name="owner">The world that owns this realm.</param>
/// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
/// <returns>Returns an initialized instance of IRealm</returns>
Task<IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset);
/// <summary>
/// Creates and initializes a new instance of a realm.
/// All of the children zones will be initialized prior to being added to the realm.
/// </summary>
/// <param name="name">The name of the realm.</param>
/// <param name="owner">The world that owns this realm.</param>
/// <param name="zones">A collection of zones that will be initialized and added to the realm.</param>
/// <returns>Returns an initialized instance of IRealm</returns>
Task<IRealm> CreateRealm(string name, IWorld owner, IEnumerable<IZone> zones);
/// <summary>
/// Creates and initializes a new instance of a realm.
/// All of the children zones will be initialized prior to being added to the realm.
/// </summary>
/// <param name="name">The name of the realm.</param>
/// <param name="owner">The world that owns this realm.</param>
/// <param name="timeZoneOffset">The time zone offset to apply to the realm.</param>
/// <param name="zones">A collection of zones that will be initialized and added to the realm.</param>
/// <returns>Returns an initialized instance of IRealm</returns>
Task<IRealm> CreateRealm(string name, IWorld owner, ITimeOfDay timeZoneOffset, IEnumerable<IZone> zones);
}
当我在 IWorld 接口上调用 CreateRealm 时,实现使用 IRealmFactory 来创建它。工厂是通过 IWorld 实现的构造函数传入的。
我现在的问题是,工厂实现应该存在于哪里?包含域接口实现的同一个项目为工厂提供实现是常见的,还是应该由消费层(例如演示/单元测试项目)负责实现工厂并使用它们?
目的是这些组件可以根据您正在构建的基于文本的游戏类型进行交换。所以我倾向于每个实现接口的包都有自己的工厂。我关心的是DI设置。分层中的 IoC 容器(服务器/客户端应用程序)需要知道它应该使用每个包中的哪个工厂,而不是仅使用 IoC 容器所属的消费层中定义的工厂。
对此有任何行业标准指导吗?
【问题讨论】:
-
不要把事情复杂化。 Di Container 本身就是一个工厂,通常在应用程序启动时进行配置。我个人在任何项目/组件中都有 autofac 模块。如果您需要一个真正的工厂,一个您根据某些特定规则创建对象的工厂,那是您的域的一部分,因为它封装了域规则。并且没有任何行业标准。
-
@JohnathonSullinger "取决于您构建的基于文本的游戏类型" => 这是否意味着应用程序的用户是游戏管理员?甚至,他必须为自己的游戏编写模块,然后自己实现
IRealmFactory? -
@guillaume31 想要构建 Mud 的人必须是游戏管理员,负责构建内容。他们可以从一系列不同的设置中进行选择,例如 D&D 构建或探路者构建。我为这些类提供了不同的实现作为插件,它们通过编辑器选择要运行的类。如果他们不喜欢可用的东西,他们只需要构建一个自定义的 Irealm。我不想将引擎装箱以阻止人们扩展它。
-
与问题无关,但为什么你必须将
IWorld传递给IWorld.CreateRealm?你真的会使用一个世界的实例来创建一个属于另一个世界实例的领域吗?
标签: c# domain-driven-design factory-pattern