对 CustomFormatter 的依赖注入

Dependency Injection to CustomFormatter

我在 webapi 中工作,ninject 作为依赖注入器。我所有的构造函数注入都适用于我的控制器。

我有自定义媒体类型格式化程序,当用户请求 'application/pdf'

时,它用于 return pdf

这里我需要在创建pdf后更新数据。所以在这里我需要打电话给我的公司 class 来更新数据。

MyCustomFormatter 代码:

public class PdfMediaTypeFormatter : MediaTypeFormatter
{
    private readonly string mediaType = "application/pdf";
    Func<Type, bool> typeisIPdf = (type) => typeof(IPdf).IsAssignableFrom(type);
    Func<Type, bool> typeisIPdfCollection = (type) => typeof(IEnumerable<IPdf>).
    IsAssignableFrom(type);

    public PdfMediaTypeFormatter()
    {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
        MediaTypeMappings.Add(new UriPathExtensionMapping("pdf", new MediaTypeHeaderValue(mediaType)));
    }

    public override bool CanReadType(Type type)
    {
        return false;
    }

    public override bool CanWriteType(Type type)
    {
        return typeisIPdf(type) || typeisIPdfCollection(type);
    }

    public async override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
    {
        var pdfData= new SamplePdf();

        var memoryStream = report.Create(value);

        var bytes = memoryStream.ToArray();
        await writeStream.WriteAsync(bytes, 0, bytes.Length);
    }
}

我的SamplePdf class代码:

public class SamplePdf
{
    private readonly IBusinessLogic _logic;
    public MemoryStream Create(object model)
    {
       //Pdf generation code
       // here i need to call data update
       //_logic.update(model);
    }
}

我需要在我的业务 class 中注入 _logic 字段。我已经在 ninject 配置中映射了相同的配置,如下所示;

kernel.Bind<IBusinessLogic>().To<MyBusinessLogic>().InRequestScope();

如何将此注入我的 class?

根据提供的示例做出以下假设。

public class PdfMediaTypeFormatter : MediaTypeFormatter {
    private readonly string mediaType = "application/pdf";
    Func<Type, bool> typeisIPdf = (type) => typeof(IPdf).IsAssignableFrom(type);
    Func<Type, bool> typeisIPdfCollection = (type) => typeof(IEnumerable<IPdf>).
    IsAssignableFrom(type);
    private readonly IPdfFactory report;

    public PdfMediaTypeFormatter(IPdfFactory report) {
        SupportedMediaTypes.Add(new MediaTypeHeaderValue(mediaType));
        MediaTypeMappings.Add(new UriPathExtensionMapping("pdf", new MediaTypeHeaderValue(mediaType)));

        this.report = report;
    }

    public override bool CanReadType(Type type) {
        return false;
    }

    public override bool CanWriteType(Type type) {
        return typeisIPdf(type) || typeisIPdfCollection(type);
    }

    public async override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext) {

        var memoryStream = report.Create(value);

        var bytes = memoryStream.ToArray();

        await writeStream.WriteAsync(bytes, 0, bytes.Length);
    }
}

public interface IPdfFactory {
    MemoryStream Create(object model);
}

public class PdfFactory : IPdfFactory {
    private readonly IBusinessLogic _logic;

    public PdfFactory(IBusinessLogic logic) {
        this._logic = logic;
    }

    public MemoryStream Create(object model) {
        var stream = new MemoryStream();
        //...Pdf generation code

        //call data update
        _logic.update(model);

        return stream;
    }
}

注册会使用DI容器解析formatter

public static class WebApiConfig {

    public static void Register(HttpConfiguration config) {
        //...DI configuration

        var formatter = (PdfMediaTypeFormatter)config.DependencyResolver.GetService(typeof(PdfMediaTypeFormatter));

        config.Formatters.Add(formatter);
    }
}

假设

kernel.Bind<IBusinessLogic>().To<MyBusinessLogic>();
kernel.Bind<IPdfFactory>().To<PdfFactory>();
kernel.Bind<PdfMediaTypeFormatter>().ToSelf();

引用Media Formatters in ASP.NET Web API 2