简体   繁体   English

强类型的ASP.NET MVC ViewData扩展 - 放置标识符的位置?

[英]Strongly typed ASP.NET MVC ViewData extensions - where to put identifiers?

Sometimes the overhead of creating a new model type is annoying. 有时,创建新模型类型的开销很烦人。 In these cases, I want to set ViewData, and the options seem to be: 在这些情况下,我想设置ViewData,选项似乎是:

  1. Simply set viewdata with a string key, and cast it out. 只需使用字符串键设置viewdata,然后将其抛出即可。 This has obvious problems. 这有明显的问题。

  2. Store the identifier (string key) somewhere (where? on the controller?) and cast it out on the viewpage. 将标识符(字符串键)存储在某处(控制器上的?)并将其强制转换为视图页面。 While solving one of the problems, I'm still casting, which is problematic, and kills type inference. 在解决其中一个问题时,我仍在施法,这是有问题的,并且会杀死类型推断。

  3. Use the MVCContrib strongly typed Set/Get functions. 使用MVCContrib强类型的Set / Get函数。 These are nice sometimes, but if the type isn't very descriptive, say a boolean "IsNew", they don't work too well. 有时这些很好,但如果类型不是很具描述性,比如布尔“IsNew”,它们就不能很好地工作。 It also kills type inference and it's still pretty much just doing a cast, so I must manually sync up the view and controller. 它也会杀死类型推断,它仍然只是在进行转换,所以我必须手动同步视图和控制器。

So, instead I'm thinking of using a phantom type to combine both the type and key identifier. 所以,我正在考虑使用幻像类型来组合类型和密钥标识符。 It'd be equivalent to this psuedo-C#: 它等同于这个psuedo-C#:

class ViewDataKey<T> = string

This would let me create identifiers in the controller like this: 这将让我在控制器中创建标识符,如下所示:

public static readonly ViewDataKey<bool> IsNew = "IsNew";

With some simple extension methods, setting the data would be: 使用一些简单的扩展方法,设置数据将是:

  ViewData.Set(IsNew, true);

Getting it out is just as easy: 把它拿出来也很简单:

var isNew = ViewData.Get(FrobNozzleController.IsNew);

Notice we don't need to specify the type of isNew, it's safely inferred. 请注意,我们不需要指定isNew的类型,它是安全推断的。 The key and type definitons are stored in one location. 密钥和类型定义存储在一个位置。

Questions: 问题:

  1. Where should the key be stored? 密钥应存放在哪里? For "global-ish" ViewData, some common class works fine. 对于“global-ish”ViewData,一些常见的类可以正常工作。 But for View/Controller specific data? 但对于View / Controller特定数据? Is there any downside to putting it on the controller? 将它放在控制器上是否有任何缺点? (Apart from the long name of the controller?) (除了控制器的长名称?)

  2. Are there any easier ways, or things already built-in? 有没有更简单的方法,或已经内置的东西?

I think this is what the model does in the MVC pattern. 我认为这就是模型在MVC模式中的作用。 Why you don't use a typed view instead? 为什么不使用打字视图呢?

I believe you just make things more complicated by introducing yet another dependency between the controller and the view. 我相信你只是通过在控制器和视图之间引入另一个依赖项来使事情变得更复杂。 I don't see any specific benefit in terms of line of code or something like that. 我没有看到代码行或类似的任何特定好处。 You have to declare the existence of the variable, which you'd also do in the model class definition. 您必须声明变量的存在,您还要在模型类定义中执行该变量。

You don't get any significant advantage by doing so. 这样做不会带来任何重大优势。

One idea would be to create tuples for a strongly typed view in cases where you don't want to create a specific view model class: 在您不想创建特定视图模型类的情况下,一种想法是为强类型视图创建元组:

class SimpleModel<T>

class SimpleModel<T, U>

class SimpleModel<T, U, V>

Then make a strongly typed view, as usual. 然后像往常一样制作强类型视图。 Now you have the advantages of strong typing without having to make a new class for every case. 现在您拥有强大的打字优势,而无需为每个案例创建一个新类。

.NET 4.0 has Tuple, etc., built in. .NET 4.0内置了Tuple等。

I'd still use the ViewData dictionary, and instead of "casting it out" I'd just use ToString() and Html.Encode it. 我仍然使用ViewData字典,而不是“把它扔掉”我只是使用ToString()和Html.Encode它。 Why break the MVC pattern? 为什么打破MVC模式?

I just put up a post covering this topic. 我刚刚张贴了一个涵盖这个主题的帖子。

http://agilefutures.com/index.php/2009/06/mvc-strongly-typed-global-viewdata http://agilefutures.com/index.php/2009/06/mvc-strongly-typed-global-viewdata

Hope it's of use. 希望它有用。

Regards 问候

Tobi 托比

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM