ninject / Ninject.Extensions.Conventions

Convention based binding for Ninject
Other
76 stars 28 forks source link

"public interface" with "internal class" implementation not supported #10

Closed JanWosnitza closed 11 years ago

JanWosnitza commented 11 years ago
// ignore typos ;)

public interface IExample
{
    int Property { get; }
}

internal class Example : IExample
{
    public int Property
    { get { return 0; } }
}

public ExampleModule : NinjectModule
{
    public override void Load()
    {
        Bind( x => x
            .FromThisAssembly()
            .BindDefaultInterface() );
    }
}

This will not bind Example to IExample. Is this on purpose?

I expeted this to be working, since IExample could be used by anyone. The concrete implementation of the interface is not important to any users of the interface.

I really would like to have this setup working. (I would call this "injection in perfection" lolz)

JanWosnitza commented 11 years ago

For anybody interessted my "workaround" is:

var assembly = typeof( __Module__ ).Assembly;
foreach ( var type in assembly.GetTypes() )
{
    if ( !type.IsClass ||
         type.IsAbstract )
        continue;

    var i = type.GetInterfaces().FirstOrDefault( t => t.Name == "I" + type.Name );
    if ( ( i == null ) ||
            ( i.Assembly != assembly ) )
        continue;

    if ( type.IsGenericTypeDefinition &&
            i.IsGenericType &&
            i.GetGenericArguments().All( t => t.IsGenericParameter && ( t.DeclaringType == type ) ) )
        i = i.GetGenericTypeDefinition();

    this.Bind( i ).To( type );
}
remogloor commented 11 years ago

It is supported:

.FromThisAssembly().IncludingNonePublicTypes().SelectAllClasses().BindDefaultInterface()
JanWosnitza commented 11 years ago

Oh thx :) now this ticket is at least some documentation ^^