Unity:解决 RIA DomainContext 的问题

发布于 2024-10-15 19:20:03 字数 3130 浏览 3 评论 0原文

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

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

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>());

        }

视图将视图模型作为依赖项:

 public NavigationView(NavigationViewModel viewModel)
        {
            InitializeComponent();

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

ViewModel 具有以下内容:

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

一旦我注释掉这个 ctor 并放置一个空 ctor,一切都很好,由于某种原因我无法解析 SCMDomainContext 类。这是您添加的一个,以便由 Ria 服务为您创建域上下文。

由于我使用的是 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)

非常感谢您对此的帮助, 卡韦

I am using PRISM 4 and got my head around almost all features, however as soon as I would like to inject my DomainContext class (RIA) into my view model, the hell breaks loose. :) It would be great if an experienced Unity/Prism developer could give me an advice how to proceed.

Within my bootstrapper, I am registering the required class in Unity Container like this:

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

Within the NavigationModule, I have the following in the ctor to register the NavigationView with a particular region.

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

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

        }

The View takes the View Model as dependency:

 public NavigationView(NavigationViewModel viewModel)
        {
            InitializeComponent();

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

The ViewModel has the following:

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

As soon as I comment this ctor out and put a en empty ctor, it is all fine, for some reason I can't resolve the SCMDomainContext class. Which is the one you add to have the Domain Context created for you provided by Ria Services.

Since I am using Silverlight, I can't see the stack trace to follow the exception, all I get is this message on a page. What am I missing please?

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)

Your help on this is highly appreciated,
Kave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光是把杀猪刀 2024-10-22 19:20:03

我看不出有什么问题。但话虽如此,我按以下方式使用接口中的 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 类型是否会产生影响。就我个人而言,我更喜欢控制容器解析类型的方式。

I can't see much wrong here. But having said that, I'm using the Initialize method from the interface in the following way to register types and views for regions:

 #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>());
}

Not sure whether it makes a difference if you don't explicitly register the ViewModel and the View type. Personally I prefer to have control over the way how a type gets resolved by the container.

帅的被狗咬 2024-10-22 19:20:03

事实上,最好像这样为 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
    }

In fact its best to create a layer for the DomainContext like this. Then its easily resolvable by an 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
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文