【问题标题】:Unity: Problem with resolving RIA DomainContextUnity:解决 RIA DomainContext 的问题
【发布时间】:2011-02-05 20:02:50
【问题描述】:

我正在使用 PRISM 4 并了解几乎所有功能,但是一旦我想将我的 DomainContext 类 (RIA) 注入到我的视图模型中,地狱就会崩溃。 :) 如果有经验的 Unity/Prism 开发人员能给我建议如何继续,那就太好了。

在我的引导程序中,我在 Unity Container 中注册所需的类,如下所示:

protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            Container.RegisterType<SCMDomainContext>();
        }

在 NavigationModule 中,我在 ctor 中有以下内容以将 NavigationView 注册到特定区域。

public NavigationModule(IUnityContainer container, IRegionManager regionManager)
        {
            _container = container;
            _regionManager = regionManager;

            _regionManager.RegisterViewWithRegion(Constants.NavRegion, () => _container.Resolve<NavigationView>());

        }

View 以 View Model 作为依赖:

 public NavigationView(NavigationViewModel viewModel)
        {
            InitializeComponent();

            Loaded += (s, e) =>
                          {
                              DataContext = viewModel;
                          };            
        }

ViewModel 具有以下内容:

public NavigationViewModel(SCMDomainContext context)
        {
            _context = context;
            ConstructCommon();
        }

只要我将此 ctor 注释掉并放入一个空的 ctor,一切都很好,由于某种原因,我无法解析 SCMDomainContext 类。哪个是您添加的域上下文,由 Ria Services 为您创建。

由于我使用的是 Silverlight,我看不到跟踪异常的堆栈跟踪,我得到的只是页面上的这条消息。请问我错过了什么?

Microsoft JScript runtime error: Unhandled Error in Silverlight Application An exception occurred while initializing module 'NavigationModule'. 
    - The exception message was: Activation error occured while trying to get instance of type NavigationModule, key ''
    Check the InnerException property of the exception for more information. If the exception occurred 
    while creating an object in a DI container, you can exception.GetRootException() to help locate the 
    root cause of the problem.    at Microsoft.Practices.Prism.Modularity.ModuleInitializer.HandleModuleInitializationError(ModuleInfo moduleInfo, String assemblyName, Exception exception)
   at Microsoft.Practices.Prism.Modularity.ModuleInitializer.Initialize(ModuleInfo moduleInfo)
   at Microsoft.Practices.Prism.Modularity.ModuleManager.LoadModulesThatAreReadyForLoad()
   at Microsoft.Practices.Prism.Modularity.ModuleManager.IModuleTypeLoader_LoadModuleCompleted(Object sender, LoadModuleCompletedEventArgs e)
   at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.RaiseLoadModuleCompleted(LoadModuleCompletedEventArgs e)
   at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.HandleModuleDownloaded(DownloadCompletedEventArgs e)
   at Microsoft.Practices.Prism.Modularity.XapModuleTypeLoader.IFileDownloader_DownloadCompleted(Object sender, DownloadCompletedEventArgs e)
   at Microsoft.Practices.Prism.Modularity.FileDownloader.WebClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)

非常感谢您对此提供的帮助, 卡夫

【问题讨论】:

    标签: silverlight dependency-injection unity-container prism wcf-ria-services


    【解决方案1】:

    我看不出有什么问题。但话虽如此,我还是通过以下方式使用接口中的 Initialize 方法为区域注册类型和视图:

     #region properties
     [Dependency]
     public IUnityContainer Container { get; set; }
    
     [Dependency]
     public IRegionManager RegionManager { get; set; }
     #endregion
    
    public virtual void Initialize()
    {
       this.Container.RegisterType<NavigationViewModel>(new ContainerControlledLifetimeManager());
       this.Container.RegisterType<NavigationView>(new ContainerControlledLifetimeManager());
    
       this.RegionManager.RegisterViewWithRegion(Constants.NavRegion, () => this.Container.Resolve<NavigationView>());
    }
    

    不确定如果您不显式注册 ViewModel 和 View 类型是否会有所不同。就我个人而言,我更喜欢控制容器解析类型的方式。

    【讨论】:

      【解决方案2】:

      事实上,最好像这样为 DomainContext 创建一个层。然后它很容易被 IoC 解决:

      public class ContactModuleService : IContactModuleService
          {
              readonly SCMDomainContext _context = new SCMDomainContext();
      
              #region Implementation of IContactModuleService
      
              public EntitySet<Contact> Contacts
              {
                  get { return _context.Contacts; }
              }
      
              public EntityQuery<Contact> GetContactsQuery()
              {
                  return _context.GetContactsQuery();
              }
      
              public SubmitOperation SubmitChanges(Action<SubmitOperation> callback, object userState)
              {
                  return _context.SubmitChanges(callback, userState);
              }
      
              public SubmitOperation SubmitChanges()
              {
                  return _context.SubmitChanges();
              }
      
              public LoadOperation<TEntity> Load<TEntity>(EntityQuery<TEntity> query, Action<LoadOperation<TEntity>> callback, object userState) where TEntity : Entity
              {
                  return _context.Load(query, callback, userState);
              }
      
              public LoadOperation<TEntity> Load<TEntity>(EntityQuery<TEntity> query) where TEntity : Entity
              {
                  return _context.Load(query);
              }
      
              #endregion
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 2020-08-23
        相关资源
        最近更新 更多