如何在ASP.NET WebForm中使用Ninject注入依赖项? [英] How to inject dependencies using Ninject In ASP.NET WebForm?

查看:173
本文介绍了如何在ASP.NET WebForm中使用Ninject注入依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很好的使用存储库模式,并一直在尝试升级我们当前的方式来创建ASP .Net网站。所以我做以下


  1. 创建一个名为 DataAccessLayer 的类项目的解决方案,另一个类项目称为 BusinessLogicLayer 即可。最后一个第三个项目是我的 ASP .Net网站(一个正常网站)。

  2. 我将一个dbml文件添加到DAL并拖动一个表,然后在我的BLL我添加了一个接口和一个实现这个界面的类:



我的界面



 命名空间BLL.Interfaces 
{
接口IUser
{
列表<用户> GetAllUsers();
}
}



在我的课上



 命名空间BLL.Services 
{
public class UserService:BLL.Interfaces.IUser
{
public List&使用者名称> GetUsers()
{
throw new NotImplementedException();
}
}
}

我知道代码不是完全完成,但是为了说明的目的。



所以我右键单击BLL项目>管理NuGet软件包>搜索Ninject并找到几个。在进一步研究之后,我失去了如何将Ninject添加到正常的ASP .Net网站,输入的数量不足。具体哪些加我需要?因为有很多MVC和阅读进一步我觉得有点困惑。



我正在尝试将其添加到BLL项目,因为我认为它应该去,所以我可以在那里注册我的服务。



任何人都可以指导我所需要的,以便使用Ninject条目,但是不使用MVC ?

解决方案

安装 Ninject.Web 来自软件包管理器控制台或NuGet。



版本是3.2.1









它将安装以下4个包 -





示例服务类别



  public interface IUserService 
{
列表< string> GetUsers();
}

public class UserService:IUserService
{
public List< string> GetUsers()
{
返回新列表< string> {john,eric};
}
}

然后添加绑定到〜 /App_Start/NinjectWebCommon.cs





在页面后面的代码中,使用 [Inject] 属性注入属性。




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?

解决方案

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

Version is 3.2.1 as of this writing.

OR

It will install the following 4 packages -

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.

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

这篇关于如何在ASP.NET WebForm中使用Ninject注入依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆