Skip to content

StructureMap

This registration will return different instances when calling ObjectFactory.Container.GetInstance multiple times:

ObjectFactory.Initialize(initializer => initializer.For<IMyInterface>().Use<MyClass>());

IMyInterface firstObject = (IMyInterface) ObjectFactory.Container.GetInstance<IMyInterface>();
IMyInterface secondObject = (IMyInterface) ObjectFactory.Container.GetInstance<IMyInterface>();

if(firstObject.Equals(secondObject))
    throw new Exception("The instances should NOT be the same");

 

This registration will return the same instance when calling ObjectFactory.Container.GetInstance multiple times:

ObjectFactory.Initialize(initializer => initializer.For<IMyInterface>().Singleton().Use<MyClass>());

IMyInterface firstObject = (IMyInterface) ObjectFactory.Container.GetInstance<IMyInterface>();
IMyInterface secondObject = (IMyInterface) ObjectFactory.Container.GetInstance<IMyInterface>();

if(!firstObject.Equals(secondObject))
    throw new Exception("The instances should be the same.");

 

To clear earlier registrations:

ObjectFactory.Initialize(initialization => { });

 

Advertisements
Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: