webadvanced / Structuremap.MVC5

Apache License 2.0
21 stars 9 forks source link

ControllerConvention update for StructureMap 4 #15

Open JenTechSystems opened 8 years ago

JenTechSystems commented 8 years ago

With the release of StructureMap4 it appears that the ControllerConvention registration class requires a minor update.

This is how I have changed it, not sure if this is the best way to go:

    public void ScanTypes(TypeSet types, Registry registry)
    {
        types.AllTypes().ForEach(type =>
        {
            if (type.CanBeCastTo<Controller>() && !type.IsAbstract)
            {
                registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
            }
        });
    }
onslaughtq commented 8 years ago

What about:

    public void ScanTypes(TypeSet types, Registry registry)
    {
        foreach(var type in types.FindTypes(TypeClassification.Concretes).Where(type => type.CanBeCastTo<Controller>()))
        {
            registry.For(type).LifecycleIs(new UniquePerRequestLifecycle());
        };
    }

Finding only concrete types first would avoid an unnecessary attempt to cast things that are obviously not controllers.

mxmissile commented 8 years ago

+1 for a PR

ABonner commented 8 years ago

If there is an agreed upon update for this that won't break existing users, feel free to submit a PR and it will be incorporated.

mxmissile commented 8 years ago

After thinking about this more, might be better off with a Structuremap4.MVC5 fork.

I still need to play around with SM4 more.

dotnetchris commented 8 years ago

I think this also requires a small change to the DefaultRegistry class. It looks like SM4 removed TheCallingAssembly. I rotated in:

using StructureMap;

public class DefaultRegistry : Registry {
    #region Constructors and Destructors

    public DefaultRegistry() {
        Scan(
            scan => {
                //think this reasonably replaces TheCallingAssembly
                scan.Assembly(GetType().Assembly); 

                scan.WithDefaultConventions();
                scan.With(new ControllerConvention());
            });
        //For<IExample>().Use<Example>();
    }

    #endregion
}
justinmichaels commented 8 years ago

@dotnetchris I'm still seeing TheCallingAssembly as part of the StructureMap.Graph.IAssemblyScanner. I have version 4.1.2.386 that I'm referencing from NuGet.

robgha01 commented 8 years ago

Is this project dead ? im planing to upgrade structuredmap from 3 to 4.