ninject / Ninject.Extensions.Conventions

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

Named bindings by convention #36

Open Micha-kun opened 4 years ago

Micha-kun commented 4 years ago

I have a lot of implementations that would be good if all their bindings were named dynamically, pe. marking a class with a special attribute with the named instance. Currently, Named method only accepts string as a parameter, not a function like others that could use IContext. Is there a way to do what I'm trying to do currently with Ninject? Thank you.

this.Bind(
                ctx =>
                {
                    ctx.FromThisAssembly()
                       .SelectAllClasses()
                       .InheritedFrom<IReportGenerationManager>()
                       .WithAttribute<ReportPeriodicityAttribute>(attr => attr.Value == "Monthly")
                       .BindToSelf()
                       .Configure(
                            syntax => 
                                syntax
                                  .Named(/* I WANT HERE TO READ THE CLASS ATTRIBUTE TO NAME THE BINDING */)
                                  .WithConstructorArgument(
                                     "dataMartRepository",
                                      ctxArg => ctxArg.Kernel.Get<IDataMartRepository>("Monthly")));
                });