Inject many classes to controller using ninject

Asked at 2017-01-07 15:26:52Z
  • 5 Subscribers
  • 114 Views
0

I have following code in my WebApi project

public class HomeApiController : ApiController
    {
        private WebSettingBll _webSettingBll;
        private PageBll _pageBll;
        private HomeBll _homeBll;
        private readonly HitCountBll _hitCountBll;
        public HomeApiController()
        {
            _hitCountBll = new HitCountBll();
            _webSettingBll = new WebSettingBll();
            _homeBll = new HomeBll();
            _pageBll = new PageBll();
        }

    }

I use those *Bll class in HomeApi methods
Is this design basically wrong (using many *Bll classes in controller)if it is help me to correct it.

I also know that my controller is not decoupled with those *Bll classes how can I use Ninject to solve this issue (with this approach or with redesigning)I ask this because I think my code is wrong because I couldn't find any case like this in any blog that I've read about ninject.

Note:I’m new with using Ninject


1 answers in total

2
federico scamuzzi Posted at 2017-01-07 15:34:34Z

Hi Basically what you want is DI if i understand correctly... i use Unity but Ninject is the same.. try with something like:

public class HomeApiController : ApiController
    {
        private IWebSettingBll _webSettingBll;
        private IPageBll _pageBll;
        private IHomeBll _homeBll;
        private readonly IHitCountBll _hitCountBll;
        public HomeApiController(IWebSettingBll webSettingBll,IHitCountBll hitCountBll,IPageBll pageBll,IHomeBll homeBll)
        {
            _hitCountBll = hitCountBll;
            _webSettingBll = webSettingBll;
            _homeBll = homeBll;
            _pageBll = pageBll;
        }

    }

And then you Map your interfaces with the correct implementations (classes) in just one file (so it'll be easy if you need to change somthing ..cause you 'll need to change it in just one file and not everywhere you used the interface instead the concrete classes):

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http.Formatting;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web.Http;       
    using Newtonsoft.Json;
    using Newtonsoft.Json.Serialization;
    using Ninject;
    using Ninject.Web.Common.OwinHost;
    using Ninject.Web.WebApi.OwinHost;
    using Owin;


    namespace SelfHostSIAE
    {
        public class Startup
        {
            // This code configures Web API. The Startup class is specified as a type
            // parameter in the WebApp.Start method.
            public void Configuration(IAppBuilder appBuilder)
            {
                // Configure Web API for self-host. 
                HttpConfiguration config = new HttpConfiguration();


                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );

                var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
                jsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                // jsonFormatter.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
                jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

                appBuilder.UseErrorPage();
                appBuilder.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
                appBuilder.UseNinjectMiddleware(CreateKernel);
                appBuilder.UseNinjectWebApi(config);
                //appBuilder.UseWebApi(config);
            }

            public static IKernel CreateKernel()
            {
                var kernel = new StandardKernel();

                kernel.Bind<IWebSettingBll>().To<WebSettingBll>().InSingletonScope(); //<-- IF YOU WANT  A SINGLETON
                kernel.Bind<IPageBll>().To<PageBll>();
                kernel.Bind<ILogger>().To<Logger>();
                kernel.Bind<IHomeBll>().To<HomeBll>();
                kernel.Bind<IHitCountBll>().To<HitCountBll>();
                return kernel;
            }
        }
    }

This is an example with Ninject .. i use it in a Self Hosted webapi (console app)..but is similar to a webapi project or toher type of project

Answer this questsion