导入的MEF问题

时间:2010-12-17 12:28:22

标签: wpf dependency-injection inversion-of-control mef

来自shell-view-model的模型与抽象工厂模式。我需要从外部程序集中注入视图模型类。如果我在创建视图模型上使用抽象工厂模式。问题是视图模型中的导入类是null。

Shell-view-models看起来像这样:

public interface IViewModelFactory
    {
        ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel);
        IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel);
    }

    [Export(typeof(IViewModelFactory))]
    public class DefaulFactoryViewModel:IViewModelFactory
    {
        #region Implementation of IViewModelFactory

        public ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel)
        {
            return  new LogOnViewModel(shellViewModel);
        }

        public IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel)
        {
            return new MessengerViewModel(shellViewModel);
        }

        #endregion
    }


    public interface IShellViewModel
    {
        void ShowLogOnView();
        void ShowMessengerView();
    }

    [Export(typeof(IShellViewModel))]
    public class ShellViewModel : Conductor<IScreen>, IShellViewModel
    {

        private readonly IViewModelFactory _factory;

        [ImportingConstructor]
        public ShellViewModel(IViewModelFactory factory)
        {
            _factory = factory;
            ShowLogOnView();
        }

        public void ShowLogOnView()
        {
            var model = _factory.CreateLogOnViewModel(this);
            // var model = IoC.Get<LogOnViewModel>();
            ActivateItem(model);
        }

        public void ShowMessengerView()
        {
            var model = _factory.CreateMessengerViewModel(this);
            ActivateItem(model);
        }
    }

一些视图模型。:

public class LogOnViewModel : Screen,ILogOnViewModel
{
    [Import]//inject class from external assembly
    private IPokecConnection _pokecConn;

    private readonly IShellViewModel _shellViewModel=null;

    private User _user=null;

    public LogOnViewModel(IShellViewModel shellViewModel)
    {
        _shellViewModel = shellViewModel;
        _user = new User();
    }
}

变量_pokecConn为null因为我在创建新的视图模型时使用抽象工厂。

如果我在shell-view模型中使用:

var model = IoC.Get<LogOnViewModel>();
而不是这个:

var model = _factory.CreateLogOnViewModel(this);

并在视图模型类上添加了Export属性,但是我想使用抽象工厂,并且只在视图模型中注入来自extrenal程序集的类。 它存在这个问题的解决方案,或者我必须从IoC创建视图模型并导出所有类?坦白推进。

已编辑:

MEF BOOTSTRAPER CLASS:

 public class MefBootStrapper : Bootstrapper<IShellViewModel>
    {
    #region Fields
    private CompositionContainer _container;
    #endregion

    #region Overrides
    protected override void Configure()
    { //  configure container
    #if SILVERLIGHT
        _container = CompositionHost.Initialize(
        new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
    #else

        var catalog =
            new AggregateCatalog(
                AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());

        //add external DLL
        catalog.Catalogs.Add(
            new AssemblyCatalog(string.Format(
                CultureInfo.InvariantCulture, "{0}{1}", System.IO.Directory.GetCurrentDirectory(), @"\Pokec_Toolkit.dll")));

        _container = new CompositionContainer(catalog);
    #endif

        var batch = new CompositionBatch();

        batch.AddExportedValue<IWindowManager>(new WindowManager());
        batch.AddExportedValue<IEventAggregator>(new EventAggregator());
        batch.AddExportedValue(_container);

        _container.Compose(batch);
        _container.SatisfyImportsOnce(this);  
    }

    protected override object GetInstance(Type serviceType, string key)
    {
        string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
        var exports = _container.GetExportedValues<object>(contract);

        if (exports.Count() > 0)
        return exports.First();

        throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
    }

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {
        return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
    }

    protected override void BuildUp(object instance)
    {
        _container.SatisfyImportsOnce(instance);
    }
    #endregion
    }

1 个答案:

答案 0 :(得分:0)

您是否忘记了LogOnViewModel构造函数的ImportingConstructor属性?

编辑:Import property always null (MEF import issue)