After kernel.Bind().To()
put .InSingletonScope()
Ninject singleton scope loading
-
11-04-2022 - |
题
I am very much new to ASP.NET MVC and I need help loading some types in singleton scope using Ninject.
--- existing code looks as ----
List<Type> types = loading some types into list here.
foreach (var type in types.Where(O => O.Name.StartsWith("I")))
{
Kernel.Bind(type).To(Type.GetType(type.FullName.Replace(".I", ".")));
}
My job is to bind these types in singleton scope and I am not sure how to do that.
解决方案
其他提示
Also have a look at the Ninject conventions extension. It makes stuff like this a lot easier. For example you could write it like this instead.
kernel.Bind(x =>
x.FromThisAssembly()
.SelectAllClasses()
.Where(types.Contains)
.BindDefaultInterface()
.Configure(b => b.InSingletonScope()));
Depending on how you get your list of types, this might be written even easier. Just check out the documentation and the samples on the wiki.
https://github.com/ninject/ninject.extensions.conventions/wiki
不隶属于 StackOverflow