1

I'm convering from old structure map to a new one.. 2. something to 3.1.6.186...

I'm trying to define a default instance for IWebAccess as WinFormAccess...when run it I get this error:

SetUp : StructureMap.StructureMapConfigurationException : No default Instance is registered and cannot be automatically determined for type 'JCDCTools.Core.Utilities.Interfaces.IWebAccess'

There is no configuration specified for JCDCTools.Core.Utilities.Interfaces.IWebAccess

1.) Container.GetInstance(JCDCTools.Core.Utilities.Interfaces.IWebAccess)

at StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\SessionCache.cs: line 63 at StructureMap.Container.GetInstance(Type pluginType) in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\Container.cs: line 339 at StructureMap.Container.GetInstance() in c:\BuildAgent\work\a395dbde6b793293\src\StructureMap\Container.cs: line 202 at _Test_DAL.BaseTest.TestFixtureSetup() in BaseTest.cs: line 22

Here is my code

 public DefaultRegistry()
        {
            Scan(
                scan =>
                {
                    scan.AssemblyContainingType<IWebAccess>();     // JCDCTools.Core

                    scan.LookForRegistries();
                    scan.WithDefaultConventions();
                });


            For<IWebAccess>()
                .LifecycleIs<HybridLifecycle>() //why isn't this creating a default instance?
                .Use<WinFormAccess>();

        }

I've googled and dug, and I dont' get why For.use isn't creating a default instance... it looked simple,but I can't get this working..

Can anyone help me understand what I'm doing wrong? either code examples, or documentation that explains better than the official docs on Github (http://structuremap.github.io.)

Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97

1 Answers1

0

As it turns out, I was initializing the container as shown above, and therefor ObjectFactory was not initialized, at all, it had 0 object scanned into it...

Let me repeat that, because it took me a stupidly long time to Grok it... and I'd hate for you to suffer like I did...

In StructureMap 3, you can use ObjectFactory, Xor container...initing the container leaves ObjectFactory dead in the water. If you want you can roll your own ObjectFactory (as below) or have a global static container somewhere that gets set at init time.

look at the 2nd code section here to see examples of each way to init Structure map: http://structuremap.github.io/registration/

(If anyone knows how to init the Object factory using the container as it's input I'd love to know , that way refactoring can be a gradual process, not a full stop till this I is done.)

If I replace my ObjectFactory with a container style call, it works. To make my life easier I made this class based on another SO questions answer, with my own twist of adding a GetInstance method that uses the container.

public static class JcdcObjectFactory
    {
        private static readonly Lazy<Container> _containerBuilder =
            new Lazy<Container>( defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication );


        public static T GetInstance<T>()
        {
            return Container.GetInstance<T>();
        }

        public static IContainer Container
        {
            get { return _containerBuilder.Value; }
        }

        private static Container defaultContainer()
        {
            return new Container( x =>
            {
                // default config, you have to call your initializer here, if you want you can remove the lazy stuff, and make the container a property and set it where ever you init your Structuremap, for me that's IOC.cs - EWB
            } );
        }
    }

Since we only use GetInstance and GetNamedInstance, I just need to add a GetNamedInstance, method working, and then global search & replace ObjectFactory with JCDCObjectfactory, and I'm up and running...

I just need to come back later and refactor it all to use Constructor parameters instead. The nice thing about this is I can do that gradually, not all in one fell swoop.

Eric Brown - Cal
  • 14,135
  • 12
  • 58
  • 97
  • ObjectFactory has been deprecrated. Why are you trying to force one upon yourself? – CSharper Oct 09 '15 at 20:42
  • Because we have a pile of libraries with the anti-pattern already baked in, the above fix gets things working and we can remove the Object factory (which is just a wrapper around the container), at our own pace, instead of taking a big hit up front. – Eric Brown - Cal Oct 13 '15 at 13:46