如何提供RequestContext的数据域对象的构造函数? [英] How to supply RequestContext data to the constructor of a domain object?

查看:182
本文介绍了如何提供RequestContext的数据域对象的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据中 RequestContext的来时可用的构造上我的域对象之一执行。下面的例子是什么我已经在做。

 公共网站(IDataUniverseService dataUniverse,IRegionService regionService)
$ { b $ b this.DataUniverse = dataUniverse.GetDataUniverse(this.Id);
this.Region = regionService.GetRegion(this.Id,/ *这个值是作为一个URL参数* /);
}

这是很好,因为,因为它们来自域StructureMap可以很容易地提供服务



但我怎么才能得到它也提供一个接口 RequestContext的? ?或者我应该做不同的这个



这是什么样的我心里有:

 公共网站(IDataUniverseService dataUniverse 
,IRegionService regionService
,IRequestContext上下文)
{
变种区= context.RouteData.Values [区。的ToString()

this.DataUniverse = dataUniverse.GetDataUniverse(this.Id);
this.Region = regionService.GetRegion(this.Id,区);
}



但理想情况下, IRequestContext 只会有现成的作为一个属性,而不是 RouteData.Values [区域]该值。的ToString()


< DIV CLASS =h2_lin>解决方案

添加对应什么值你的RequestContext需要那么调用者可以提取必要的数据,并只发送参数。您正在访问的对象不应该直接处理请求上下文。


I need data from the RequestContext to be available when the constructor executes on one of my Domain objects. The following example is what I'm already doing.

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 */);
}

This is fine because StructureMap can easily supply the services because they come from the domain.

But how can I get it to also supply an interface to the RequestContext? Or should I be doing this differently?

This is kind of what I have in mind:

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);
}

but ideally, the IRequestContext would just have that value readily available as a property, rather than the RouteData.Values["region"].ToString()

解决方案

Add parameters corresponding to what values you need from RequestContext so the caller can extract the necessary data and send only that. Your domain objects shouldn't deal with the request context directly.

这篇关于如何提供RequestContext的数据域对象的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆