依赖注入到CustomFormatter

依赖注入到CustomFormatter

本文介绍了依赖注入到CustomFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ninject作为依赖项注入器在webapi中工作.我所有的构造函数注入对于我的控制器都工作正常.

I am working in webapi with ninject as dependency injector. All my constructor injection working fine for my controllers.

我有一个自定义的媒体类型格式化程序,该格式程序用于在用户请求"application/pdf"时返回pdf

I have custom mediatype formatter which used to return pdf when user requested for 'application/pdf'

在这里,我需要在创建pdf后更新数据.因此,在这里我需要致电我的业务类以更新数据.

Here i need to update data after create pdf. So here i need to call my business class to update data.

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类代码:

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);
    }
}

我需要在业务类中插入_logic字段.我已经在ninject配置中映射了相同的内容,如下所示;

I need to inject _logic field with my business class. I already mapped the same in ninject configuration like below;

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

如何将其注入我的班级?

How to inject this to my class?

推荐答案

基于提供的示例进行了以下假设.

With the following assumptions made based on the provided example.

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容器解析格式程序

Registration would use the DI container to resolve 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();

参考 ASP.NET Web API 2中的媒体格式化程序

这篇关于依赖注入到CustomFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:22