简体   繁体   English

如何实例化CachedDataAnnotationsModelMetadata?

[英]How do I instantiate CachedDataAnnotationsModelMetadata?

I'm trying to test a custom model binder. 我正在尝试测试自定义模型活页夹。 As part of that process I am creating a new ModelBindingContext to pass into the BindModel method of the custom binder. 作为该过程的一部分,我将创建一个新的ModelBindingContext传递给自定义绑定程序的BindModel方法。 The problem is that I need to set the MetaData property of the ModelBindingContext as a CachedDataAnnotationsModelMetadata object, but honestly am not sure how to instantiate the object. 问题是我需要将ModelBindingContextMetaData属性设置为CachedDataAnnotationsModelMetadata对象,但是老实说,我不确定如何实例化该对象。

The signature for the CachedDataAnnotationsModelMetadata object is: CachedDataAnnotationsModelMetadata对象的签名为:

public CachedDataAnnotationsModelMetadata( CachedDataAnnotationsModelMetadata prototype, Func<object> modelAccessor )

Does anyone have an example of what the prototype and modelAccessor parameters are supposed to be? 有人有原型和modelAccessor参数应该是什么的示例吗?

Here's a snippet of the non-functional code. 这是非功能代码的片段。

        // Assemble
        var formCollection = new List<KeyValuePair<string, string>> {
            new KeyValuePair<string, string> ("SomeRequiredProperty", "SomeValue")
        };

        var valueProvider = new System.Web.Http.ValueProviders.Providers.NameValuePairsValueProvider(formCollection, null);

        var metadata = new System.Web.Http.Metadata.Providers.CachedDataAnnotationsModelMetadata(????, ????)

        var bindingContext = new ModelBindingContext {
            ModelName = "ClaimsModelBinderInputModel",
            ValueProvider = valueProvider,
            ModelMetadata = metadata
        };
        var actionContext = new HttpActionContext();

        var httpControllerContext = new HttpControllerContext();
        httpControllerContext.Request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/someUri");
        actionContext.ControllerContext = httpControllerContext;

        var cmb = new ClaimsModelBinder();

        // Act
        cmb.BindModel(actionContext, bindingContext);
        // Assert...

I've found a few examples around the net and SO of people interacting with this class, but no concrete example of implementing it. 我发现了一些与此类交互的人的网络和SO的示例,但没有实现它的具体示例。

Thanks in advance! 提前致谢!

Update 更新资料

Figured out ModelAccessor , it's just used to delay accessing the actual model until the model property is accessed. 找出ModelAccessor ,它仅用于延迟访问实际模型,直到访问model属性为止。 https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Metadata/ModelMetadata.cs#L103 https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/System.Web.Http/Metadata/ModelMetadata.cs#L103

I'm still working on supplying a prototype object if anyone can help with that. 如果有人可以提供帮助,我仍在提供原型对象。

So I ended up sidestepping this issue entirely. 因此,我最终完全回避了这个问题。

After inspecting the inheritance model, I realized that in this case I can get away with using the DataAnnotationsModelMetadataProvider rather than the CachedDataAnnotationsModelMetadata . 在检查继承模型之后,我意识到在这种情况下,可以使用DataAnnotationsModelMetadataProvider而不是CachedDataAnnotationsModelMetadata The latter of these doesn't require the parameters necessary to setup caching, and since I don't need caching to test what I am testing, the standard DataAnnotationsModelMetadataProvider will suffice. 这些中的后一个不需要设置缓存所需的参数,并且由于我不需要缓存来测试要测试的内容,因此标准的DataAnnotationsModelMetadataProvider就足够了。

I was able to identify that I could take a different approach after reading through the source code for the metadata namespace. 阅读完元数据名称空间的源代码后,我能够确定可以采用其他方法。

https://github.com/ASP-NET-MVC/aspnetwebstack/tree/master/src/System.Web.Http/Metadata https://github.com/ASP-NET-MVC/aspnetwebstack/tree/master/src/System.Web.Http/Metadata

That said, if anyone else can provide some clues to my original question, I would prefer to use the cached model object if possible. 就是说,如果其他人可以提供一些有关我的原始问题的线索,我将尽可能使用缓存的模型对象。

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

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