开发者

What options do I have for automating bindings with NInject

开发者 https://www.devze.com 2023-02-04 11:01 出处:网络
Rather than manually having to bind every class, what methods and patterns, if any, are recommended for automatically setting up bindings?

Rather than manually having to bind every class, what methods and patterns, if any, are recommended for automatically setting up bindings?

For example, the vast majority of bindings simply look like this:

Bind<ICustomerRepository>.To<CustomerRepository>();

开发者_运维百科Once modules get large, you can end up with 100s of bindings that all look exactly the same. Can this be automated?


check out the conventions extension: https://github.com/ninject/ninject.extensions.conventions

        using (IKernel kernel = new StandardKernel())
        {
            var scanner = new AssemblyScanner();
            scanner.From(Assembly.GetExecutingAssembly());
            scanner.BindWith<DefaultBindingGenerator>();
            kernel.Scan(scanner);
            var instance = kernel.Get<IDefaultConvention>();

            instance.ShouldNotBeNull();
            instance.ShouldBeInstanceOf<DefaultConvention>();
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消