Generating new instance

Feb 12, 2014 at 4:31 PM
Edited Feb 12, 2014 at 4:32 PM
I am trying to build up an array of objects, but when I use container.resolve<>(), it always uses the same instance, so I end up getting an array where the last object added is the same reference as all the other elements.
            ITCardGACCVM gacc1 = basecontainer.Resolve<ITCardGACCVM>();
            gacc1.GaccName = "NameOne";

           ITCardGACCVM gacc2 = basecontainer.Resolve<ITCardGACCVM>();
            gacc2 .GaccName = "NameTwo";
            GaccList.Add(gacc2 );

 //Both elements in GaccList are the same reference, and have the name "NameTwo"
How can i use prism to generate a NEW COPY of a class? Thanks!
Feb 12, 2014 at 6:08 PM
Hi Mathius,

You need to be careful on which BootStrapper are you using as the resolving default behaviour may change. For example, the default behavior of Unity returns a new instance each time, while MEF returns the same instance that is registered as Singleton by default.

Having said that, I imagine that you would be using MEF BootStrapper. In that case, you should set a PartCreationPolicy attribute as "NonShared" above the object class declaration in order to resolve a new instance each time from the container.

You may find useful the following links related to PartCreationPolicy attribute:

I hope this helps,

Gabriel Ostrowsky
Feb 12, 2014 at 7:15 PM
Weird. I am actually using the Unity boot strapper. Why would it be doing that then by default and how would I change it?
Feb 13, 2014 at 12:21 PM
Hi mathius,
  1. I wasn't really expecting that you were using Unity. Singleton type registration in Unity is achieved by explicitly setting a parameter of new ContainerControlledLifetimeManager() to the RegisterType() method. Are you registering the types without any parameter? In the following site you may find more detailed information regarding how object's lifetime works with Unity:

If you might have a sample that you could share with this behavior you described I would be glad to try solving the issue and report you back with the results.

Gabriel Ostrowsky
Feb 13, 2014 at 3:14 PM
Edited Feb 13, 2014 at 3:23 PM
All of my types are registered as follows
 this.container.RegisterType<IFAAVM, FAAVM>(new ContainerControlledLifetimeManager());
So that explains why it was happening that way. Thank you very much! I was just mimicking the tutorials while learning, so that got me into trouble a little.