why calling container.resolve does initialization every time?

Jan 20, 2009 at 10:10 PM

I'm using unity container for several days and I found that by calling Resolve method,
the constructor methods shall invoked.
In other words. If I register some object into the container. By calling resolve it just creates new instance every time. Why?
Jan 20, 2009 at 11:13 PM
The default behavior for Unity is to create transient instances. In other words, you get a new object every time.

If you want the container to hold onto the first object created and return the same instance every time, you configure this by specifying a lifetime manager. For example, to get "singleton" behavior, use the container controlled lifetime manager:

container.RegisterType<IFoo, Foo>(new ContainerControlledLifetimeManager());

You can also do this in configuration.

Read the documentation about lifetime managers.