如何找到行为中的输出模型类型?

发布于 2024-10-19 13:18:29 字数 151 浏览 3 评论 0原文

使用 FubuMVC,我不确定确定当前操作的输出模型类型的最佳方法是什么。我看到可以从中获取当前请求 URL 的不同对象。但这并没有带来很好的解决方案。

从行为中获取当前操作的输出模型类型的最简单方法是什么?

如果这不是一个好的做法,还有什么更好的方法呢?

With FubuMVC, I'm not sure what the best way is to determine the current action's output model type. I see different objects that I could get the current request's URL from. But that doesn't lead to a very good solution.

What's the easiest way to get the current action's output model type from the behavior?

If this isn't a good practice, what's a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

作妖 2024-10-26 13:18:29

首先,我假设您已经在 StructureMap 中设置了设置对象,并且已经连接了 ISettingsProvider 内容。

最好、最简单的做法就是在视图中提取设置,如下所示:

<%: Get().SomeSettingProperty %>

如果您坚持将这些设置为输出模型上的一个属性,然后继续阅读:

假设您有一个如下所示的设置对象:

    public class OutputModelSettings
    {
        public string FavoriteAnimalName { get; set; }
        public string BestSimpsonsCharacter { get; set; }
    }

然后您有一个如下所示的输出模型:

    public class OutputModelWithSettings
    {
        public string SomeOtherProperty { get; set; }
        public OutputModelSettings Settings { get; set; }
    }

您需要做一些事情:

  1. 连接 StructureMap,以便它将为 Settings 对象执行 setter 注入(因此它会自动将 OutputModelSettings 注入到输出模型的“Settings”属性中。

    在 StructureMap 初始化代码(注册表、全局 ASAX、Bootstrapper 等 - 无论您在何处设置容器)中设置 setter 注入策略。

    x.SetAllProperties(s => s.Matching(p => p.Name.EndsWith("设置")));
    
  2. 创建您的行为以在输出模型上调用 StructureMap 的“BuildUp()”以触发设置器注入。该行为将是开放类型(即最后)。它可以支持任何类型的输出模型

    public class OutputModelSettingBehavior; : 基本行为
        其中 TOutputModel :类
    {
        私有只读 IFubuRequest _request;
        私有只读 IContainer _container;
    
        public OutputModelSettingBehavior(IFubuRequest请求,IContainer容器)
            : 基础(PartialBehavior.执行)
        {
            _请求=请求;
            _container = 容器;
        }
    
        受保护覆盖​​ DoNext PerformInvoke()
        {
            绑定设置属性();
    
            返回DoNext.Continue;
        }
    
        公共无效BindSettingsProperties()
        {
            var viewModel = _request.Find().First();
            _container.BuildUp(viewModel);
        }
    }
    
  3. 创建约定来连接行为

    公共类OutputModelSettingBehaviorConfiguration:IConfigurationAction
    {
        公共无效配置(BehaviorGraph图)
        {
            图.Actions()
                .Where(x => x.HasOutput &&
                            x.OutputType().GetProperties()
                                .Any(p => p.Name.EndsWith("设置")))
                .Each(x => x.AddAfter(新包装(
                    typeof (OutputModelSettingBehavior<>)
                    .MakeGenericType(x.OutputType()))));
        }
    }
    
  4. 在路由部分之后将约定连接到 FubuRegistry 中:

    ApplyConvention();
    
  5. 在您的视图中,使用新的设置对象:

    <%: Model.Settings.BestSimpsonsCharacter %>
    

注意:我已将其作为工作示例提交到 Fubu 源的 FubuMVC.HelloWorld 项目中。请参阅此提交: https://github.com/DarthFubuMVC/fubumvc/commit/2e7ea30391eac0053300ec0f6f 63136503b16cca

First, I'm assuming you've already got your settings object(s) set up in StructureMap and have the ISettingsProvider stuff already wired up.

The best, simplest thing to do would be just to pull the settings in the view, like this:

<%: Get<YourSettingsObject>().SomeSettingProperty %>

If you insist on having these be a property on your output model, then continue reading:

Let's say you had a settings object like this:

    public class OutputModelSettings
    {
        public string FavoriteAnimalName { get; set; }
        public string BestSimpsonsCharacter { get; set; }
    }

Then you had an output model like this:

    public class OutputModelWithSettings
    {
        public string SomeOtherProperty { get; set; }
        public OutputModelSettings Settings { get; set; }
    }

You'll need to do a few things:

  1. Wire up StructureMap so that it will do setter injection for Settings objects (so it will automatically inject the OutputModelSettings into your output model's "Settings" property.

    Set up a setter injection policy in your StructureMap initialization code (a Registry, Global ASAX, your Bootstrapper, etc -- wherever you set up your container).

    x.SetAllProperties(s => s.Matching(p => p.Name.EndsWith("Settings")));
    
  2. Create your behavior to call StructureMap's "BuildUp()" on the output model to trigger the setter injection. The behavior will be an open type (i.e. on the end) so that it can support any kind of output model

    public class OutputModelSettingBehavior<TOutputModel> : BasicBehavior
        where TOutputModel : class
    {
        private readonly IFubuRequest _request;
        private readonly IContainer _container;
    
        public OutputModelSettingBehavior(IFubuRequest request, IContainer container)
            : base(PartialBehavior.Executes)
        {
            _request = request;
            _container = container;
        }
    
        protected override DoNext performInvoke()
        {
            BindSettingsProperties();
    
            return DoNext.Continue;
        }
    
        public void BindSettingsProperties()
        {
            var viewModel = _request.Find<TOutputModel>().First();
            _container.BuildUp(viewModel);
        }
    }
    
  3. Create a convention to wire up the behavior

    public class OutputModelSettingBehaviorConfiguration : IConfigurationAction
    {
        public void Configure(BehaviorGraph graph)
        {
            graph.Actions()
                .Where(x => x.HasOutput &&
                            x.OutputType().GetProperties()
                                .Any(p => p.Name.EndsWith("Settings")))
                .Each(x => x.AddAfter(new Wrapper(
                    typeof (OutputModelSettingBehavior<>)
                    .MakeGenericType(x.OutputType()))));
        }
    }
    
  4. Wire the convention into your FubuRegistry after the Routes section:

    ApplyConvention<OutputModelSettingBehaviorConfiguration>();
    
  5. In your view, use the new settings object:

    <%: Model.Settings.BestSimpsonsCharacter %>
    

NOTE: I have committed this as a working sample in the FubuMVC.HelloWorld project in the Fubu source. See this commit: https://github.com/DarthFubuMVC/fubumvc/commit/2e7ea30391eac0053300ec0f6f63136503b16cca

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文