Using the same Unity Container in whole Multi-Tiers application

Dec 22, 2010 at 3:56 PM

I'm developping a Prism application with 2 modules yet.

May i use the same Unity Container For all the application??

I've a module ("CVModule") that add a service("DataCtx" witch implement the IDataContext Interface).

I want to use the service In an Other class("MainViewsMV") of the same assembly (CVModule).

The Bootstrapper is in a other project(assembly) named "X23P".

But when i try to resolve the sevice, it tell me that the parameter "container) is Null= dosn't contain the Container Reference.

Here Is Codes ( i think that will be helpfull)

using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using CVModule.Services;
using Microsoft.Practices.Unity;

namespace CVModule
{
    public class CVModulee : IModule
    {

         private readonly IRegionViewRegistry regionViewRegistry;

         private readonly IUnityContainer cont;

         public CVModulee(IRegionViewRegistry registry, IUnityContainer container)
        {
            this.regionViewRegistry = registry;
            this.cont = container;
        }

        public void Initialize()
        {
            regionViewRegistry.RegisterViewWithRegion("DownRegion", typeof(CVModule.DownViews.CVMDownView));
            regionViewRegistry.RegisterViewWithRegion("MainRegion", typeof(CVModule.MainViews.CVMMainView));

            
            this.cont.RegisterType<IDataContext, DataCtx>(new ContainerControlledLifetimeManager());
           
        }


    }
}

 

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data;
using System.Globalization;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using CVModule.Services;

namespace CVModule
{
    class MainViewsMV
    {
        IUnityContainer cont;

        public MainViewsMV(IUnityContainer container)
        {
            this.cont = container;

        }

        public MainViewsMV()
        {
            // TODO: Complete member initialization
        }

        internal IEnumerable cv()
        {
            
            IDataContext ctx = cont.Resolve<IDataContext>();

            IEnumerable stk = ctx.GetCV();

             return (stk);

            //throw new NotImplementedException();
        }






    }
}

Developer
Dec 22, 2010 at 5:21 PM

Hi,

As explained on this chapter from the Prism MSDN documentation, you can use the Unity container throughout your application, even within different modules. In order to do so, you need to have your Bootstrapper register the container in the container itself, so that you can retrieve it through, for example, constructor injection. The exception you're experiencing might be because you're not resolving your MainViewsMV class appropriately, or because your bootstrapper hasn't registered the container in itself. You can read about the bootstrapping process in this chapter from the documentation.

Additionally, you could use the Service Locator to resolve your dependencies. For example, the code for resolving a view would be like this:

SomeView view = ServiceLocator.Current.GetInstance<SomeView>();

If you continue experiencing this problem, it could be helpful if you could provide us with a repro sample so that we could help you troubleshoot your issue.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Dec 23, 2010 at 12:37 PM

Thank you guidomaliandi, the "SomeView view = ServiceLocator.Current.GetInstance<SomeView>();" works.

But by th way. I use an anonymous variable "var" to populate an ItemsSource of a datagrid from a database using linq to entity.

when i try to use a record of a line from the ObjectResult ( via "SelectionChanged" event) to the ItemsSource of an other datagrid, the cast to any other type of Collection (IENumerable for exemple) dosen't work.

The VS2010 tell me that is impossible to cast an anonymous thing to any other Type.

How to Do it without retrieving data for the DB once more??

and ifit is impossible, how to get the values of a line, witch has an anonymous type??

Thanks' for help!!


Developer
Dec 23, 2010 at 2:54 PM

Hi,

As explained on this article, anonymouse variables cannot be cast to anything that is not the Object type. Note that when you use the var keyword you could be using both an anonymous type or an implicitly typed variable. You can read more about the use of the latter in query expressions in this article.

As this is not related to Prism, you might find better support in the ADO.NET Entity Framework and LINQ to Entities forum. If you're using Silverlight and WCF RIA Services, you could also find support in the WCF RIA Services forum.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi