Ninject conditional binding based on property value

前端 未结 2 1295
栀梦
栀梦 2021-01-19 02:07

I am having trouble defining bindings using ninject.

I am in a standard ASP.NET WebForms application. I have defined an http handler to Inject dependencies in pages

2条回答
  •  忘掉有多难
    2021-01-19 02:23

    What I'm using (with Ninject 3 now) is a little different but it works for me. I create an array of dependencies and let them decide if they accept to handle the request or not. For example if I had this case

    public enum FileFormat
    {
        Pdf,
        Word,
        Excel,
        Text,
        Tex,
        Html
    }
    
    public interface IFileWriter
    {
        bool Supports(FileFormat format)
    
        ...
    }
    
    public class FileProcessor
    {
        FileProcessor(IFileWriter[] writers)
        {
            // build a dictionary with writers accepting different formats 
            // and choose them when needed
        }
    }
    
    public class MyModule : NinjectModule
    {
         public override void Load()
         {
             ...
    
             Bind().To();
             Bind().To();
             Bind().To();
             Bind().To();
             Bind().To();
         }
    }
    

    I hope that helps!

提交回复
热议问题