ninject / Ninject.Extensions.Conventions

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

WhereTypeInheritsFrom shouldn't have side effect #5

Closed WalkerCodeRanger closed 13 years ago

WalkerCodeRanger commented 13 years ago

We just spent half a day tracking this issue down. The WhereTypeInheritsFrom registers a Binding Convention. It should not. I know that in issue #2 you seemed to indicate that this is desired behavior, however that really doesn't work. It is quite reasonable that I might want to select types by inheritance to bind with some other convention than WhereTypeInheritsFrom. I should be allowed to specify my own binding convention.

Furthermore, the regular expression you are using is incorrect. I believe you need to add ^ to the beginning and $ to the end. Currently WhereTypeInheritsFrom<IFoo>() would cause those types to be bound to interfaces such as IFooBar and UIFoo if they also implemented them.

The code where we had this problem is: Kernel.Scan(x => { x.FromAssemblyContaining(); x.WhereTypeInheritsFrom(); x.BindWithDefaultConventions(); x.InRequestScope(); });

In this case you can see we are using WhereTypeInheritsFrom with a type that isn't even an interface which is perfectly reasonable. But it causes the types to be bound to things like IUserRepoistory becuase they contain the word Repository in the type name.

idavis commented 13 years ago

I could probably switch over to creating a filter delegate instead. That would probably be simpler. In the #2 issue, I was just trying to make it clear that there were two different calls depending on what you wanted to do.

idavis commented 13 years ago

Fixed