是否可以使用 MEF 编写 WCF RIA 域服务?
假设我在域服务中有以下属性:
[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public TestClass Dependency{ get; set; }
和TestClass..
[Export]
public class TestClass
{
public void Do()
{
}
}
我有一个 DomainServiceFactory 类:
public class DomainServiceFactory : IDomainServiceFactory
{
private readonly ComposablePartCatalog _catalog;
private readonly CompositionContainer _container;
public DomainServiceFactory()
{
_catalog = new TypeCatalog(typeof(DomainService1), typeof(DomainService2));
_container = new CompositionContainer(_catalog);
//What should I call here? _container.SatisfyImportsOnce(this);
}
#region Implementation of IDomainServiceFactory
public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
{
DomainService domainService;
var export = _container.GetExports(domainServiceType, null, null).FirstOrDefault();
if(export != null)
{
domainService = (DomainService) export.Value;
}
else
{
domainService = (DomainService)Activator.CreateInstance(domainServiceType);
}
domainService.Initialize(context);
return domainService;
}
public void ReleaseDomainService(DomainService domainService)
{
domainService.Dispose();
}
#endregion
}
SatisfyImportsOnce 是构建容器的方法吗?输入参数是一个ComposablePart,我应该传入什么?
问候。