对CustomFormatter的依赖注入

时间:2017-04-20 07:22:46

标签: c# asp.net-web-api dependency-injection ninject

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

我有自定义mediatype格式化程序,用于在用户请求' application / pdf'

时返回pdf

这里我需要在创建pdf后更新数据。所以在这里我需要调用我的业务类来更新数据。

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配置中映射了相同的内容,如下所示;

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

如何将此注入我的班级?

1 个答案:

答案 0 :(得分:2)

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

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

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