MEF Tip: Initializing Objects

December 5, 2016

I like MEF. Maybe it's because of the default way MEF binds – using custom attributes; I like the declarative nature of attributes. Anyway, MEF exports declare their dependencies through the Import attribute. Here is a simple class export that has some imports:class AppManager {         ILogger _logger;         IFileService _fileService;     public AppManager() {        _logger.LogMessage("AppManager created");    }     public void DoWork() {        var filename = _fileService.GetFileForOpen();        if (filename == null)            return;         _logger.LogMessage("Opening file...");         var lines = File.ReadAllLines(filename);        _logger.LogMessage("Reading done.");         Console.WriteLine($"Read {lines.Length} lines of text.");    }} The AppManager class has two dependencies – an ILogger implementation and...
no comments