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. java后台常见问题

    Java后台面试 常见问题 Nginx负载均衡 轮询.轮询是默认的,每一个请求按顺序逐一分配到不同的后端服务器,如果后端服务器down掉了,则能自动剔除 ip_hash.个请求按访问IP的hash结果 ...

  2. man vxfenadm

    man vxfenadmReformatting page. Please Wait... done VCS 6.0.1 VXFENADM(1M) NAME vxfenadm - Manage SCS ...

  3. Python全栈之路----常用模块----软件开发目录规范

    目录基本内容 log  #日志目录 conf  #配置目录 core/luffycity  #程序核心代码目录  #luffycity 是项目名,建议用小写 libs/modules  #内置模块 d ...

  4. git版本管理工具常用命令

    git是分布式版本管理工具,一台电脑既可以是客户端,也可以是服务端.工作过程中可以断开网络.svn是集中式版本管理工具,一台服务器控制很多客户端,使用过程不能断网. git的优点有:适合分布式开发,强 ...

  5. OO第一次博客作业(第一单元总结)

    Q:菜是绿的,鸡是黄的,那菜鸡是什么颜色的? A:红的,强测全WA了,能不红么. 菜不菜的问题先不说了,认真研究一下这次的题目,以及WA的原因吧. 程序结构简析 三次实验的核心结构都是差不多 第一次的 ...

  6. ubuntu 终端作死体验

    [参考]: https://blog.csdn.net/m0_37192554/article/details/81697791 https://blog.csdn.net/amazingren/ar ...

  7. tensorboard No graph definition files were found No scalar data was found 解决方法

    logdir后面的路径不要加引号!!!! tensorboard --logdir='D:\WorkSpace\eclipse\tf_tr\train\my_graph\' 删除引号,改为: tens ...

  8. python的xml模块用法

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  9. cpu工作原理

  10. VS调试快捷键配置更改

    VS进行调试时,默认情况下需按下Fn+F5等组合按键,手短的用起来很不便利 如何去掉组合键只按下F5? 解决:即按下Fn+Esc,然后就可以直接按下F1-F12使用VS的快捷键,如果想回到组合键也是同 ...