Created
January 20, 2012 20:26
-
-
Save PaulStovell/1649391 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel.Composition; | |
using System.ComponentModel.Composition.Hosting; | |
using System.Linq; | |
using System.Text; | |
namespace MefDemo | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var container = new CompositionContainer( | |
new AssemblyCatalog(typeof(Program).Assembly) | |
); | |
var receiver = container.GetExport<TaskListener>(); | |
receiver.Value.Accept(); | |
receiver.Value.Accept(); | |
receiver.Value.Accept(); | |
Console.ReadKey(); | |
} | |
} | |
[Export] | |
public class TaskListener | |
{ | |
private readonly ExportFactory<TaskProcessor> taskProcessorFactory; | |
[ImportingConstructor] | |
public TaskListener(ExportFactory<TaskProcessor> taskProcessorFactory) | |
{ | |
this.taskProcessorFactory = taskProcessorFactory; | |
} | |
public void Accept() | |
{ | |
using (var processor = taskProcessorFactory.CreateExport()) | |
{ | |
processor.Value.Process(); | |
} | |
} | |
} | |
[Export] | |
public class TaskProcessor | |
{ | |
private readonly DataContext dataContext; | |
[ImportingConstructor] | |
public TaskProcessor(DataContext dataContext) | |
{ | |
Console.WriteLine("Ceated"); | |
this.dataContext = dataContext; | |
} | |
public void Process() | |
{ | |
Console.WriteLine("Processing..."); | |
} | |
} | |
[Export] | |
public class DataContext : IDisposable | |
{ | |
public DataContext() | |
{ | |
Console.WriteLine("Data context created"); | |
} | |
public void Dispose() | |
{ | |
Console.WriteLine("Data context disposed"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment