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

前端 未结 2 686
南笙
南笙 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 GetUsers();
    }
    
    public class UserService : IUserService
    {
        public List GetUsers()
        {
            return new List {"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

提交回复
热议问题