如何创建将站点配置值复制到输出模型的 FubuMVC 行为?
我试图找出创建一种将布尔站点配置值复制到输出模型的行为。
这样我就不必在视图需要的每个操作中复制布尔值,而可以简单地将行为添加到需要该值的控制器操作中。
在 FubuMVC 的一些旧版本中,我相信行为可以在输出模型离开控制器后修改它。但我不确定如何在最新版本的 FubuMVC 中执行此操作(或者我已经忘记了)。
谁能给我举个例子,或者为我指明将站点配置值复制到输出模型的最佳实践方向?
I'm trying to figure out to create a behavior that will copy a boolean site configuration value to an output model.
This way I don't have to copy the bool in each action who's view requires it, but can simply add the behavior to the controller actions that need this value.
In some of the older versions of FubuMVC, I believe behaviors could modify the output model after it's left the controller. But I'm not sure how to do this in the more recent versions of FubuMVC (or I've forgotten).
Can anyone give me an example of or point me in the direction of the best practice for copying a site configuration value to an output model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设我有一个名为
HomeViewModel
的输出模型,它有一个名为FooterText
的属性,我想从设置对象(假设是HomeSettings
)加载该属性,该对象是从容器中检索(即 StructureMap)。行为
我的行为看起来像这样:
此行为接收
HomeSettings
对象和IFubuRequest
对象(都是注入的依赖项),然后获取请求中的HomeViewModel
(输出模型),然后根据设置对象中的值设置输出模型上的HomeFooterText
属性。注意:我假设您已经将
HomeSettings
对象连接到容器中(例如,使用 FubuMVC 中内置的 ISettingsProvider 内容)。如果您还没有这个,请告诉我,我可以发布一些关于如何执行此操作的代码。连接约定
要连接行为,您需要通过
IConfigurationAction
定义约定,例如:这是用于演示目的的真正愚蠢的约定。在您的项目中,您可能会使其更通用一些。例如,任何具有属性的输出模型,或实现特定接口等。事实上,您可能想要检查所有输出模型,看看它们是否包含与特定属性匹配的任何属性。标准(例如,所有以“Settings”结尾的属性 - 例如“FooterSettings”或其他内容)。
不要因为性能问题而害怕定义像这样的广泛的约定,因为所有这些约定代码都在启动时运行,而不是在每个请求上运行。
请注意“AddAfter”调用和“Wrapper.For”调用。这是关键,因为它将您的行为放置在执行控制器操作之后、渲染视图之前。
现在您已经定义了行为和约定,是时候将其连接到 FubuRegistry 中了。
在 FubuRegistry 中连接约定
在调用“Routes”之后。在您的 FubuRegistry 中,添加如下行:
重新编译,它应该可以工作。
如果您遇到任何问题,请告诉我。
Let's say I had an output model called
HomeViewModel
that had a property calledFooterText
that I wanted loaded from settings object (let's sayHomeSettings
) that was retrieved from the container (i.e. StructureMap).The Behavior
My behavior would look something like this:
This behavior takes in the
HomeSettings
object and theIFubuRequest
object (both injected dependencies) and then gets theHomeViewModel
(output model) from the request and then sets theHomeFooterText
property on the output model based on the value from the settings object.NOTE: I'm assuming that you've already got your
HomeSettings
object wired up in the container (for example, using the ISettingsProvider stuff built into FubuMVC). If you don't already have this, let me know and I can post some code on how to do that.Wiring Up The Convention
To wire up the behavior, you'll need to define the convention through an
IConfigurationAction
, for example:This is a real dumb convention for demonstration purposes. In your project, you might make it a little more generic. For example, any output model that has an attribute on it, or implements a specific interface, etc. In fact, you might want to inspect all output models to see if they contain any properties that match a certain criteria (for example, all properties that end with "Settings" - like "FooterSettings" or something).
Don't be afraid to define wide sweeping conventions like this due to performance concerns since all this convention code runs at start-up time and not on every request.
Note the "AddAfter" call and the "Wrapper.For" call. That's the key in that it places your behavior after the controller action is executed, but before the view is rendered.
Now that you have your behavior and your convention defined, it's time to wire it up in your FubuRegistry.
Wiring Up Your Convention in your FubuRegistry
After the call to "Routes." in your FubuRegistry, add a line like this:
Recompile and it should work.
Please let me know if you run into any problems.