jeudi 13 août 2015

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. 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.

The signature for the CachedDataAnnotationsModelMetadata object is:

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

Does anyone have an example of what the prototype and modelAccessor parameters are supposed to be?

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.

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire