Dependency Injection to CustomFormatter

百般思念 提交于 2019-12-11 05:56:13

问题


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

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

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

MyCustomFormatter Code:

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

My SamplePdf class Code:

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

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?


回答1:


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

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

Assuming

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

Reference Media Formatters in ASP.NET Web API 2



来源:https://stackoverflow.com/questions/43512810/dependency-injection-to-customformatter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!