How to inject dependencies using Ninject In ASP.NET WebForm?

前端 未结 2 672
南笙
南笙 2021-01-14 12:38

I have a fair idea of using the Repository Pattern and have been attempting to \"upgrade\" our current way of creating ASP .Net websites. So i do the following

相关标签:
2条回答
  • 2021-01-14 12:54

    Install Ninject.Web either from "Package Manager Console" or NuGet.

    Version is 3.2.1 as of this writing.

    enter image description here

    OR

    enter image description here

    It will install the following 4 packages -

    enter image description here

    Sample Service Class

    public interface IUserService
    {
        List<string> GetUsers();
    }
    
    public class UserService : IUserService
    {
        public List<string> GetUsers()
        {
            return new List<string> {"john", "eric"};
        }
    }
    

    Then add binding to ~/App_Start/NinjectWebCommon.cs.

    enter image description here

    In code behind page, property inject using [Inject] attribute.

    enter image description here

    0 讨论(0)
  • 2021-01-14 13:08

    In Addition in answer by win I would advise people not to get confused by using Constructor based injection in ASP.NET Webforms as Web Forms doesn't support constructor based injection simply. In default configuration they only support Property based Injections as already demonstrated by Win.

    0 讨论(0)
提交回复
热议问题