[英]ASP.NET WebApi Model Binding with Dependency Injection
I have an web application that is written in ASP.NET MVC 5 with Razor views that works perfectly. 我有一个用ASP.NET MVC 5编写的Web应用程序,它具有完美的Razor视图。 I have a set of model classes that expects an
ISomething
in the constructor, and that ISomething
is injected there using Unity. 我有一组模型类,期望在构造函数中有一个
ISomething
,并使用Unity将ISomething
注入那里。 Everything works very nice. 一切都很好。
I have the model class like this: 我有这样的模型类:
public class SecurityRoleModel : PlainBaseModel
{
#region Constructor
/// <summary>
/// Initializes a new instance of the <see cref="SecurityRoleModel"/> class.
/// </summary>
/// <param name="encryptionLambdas">The encryption lambdas.</param>
public SecurityRoleModel(IEncryptionLambdas encryptionLambdas)
{
}
#endregion
}
In order to have the injection correctly working, I had to implement a custom DefaultModelBinder
that takes care of the model constructor injection like this: 为了使注入正常工作,我必须实现一个自定义的
DefaultModelBinder
,它可以像这样处理模型构造函数注入:
public class InjectableModelBinder : DefaultModelBinder
{
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
{
if (modelType == typeof(PlainBaseModel) || modelType.IsSubclassOf(typeof(PlainBaseModel)))
return DependencyResolver.Current.GetService(modelType);
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
Again, this is for the MVC part of the application, but now comes the ugly part: I have to implement a set of services (WebAPI) that deals with these models and I thought that I can do something similar to the MVC's DefaultModelBinder
in WebAPI but it seems that is not as easy as I thought. 同样,这是针对应用程序的MVC部分的,但现在是丑陋的部分:我必须实现一组处理这些模型的服务(WebAPI),我认为我可以做一些类似于WebAPI中MVC的
DefaultModelBinder
的操作但似乎并不像我想的那么容易。
Now comes my question - though I've read (I think) a lot of posts regarding the implementation of custom IModelBinder
(WebAPI), I cannot say that I found what I'm looking for; 现在是我的问题-尽管我已经阅读(我认为)很多有关自定义
IModelBinder
(WebAPI)实现的帖子,但是我不能说我找到了想要的东西。 what I want is to find a way to not re-invent the wheel (to be read as "write a IModelBinder
from scratch), I just want to have a place where the model class is instantiated and to have the possibility to put my code which gets the instance of the model class from the DI. 我想要的是找到一种方法,而不是重新发明轮子(读作“从头开始编写
IModelBinder
”),我只想拥有一个实例化模型类的地方,并有可能放置我的代码它从DI获取模型类的实例。
I hope I was clear enough. 我希望我足够清楚。 Thank you in advance.
先感谢您。
Evdin 伊夫丁
Though is not as broad as the MVC DefaultModelBinder
and it covers only the situation when the serializer / deserializer is JSON.NET, the solution that I found for my issue is the following: 尽管它不像MVC
DefaultModelBinder
那样广泛,并且仅涵盖了当序列化器/反序列化器为JSON.NET时的情况,但为我的问题找到的解决方案如下:
a) implement a custom version of CustomCreationConverter<T>
from Newtonsoft.Json.Converters
like this: a)从
Newtonsoft.Json.Converters
实现自定义版本的CustomCreationConverter<T>
,如下所示:
public class JsonPlainBaseModelCustomConverter<T> : CustomCreationConverter<T>
{
public override T Create(Type objectType)
{
return (T)DependencyResolver.Current.GetService(objectType);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
return base.ReadJson(reader, objectType, existingValue, serializer);
}
}
b) register the custom converter in the WebApiConfig
class, Register
method like this: b)在
WebApiConfig
类中Register
自定义转换器, Register
方法如下:
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new JsonPlainBaseModelCustomConverter<PlainBaseModel>());
Though it might not be the optimal situation, it covers perfectly my issue. 尽管这可能不是最佳情况,但它完全涵盖了我的问题。
If there is someone who knows a better solution, please let me know. 如果有人知道更好的解决方案,请告诉我。
Thank you! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.