我试图避免以贫乏的域模型结束,所以我试图在域模型本身中保留尽可能多的逻辑。我有一个名为 AddIngredient
的方法,这需要添加一个新的KeyedObject
给我的Recipe
总计的。
由于域模型本身意味着没有存储库,因此我通过业务规则类获取成分:
public class Recipe : AggregateObject
{
public void AddIngredient(int ingId, double quantity)
{
GetIngredientMessage message = new GetIngredientMessage();
message.IngredientId = ingId;
GetIngredient handler = ServiceLocator.Factory.Resolve<GetIngredient>();
Ingredient ingredient = handler.Execute(message);
Ingredients.Add(new OriginalIngredient()
{
Ingredient = ingredient,
Quantity = quantity
});
}
}
如您所见,我使用的是
ServiceLocator.Factory.Resolve<GetIngredient>();
行。获取我的GetIngredient
业务规则类。 GetIngredient
是一个简单的命令处理程序,如下所示:public class GetIngredient : ICommandHandler<Ingredient, GetIngredientMessage>
{
private readonly IIngredientRepository _ingredientRepository;
public GetIngredient(IIngredientRepository ingredientRepository)
{
_ingredientRepository = ingredientRepository;
}
}
我将 IoC 工厂类分配给
ServiceLocator.Factory
,因此域有能力使用自己的接口(interface),而无需查看具体的类实现: ServiceLocator.Factory = new IoCFactory();
我很确定我做错了什么,因为这一切都感觉有点像笨蛋。
GetIngredient
没有对我的 IoC 工厂的静态引用? 最佳答案
我建议你在设计中引入另一层——应用层。该层的职责是将命令(显式封装在命令对象中或隐式传递为 int ingId, double quantity
)转换为域模型调用( Recipe.AddIngredient
)。
通过这样做,您将通过 id 查找成分的责任转移到域之上的层,您可以安全地直接使用存储库,而不会引入不必要的耦合。转换后的解决方案如下所示:
public class ApplicationLayer
{
private readonly IRecipeRepository _recipeRepository;
private readonly IIngredientRepository _ingredientRepository;
/*
* This would be called by IoC container when resolving Application layer class.
* Repositories would be injected by interfacy so there would be no coupling to
* concrete classes.
*/
public ApplicationLayer(IRecipeRepository recipeRepository, IIngredientRepository ingredientRepository)
{
_recipeRepository = recipeRepository;
_ingredientRepository = ingredientRepository;
}
public void AddIngredient(int recipeId, int ingId, double quantity)
{
var recipe = _recipeRepository.FindById(recipeId);
var ingredient = _ingredientRepository.FindById(ingId);
recipe.AddIngredient(ingredient, quantity);
}
}
现在简化的 Recipe 类看起来像这样:
public class Recipe : AggregateObject
{
public void AddIngredient(Ingredient ingredient, double quantity)
{
Ingredients.Add(new OriginalIngredient()
{
Ingredient = ingredient,
Quantity = quantity
});
}
}
希望有帮助。
关于c#-4.0 - 这是从域模型中实例化具有依赖关系的对象的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3709972/