How best way to use ChildContainer in different aplication

Feb 10, 2011 at 5:50 AM

Hi

We have different application like App1, App2 .. app...N.  Each can work as standalone(window appliation).   In case of standalond we were using application specfic container  The same way App1 can call App2 and App2 to app3. What I did was I have created Root application(Standalone) whis has one Default container with region( example AppRegion). From Root Application i am calling App1 through service class and passing root application container.   The App1 service class i created the childContainer from parent(root applicatio container) and added App1 applcation controls into "AppRegion".  From App1 i am calling App2 and pass App1 container.parent.  But still i am able access App1 childcontainer region into App2 container. Even if i dispose child container of App1, still i can access in App2.

 

How best way to use this type of scenario.

 

Gopal

Feb 11, 2011 at 3:22 AM

I don't understand what you're saying. Are you saying you've got a composite app with multiple modules, or you have separate complete applications? If it's the latter, then you're not going to get this to work, as containers will not cross application boundaries.

 

I really can't follow what you're doing otherwise, and don't understand why you want to pass containers around. In any event, if you're accessing containers from many different places you're using it wrong.

Feb 11, 2011 at 5:01 AM

Hi

This is the kind of navigation application. Navigation happen between the module. Each module has own container since module can work as standalone. We don’t have any issue, if we using as standalone.

Please suggest me what is the best way to use in case of navigation application.

The picture below will explain more details -- SA-àstandalone

In case of Navigation

Step 1

Back

Next

App1 -

Press Button To show App2

Navigation Region

Application Region


Step 2

Next

Back

App2 -

Press Button To show App3


Step 3

Back

App3 -


Regards

Gopal

From: ctavares [email removed]
Sent: Friday, February 11, 2011 9:53 AM
To: Gopalakrishnan T
Subject: Re: How best way to use ChildContainer in different aplication [unity:245393]

From: ctavares

I don't understand what you're saying. Are you saying you've got a composite app with multiple modules, or you have separate complete applications? If it's the latter, then you're not going to get this to work, as containers will not cross application boundaries.

I really can't follow what you're doing otherwise, and don't understand why you want to pass containers around. In any event, if you're accessing containers from many different places you're using it wrong.

Feb 11, 2011 at 10:07 AM
gop_sur wrote:

The App1 service class i created the childContainer from parent(root applicatio container) and added App1 applcation controls into "AppRegion".  From App1 i am calling App2 and pass App1 container.parent.  But still i am able access App1 childcontainer region into App2 container. Even if i dispose child container of App1, still i can access in App2.

Is something like the following what you are looking for (see unit test at very end)?

using System;
using System.Diagnostics;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Practices.ObjectBuilder2;
using Microsoft.Practices.Unity.ObjectBuilder;
using Microsoft.Practices.Unity.Tests.TestObjects;
using Microsoft.Practices.Unity.TestSupport;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Microsoft.Practices.Unity.Tests
{
    [TestClass]
    public class UnityContainerFixture
    {
        public class AppBase : IDisposable
        {
            protected IApplicationController AppController;
            public AppBase OtherApp { get; set; }
            public IUnityContainer Container { get; set; }

            public AppBase(IUnityContainer container)
            {
                Container = container;
                AppController = container.Resolve<IApplicationController>();
            }
            public virtual void Dispose()
            {
                Debug.WriteLine(string.Format("Disposing {0}", GetType().Name));
                OtherApp = null;
                Container = null;
            }
        }
        public class App2 : AppBase
        {
            public App2(IUnityContainer container) : base(container) { }
            public void SetApp1()
            {
                OtherApp = AppController.GetApp("App1");                               
            }
        }
        public class App1 : AppBase
        {
            public App1(IUnityContainer container) : base(container) { }
            public void SetApp2()
            {
                OtherApp = AppController.GetApp("App2");
            }
        }
        public interface IApplicationController
        {
            T CreateApp<T>() where T : AppBase;
            AppBase GetApp(string appName);
            void RemoveApp(AppBase appObj);
        }
        public class ApplicationController : IApplicationController
        {
            private IUnityContainer _container;
            private List<AppBase> appList = new List<AppBase>();

            public ApplicationController(IUnityContainer container)
            {
                _container = container;
            }
            public AppBase this [int index]
            {
                get { return appList[index]; }
                set { appList[index] = value; }
            }
            public AppBase GetApp(string appName)
            {
                return appList.FirstOrDefault(a => a.GetType().Name == appName);
            }
            public T CreateApp<T>() where T : AppBase
            {
                var childContainer = _container.CreateChildContainer();
                var newApp = childContainer.Resolve<T>();
                appList.Add(newApp);
                return newApp;
            }
            public void RemoveApp(AppBase appObj)
            {
                var app = appList.FirstOrDefault(a => a==appObj);
                var container = app.Container;
                container.Dispose();
                appObj.Dispose();
            }
        }

        [TestMethod]
        public void CanHandleChildContainers()
        {
            var container = new UnityContainer();
            container.RegisterType<IApplicationController, ApplicationController>
                    (new ContainerControlledLifetimeManager());

            var appCtlr = container.Resolve<IApplicationController>();
            var app1 = appCtlr.CreateApp<App1>();
            var app2 = appCtlr.CreateApp<App2>();

            app1.SetApp2();
            app2.SetApp1();

            Assert.AreEqual(app1.OtherApp, app2);
            Assert.AreEqual(app2.OtherApp, app1);

            appCtlr.RemoveApp(app1);
            appCtlr.RemoveApp(app2);
        }

Feb 11, 2011 at 1:01 PM

Hi Billkrat

Thanks for your reply. This is what i was expected. I also tried to using childcontainer. So i think, i am on the right track. :-)

Regards

Gopal