1
我是MEF的新手。我想知道什麼時候通過組合來初始化實例,而不是調用它們的構造函數。例如,我不確定如何解決MEF中的以下問題。如果經理類是在另一個DLL因爲你沒有使用自動實現的屬性在計劃如何使用MEF初始化方法
class Program
{
static void Main(string[] args)
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
var manager= container.GetExportedValue<Manager>();
container.ComposeParts(manager);
var letter = manager.Letter;
Console.ReadKey();
}
}
//Manager Class is Business Access Layer referenced to Program as DLL
[Export]
public class Manager
{
private Letter _letter;
[Import]
public Letter Letter
{
get { return _letter ?? (_letter = InitializeComposeLetter()); }
}
private Letter InitializeComposeLetter()
{
var attachments = new List<string>();
var details = new StringBuilder();
var attachmentDirectory = ConfigurationManager.AppSettings["attachmentsDirectory"];
var letterPath = ConfigurationManager.AppSettings["letterPath"];
if (Directory.Exists(attachmentDirectory))
{
var files = Directory.EnumerateFiles(attachmentDirectory);
attachments = files.ToList();
}
if (File.Exists(letterPath))
{
var lines = File.ReadAllLines(letterPath);
foreach (var line in lines)
{
details.Append(line);
details.Append(Environment.NewLine);
}
}
//*********************QUESTION IS HERE *************//
var letter = new Letter() //SHOULD I MEF THIS OUT ??? IF SO HOW??
{
Subject = ConfigurationManager.AppSettings["defaultEmailTitle"],
Details = details.ToString(),
Attachments = attachments,
};
return letter;
}
}