Neject 开始是用3.3.0.0,不能自动生成NinjectWebCommon文件,测试了很久发现,是版本的问题 ,后来用Nuget卸载后,重新下了Ninject,Ninject.Web.Common,Ninject.Web.Mvc这三个程序集,都是选择3.2.0.0版本的,然后进行测试,加一个类库,里面放接口,实现类,以及实体类

1.创建接口

using ADT.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ADT.Abstract
{
public interface IProduct
{
/// <summary>
/// 根据主键id获取一个实体
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
ProductBean GetModelById(int id);
/// <summary>
/// 获取所有数据,返回List集合
/// </summary>
/// <returns></returns>
List<ProductBean> GetAllList(); bool AddOrEdit(ProductBean model); /// <summary>
/// 根据主键id改变ValID值
/// </summary>
bool UpdateValIDById(long id, bool valid);
}
}

2.创建实现类

using ADT.Abstract;
using ADT.Entities;
using CYQ.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ADT.Concrete
{
public class Product : IProduct
{
/// <summary>
/// 对商品的添加和编辑
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool AddOrEdit(ProductBean model)
{
bool result = false;
using (MAction act = new MAction("Product"))
{
act.Set("ProductName", model.ProductName);
act.Set("ProdutPrice", model.ProdutPrice);
act.Set("ValidTime", model.ValidTime);
act.Set("Valid", model.Valid);
act.Set("Coin", model.Coin);
if (model.Id > )
{
result = act.Update("Id=" + model.Id);
}
else
{
result = act.Insert();
} }
return result;
} /// <summary>
/// 获取全部产品信息
/// </summary>
/// <returns></returns>
public List<ProductBean> GetAllList()
{
List<ProductBean> list = new List<ProductBean>();
using (MAction act = new MAction("Product"))
{
list = act.Select().ToList<ProductBean>();
}
return list;
}
/// <summary>
/// 根据主键id获取一个实体信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ProductBean GetModelById(int id)
{
ProductBean model = null;
using (MAction act = new MAction("Product"))
{
if (act.Fill(id))
{
model = act.Data.ToEntity<ProductBean>(); }
}
return model;
} /// <summary>
/// 根据主键id改变ValID值
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool UpdateValIDById(long id, bool valid)
{
bool result = false;
using (MProc proc = new MProc("update Product set Valid=@valid where ID=@id"))
{
proc.Set("valid", valid);
proc.Set("id", id);
int num = proc.ExeNonQuery();
if (num > )
{
result = true;
}
}
return result;
}
}
}

3.创建实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ADT.Entities
{
public class ProductBean
{
/// <summary>
/// 主键
/// </summary>
public Int64 Id { get; set; }
/// <summary>
/// 产品名称
/// </summary>
public string ProductName { get; set; }
/// <summary>
/// 产品价格
/// </summary>
public Decimal ProdutPrice { get; set; }
/// <summary>
/// 无期限的为0, 3个月为3,6个月为6, 12个月为12
/// </summary>
public int ValidTime { get; set; }
/// <summary>
/// 是否有效
/// </summary>
public bool Valid { get; set; }
/// <summary>
/// 产品虚拟币
/// </summary>
public Int64 Coin { get; set; }
}
}

4.在web.config配置下连接字符串

<add name="conn" connectionString="server=.;database=xx;uid=sa;pwd=xxxx;Max Pool Size=512;Min Pool Size=5;Connect Timeout=600;" />

5.我用的是ORM框架是CYQ,Data,这边需要引用一个CYQ.Data的程序集,用作和数据库交互的

新增一个NinjectDependencyResolver.cs的文件,实现IDependencyResolver,贴上代码如下:

using ADT.Abstract;
using ADT.Concrete;
using Ninject;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace WebApplication4
{
public class NinjectDependencyResolver : IDependencyResolver
{ private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
} public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
} public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
} private void AddBindings()
{
kernel.Bind<IProduct>().To<Product>();
} }
}

接下来新键一个控制器,测试下依赖注入有没有成功

using ADT.Abstract;
using ADT.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace WebApplication4.Controllers
{
public class DefaultController : Controller
{
private IProduct productRepository;
public DefaultController(IProduct productRepository)
{
this.productRepository = productRepository;
}
// GET: Default
public ActionResult Index()
{
List<ProductBean> list = productRepository.GetAllList();
ViewBag.count = list;
return View();
}
}
}

这样下来,Neject在MVC5中的使用已经介绍完毕了。

Neject 在MVC框架中使用的更多相关文章

  1. 找到MVC框架中前端URL与后端同步的解决方案

    基本思路: 先用URL标签生成完整的URL字符,前端动态参数的部分以适配符先填充,最后动态参数利用正则匹配进行替换. 这种方式,可以在各种MVC框架中适用,妙. 不废话,上码. var url = & ...

  2. [原]命令模式在MVC框架中的应用

    其实在项目开发中,我们使用了大量的设计模式,只是这些设计模式都封装在框架中了,如果你想要不仅仅局限于简单的使用,就应该深入了解框架的设计思路. 在MVC框架中,模式之一就是命令模式,先来看看模式是如何 ...

  3. 命令模式在MVC框架中的应用

    事实上在项目开发中,我们使用了大量的设计模式,不过这些设计模式都封装在框架中了,假设你想要不只局限于简单的使用,就应该深入了解框架的设计思路. 在MVC框架中,模式之中的一个就是命令模式,先来看看模式 ...

  4. asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析

    下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...

  5. 在ASP.NET MVC 框架中调用 html文件及解析get请求中的参数值

    在ASP.NET MVC 框架中调用 html文件: public ActionResult Index() { using (StreamReader sr = new StreamReader(P ...

  6. MVC框架中的值提供机制(三)

    在MVC框架中NameValueCollectionValueProvider采用一个NameValueCollection作为数据源,DictionnaryValueProvider的数据源类型自然 ...

  7. MVC框架中的值提供机制(二)

    在MVC框架中存在一些默认的值提供程序模板,这些值提供程序都是通过工厂模式类创建;在MVC框架中存在需要已Factory结尾的工厂类,在值提供程序中也存在ValueProviderFactories工 ...

  8. MVC框架中的值提供机制(一)

    在MVC框架中action方法中的Model数据的绑定的来源有很多个,可能是http请求中的get参数或是post提交的表单数据,会是json字符串或是路径中的相关数据;MVC框架中针对这些不同的数据 ...

  9. MVC 框架中的缓存

    在程序中加入缓存的目的很多是为了提高程序的性能,提高数据的查找效率,在MVC框架中也引入了非常多的缓存,比如Controller的匹配查找,Controller,ControllerDescripto ...

随机推荐

  1. sqlite当天时间的23:59:59

    select strftime('%Y-%m-%d %H:%M:%S','now','+1 day','localtime','start of day','-1 seconds')

  2. CF867E: Buy Low Sell High(贪心, STL) (hdu6438)

    Description 有nn个城市,第ii个城市商品价格为aiai​,从11城市出发依次经过这nn个城市到达n n城市,在每个城市可以把手头商品出售也可以至多买一个商品,问最大收益. Input 第 ...

  3. 阿里云 oss 图片上传解决方案 vue (web直传)

    我们通过aliyun-oss-web这个npm去解决 该文章主要介绍如何获取 imgSignature 和 imgPolicy 这两个参数 首先下载 web直传的案例 : http://files.c ...

  4. day11.3分页操作divmod

    # 分页显示 divmod(被除数,除数) INFO_LIST = [] for i in range(836): template = "第%s天,笨笨先僧 always be there ...

  5. Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断

    1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if …; then … ;elif …; then ...

  6. [C]奇数求和

    /* 用递归算法实现,输入整数n(n>0), 求1+3+5+7….+(2*n-1) 的和 */ #include<stdio.h> int add(int n); int main( ...

  7. mybatis 中javaType和OfType 的区别

    JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型.pojo类: publiccla ...

  8. visual studio 2017 中默认无法开发 Android 8.0 及以上系统的解决方案

    一般默认比较旧有两个原因,系统版本过旧,Visual Studio 版本过旧. 第一步,将windows 更新到最新版,必须是windows 10 并且更新到最新. 第二步,将visual studi ...

  9. xamarin android 文件选择

    调出选择界面: Intent intent = new Intent(Intent.ActionGetContent); intent.AddCategory(Intent.CategoryOpena ...

  10. WinForm控件开发总结目录

    WinForm控件开发总结(一)------开篇 WinForm控件开发总结(二)------使用和调试自定义控件 WinForm控件开发总结(三)------认识WinForm控件常用的Attrib ...