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

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

  1. Create a solution with a class project called DataAccessLayer and another class project called BusinessLogicLayer. Finally a 3rd project which is my ASP .Net website (a normal site).
  2. I add a dbml file to the DAL and drag a table, then in my BLL i add an interface and a class which implements this interface:

My interface

namespace BLL.Interfaces
{
    interface IUser
    {
        List<User> GetAllUsers();
    }
}

In my class

namespace BLL.Services
{
   public class UserService : BLL.Interfaces.IUser
    {
        public List<User> GetUsers()
        {
            throw new NotImplementedException();
        }
    }
}

I know the code is not fully completed, but there for illustrative purposes.

So i right click the BLL project > Manage NuGet Packages > Searched for Ninject and found a few. I was overwhelmed with the number of entries returned after after further research i am lost in how to add Ninject to a normal ASP .Net website? Specifically which addin i require? As there are many MVC and reading further i think im a little confused.

I was trying to add it to the BLL project as thats where i THINK it should go so i can register my services in there.

Could anyone guide me in what i need to so in order to use Ninject entries but im not using MVC?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x