views:

593

answers:

2

I have overriden the DefaultControllerFactory by using CustomControllerFactory which is actually using StructureMAp ObjectFactory to construct the Controller Instance using IOC. But some how it can not find the Controller instances and failing over it. Note. I already set the DEfaultControllerFactory in Global.asax too. So is there anything else i have to do except registering my registry to the SM .

I Understand registering the controllers solve the problems ,but i am wondering why cant it detect the controller automatically as defaultFactory does?

A: 

Do you have registered the controllers in structuremap? (regard, it's case sensitive)

Sebastian Sedlak
+2  A: 

This is how controller factory looks:

public class ControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(Type controllerType)
    {
        if (controllerType != null)
        {
            return (IController)ObjectFactory.GetInstance(controllerType);
        }
        return null;
    }
}

StructureMap configuration document:

public class DependencyRegistry : Registry
{
    protected override void configure()
    {
        Scan(x =>
                 {
                     x.Assembly("MyApp.Web");
                     x.Assembly("MyApp.Model");
                     x.Assembly("MyApp.DataAccess");
                     x.With<DefaultConventionScanner>();
                 });
        base.configure();
    }

}

Function, which configures StructureMap, using default conventions:

public void RegisterDependencies()
    {
        ObjectFactory.Initialize(InitializeStructureMap);
    }

    private void InitializeStructureMap(IInitializationExpression x)
    {
        x.AddRegistry<DependencyRegistry>();
    }

Example of controller:

public class MyController : Controller
{
    private IMyRepository _repository;

    public MyController (IRepository repository)
    {
        _repository = repository;
    }
}

Don`t forget to call RegisterDependencies() function...

I hope this helps.

Arnis L.
consider that overriding the configure mehtod is obsolete. see https://structuremap.svn.sourceforge.net/svnroot/structuremap/trunk/Source/StructureMap/Configuration/DSL/Registry.cs
Sebastian Sedlak
Thanks for edit.I extracted this small pattern from CodeCampServer project.Maybe you should make authors of it aware about this?
Arnis L.
+1: really valuable comment Sebastian.
boj
@Sebastian: hi dude. Can u please elaborate what we should do instead of overriding the (obsolete) configure() method? The source code (nod to your link) says.. "[Obsolete("configure() is unnecessary. All declarations can be made in the constructor of a Registry or any other method")]" ... but that makes no sense to my tiny brain. :( please help!
Pure.Krome
@Pure.Krome - just use constructor. Instead of overriding, create a constructor and use it like you used "Configure" method before. That's it.
Arnis L.