need to use strcucturemap IOC

Oct 3, 2010 at 5:57 PM

Hi,

 can you show me how to use the structuremap IOC with the boostrapper?

Oct 3, 2010 at 9:15 PM

It should be as simple as: 

public class StructureMapBootstrapper : Bootstrapper<IShell>
{
    StructureMap.IContainer _container;

    protected override void Configure()
    {
        _container = ... //create and configure instance of StructureMap container
    }

    protected override object GetInstance(Type serviceType, string key)
    {
         return _container.GetInstance(serviceType, key);
    }

    protected override IEnumerable<object> GetAllInstances(Type serviceType)
    {
        return _container.GetAllInstances(serviceType);
    }

    protected override void BuildUp(object instance)
    {
        _container.BuildUp(serviceType); 
    }
}

(borrowed some code from full Caliburn's StructureMap adapter)

Oct 3, 2010 at 11:04 PM

I still get an error becuase  have not wired up the viewmodels to the container.

 

Here is a snipper that works with UnityIOC:    (how do i do the same for Structuremap???)

=============

   IUnityContainer unity = new UnityContainer();

            // Register our services.
            unity.RegisterType<IFileService, FileService>();

            // Register all the ViewModels in our project.))
            foreach (Type t in typeof(ViewModel).Assembly.GetTypes())
            {
                if (typeof(ViewModel).IsAssignableFrom(t))
                {
                    unity.RegisterType(t);
                }
            }

=======================

 

the above code works for unityIOC, how do i make it work for structuremap???

what is the equivalent command of [  unit.registerType(t) ]     ?????

 

Oct 3, 2010 at 11:47 PM

I got it to work... if anyone is interested:

 public class ClientBootstrap : Bootstrapper<ShellViewModel>
    {
        StructureMap.IContainer _container;

        protected override void Configure()
        {
            _container = new StructureMap.Container(x =>
                                                                //x.For<IFileService>().Use<FileService>();
                                                                x.Scan(r =>
                                                                    {

                                                                        r.TheCallingAssembly(); // Here are all our prsenters / views
                                                                        r.WithDefaultConventions();
                                                                    })); //create and configure instance of StructureMap container

        }

        protected override object GetInstance(Type serviceType, string key)
        {
            return string.IsNullOrEmpty(key)
                       ? _container.GetInstance(serviceType)
                       : _container.GetInstance(serviceType ?? typeof(object), key);
        }

        protected override IEnumerable<object> GetAllInstances(Type serviceType)
        {
            foreach (var obj in _container.GetAllInstances(serviceType))
            {
                yield return obj;
            }
        }

        protected override void BuildUp(object instance)
        {
            _container.BuildUp(instance);
        }

    }

 

 

Oct 4, 2010 at 8:22 AM

OK, sorry. I thougt your issue was with Bootstrapper, but it's actually about StructureMap configuration. Glad you got it working anyway :-)