当构造函数在我的 Domain 对象之一上执行时,我需要来自 RequestContext
的数据可用。以下示例是我已经在做的事情。
public Site(IDataUniverseService dataUniverse, IRegionService regionService)
{
this.DataUniverse = dataUniverse.GetDataUniverse(this.Id);
this.Region = regionService.GetRegion(this.Id, /* this value comes as a URL parameter */);
}
这很好,因为 StructureMap 可以轻松提供服务,因为它们来自域。
但是我怎样才能让它也提供一个到 RequestContext
的接口(interface)呢?或者我应该采取不同的做法?
这就是我的想法:
public Site(IDataUniverseService dataUniverse
, IRegionService regionService
, IRequestContext context)
{
var region = context.RouteData.Values["region"].ToString()
this.DataUniverse = dataUniverse.GetDataUniverse(this.Id);
this.Region = regionService.GetRegion(this.Id, region);
}
但理想情况下,IRequestContext
只会将该值作为属性随时可用,而不是 RouteData.Values["region"].ToString()
最佳答案
添加与您需要的 RequestContext 值相对应的参数,以便调用者可以提取必要的数据并仅发送该数据。您的域对象不应直接处理请求上下文。
关于c# - 如何向域对象的构造函数提供 RequestContext 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4281215/