code_junkie

generic type dependency injection: How to inject T

Nov 12th, 2011
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. public interface IDocHandler<T>where T: class
  2. {
  3.  
  4. T Document { get;set;}
  5.  
  6. void Load(T doc);
  7.  
  8. void Load(string PathToDoc);
  9.  
  10. void Execute();
  11.  
  12. void Execute(T doc);
  13.  
  14. }
  15.  
  16. public class FinanceDocumentProcessor:IDocumentHandler<ReportDocument>
  17. {}
  18.  
  19.  
  20. public class MarketingDocumentProcessor:IDocumentHandler<MediaDocument>
  21. {}
  22.  
  23. IDocumentHandler<ReportDocument> docProc= new FinanceDocumentProcessor();
  24.  
  25. IDocumentHandler<ReportDocument> docProc = container.resolve("FinanceDocumentProcessor());
  26.  
  27. ChrystalReportHandler:IReportHandler<CrystalReportDocument>
  28.  
  29. IReportHandler<T (this needs also to be injected)> = IOContainer.Resolve(MyMappedType here)
  30.  
  31. public interface IDocumentHandler
  32. {
  33. IDocument Document { get; set; }
  34.  
  35. void Load(IDocument doc);
  36.  
  37. void Load(string PathToDoc);
  38.  
  39. void Execute();
  40.  
  41. void Execute(IDocument doc);
  42.  
  43. }
  44.  
  45. public class IDocument { }
  46. public class ReportDocument : IDocument { }
  47. public class MediaDocument : IDocument { }
  48. public class FinanceDocumentProcessor : IDocumentHandler { }
  49. public class MarketingDocumentProcessor : IDocumentHandler { }
  50.  
  51. public interface IDocumentHandler { }
  52. public interface IDocumentHandler<T> : IDocumentHandler { }
Add Comment
Please, Sign In to add comment