UnityContainer and Prism

Topics: Prism v2 - WPF 3.5
Dec 19, 2009 at 12:24 PM
Edited Dec 19, 2009 at 12:25 PM


  How can one ensure that a particular constructor of your class is called in Prism:  I have two constructors:

(I need to ensure my second constructor is invoked.)

 public ProductCollectionViewModel()


        public ProductCollectionViewModel(IInventoryDataService dataService,
                                          IEventAggregator eventAggregator,
                                          IRegionManager regionManager,
                                          IUnityContainer container)
            : base()

            this.dataService = dataService;
            this.regionManager = regionManager;
            this.container = container;
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<AppStatusMessageEvent>().Publish("Loading Categories...");
            this.SelectProductCommand = new DelegateCommand<Product>(this.SelectProduct);
            List<ProductCategory> prodCats = dataService.GetAllCategories();
            prodCats.Sort((x, y) => string.Compare(x.Name, y.Name));
            categories = new PopUpCollection<ProductCategory>();
            foreach (var pc in prodCats)

            selectedProduct = new Product();
            //SetValue(ShowMessageCommandProperty, new ShowMessageBoxCommand(this));
            //SetValue(ItemCommandProperty, new ItemSelectedCommand(this));

I need to declare two constructors because in my view I use an ObjectDataProvider thus:


        <ObjectDataProvider x:Key="Data" ObjectType="{x:Type vm:ProductCollectionViewModel}"/>
        <Style TargetType="{x:Type ComboBoxItem}">
            <!-- Sync the lists selected item with the ViewModel IsSelected-->
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
        <DataTemplate DataType="{x:Type model:ProductCategory}">
            <TextBlock Text="{Binding Path=Name}" />

and an ObjectDataProvider needs a default constructor. 


My question is how can I ensure that my second constructor is invoked?

My InventoryModule  which uses these is declared thusly:


public class InventoryModuleInit : IModule
        private readonly IUnityContainer _container;
        private readonly IRegionManager _regionManager;
        private Controller1 _controller;

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

        #region IModule Members

        public void Initialize()
            _regionManager.RegisterViewWithRegion("MainRegion", () => _container.Resolve<ProductWorkspaceView>());
            _regionManager.RegisterViewWithRegion("LeftRegion", GetCategorizedCollectionView);

        private object GetCategorizedCollectionView()
            //_container.RegisterType<IProductCategory, ProductCategory>();
            //IProductCategory productCategory = _container.Resolve<IProductCategory>();
            var categorizedProductView = _container.Resolve<ICategorizedProductCollectionView>();// new CategorizedProductCollectionView();
            return categorizedProductView;
        private void RegisterTypes()
            _container.RegisterType<IInventoryDataService, InventoryDataService>();
            _container.RegisterType<IProductWorkspaceView, ProductWorkspaceView>();
            _container.RegisterType<IProductWorkspaceViewModel, ProductWorkspaceViewModel>();
            _container.RegisterType<ICategorizedProductCollectionView, CategorizedProductCollectionView>();








Dec 22, 2009 at 8:40 PM

Hi Arun,

You can use the Injection constructor attribute to specify which constructor you want to Unity to use in case you have multiple constructors. The following extract from the Unity documentation explains how the constructor to be used is determined: “When a target class contains more than one constructor, Unity will use the one that has the InjectionConstructor attribute applied. If there is more than one constructor, and none carries the InjectionConstructor attribute, Unity will use the constructor with the most parameters. If there is more than one such constructor (more than one of the "longest" with the same number of parameters), Unity will raise an exception.”

If you would like more information about this topic, you can check this article:

Please let me know if this helps.

Damian Schenkelman