Dependency injection using MEF C# with Example



Dependency injection using MEF C# with Example

public interface ILogger 
{ 
void Log(string message); 
} 
[Export(typeof(ILogger))] 
 

[ExportMetadata("Name", "Console")] 
public class ConsoleLogger:ILogger 
{ 
public void Log(string message) 
{ 
Console.WriteLine(message); 
} 
} 
[Export(typeof(ILogger))] 
[ExportMetadata("Name", "File")] 
public class FileLogger:ILogger 
{ 
public void Log(string message) 
{ 
//Write the message to file 
} 
} 
public class User 
{ 
private readonly ILogger logger; 
public User(ILogger logger) 
{ 
this.logger = logger; 
} 
public void LogUser(string message) 
{ 
logger.Log(message) ; 
} 
} 
public interface ILoggerMetaData 
{ 
string Name { get; } 
} 
internal class Program 
{ 
private CompositionContainer _container; 
[ImportMany] 
private IEnumerable> _loggers; 
private static void Main() 
{ 
ComposeLoggers(); 
Lazy loggerNameAndLoggerMapping = _ loggers.First((n) => 
((n.Metadata.Name.ToUpper() =="Console")); 
ILogger logger= loggerNameAndLoggerMapping.Value 
var user = new User(logger); 
user.LogUser("user name"); 
} 
private void ComposeLoggers() 
{ 
//An aggregate catalog that combines multiple catalogs 
var catalog = new AggregateCatalog(); 
string loggersDllDirectory =Path.Combine(Utilities.GetApplicationDirectory(), "Loggers"); 
if (!Directory.Exists(loggersDllDirectory )) 
{ 
Directory.CreateDirectory(loggersDllDirectory ); 
 

} 
//Adds all the parts found in the same assembly as the PluginManager class 
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); 
catalog.Catalogs.Add(new DirectoryCatalog(loggersDllDirectory )); 
//Create the CompositionContainer with the parts in the catalog 
_container = new CompositionContainer(catalog); 
//Fill the imports of this object 
try 
{ 
this._container.ComposeParts(this); 
} 
catch (CompositionException compositionException) 
{ 
throw new CompositionException(compositionException.Message); 
} 
} 
} 
 

Partial classes provides us an option to split classes into multiple parts and in multiple source files. All parts are 
combined into one single class during compile time. All parts should contain the keyword partial,should be of the 
same accessibility. All parts should be present in the same assembly for it to be included during compile time. 

0 Comment's

Comment Form