Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.

Programming and Learning from SD

It took me too long to figure this out, so hopefully it will help you. StructureMap has way that will create a new instance of the object every time, instead of storing this in the container. I’m going to use this for the DBContext and for WCF Service references. Since the ObjectFactory is a static class, MVC will have these stored in memory without this.

Credit goes to Joshua Flanagan for answering my question.
public void GetConcreteInstanceOf_ShouldReturn_DifferentInstance()
    ObjectFactory.Initialize(registry =>
        // set it up so that it's new every time
        // use this for DBContext and service references
        registry.For<ISystemDataService>().Use(() => new SystemDataService());

    ISystemDataService result = Resolver.GetConcreteInstanceOf<ISystemDataService>();
    ISystemDataService result2 = Resolver.GetConcreteInstanceOf<ISystemDataService>();
    Assert.AreNotSame(result, result2);
Posted on Thursday, November 29, 2012 10:44 AM Unit Testing , Inversion Of Control (Ioc) , Entity Framework | Back to top

Comments on this post: Get a new instance with StructureMap

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Aligned | Powered by: