Gripsoft
Gripsoft

Reputation: 2610

StructureMap with ASP.NET MVC - configure() method is obsolete?

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?

Upvotes: 1

Views: 1474

Answers (2)

Arnis Lapsa
Arnis Lapsa

Reputation: 47607

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.

Upvotes: 3

Sebastian
Sebastian

Reputation: 819

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

Upvotes: 0

Related Questions