Neject 在MVC框架中使用
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框架中使用的更多相关文章
- 找到MVC框架中前端URL与后端同步的解决方案
基本思路: 先用URL标签生成完整的URL字符,前端动态参数的部分以适配符先填充,最后动态参数利用正则匹配进行替换. 这种方式,可以在各种MVC框架中适用,妙. 不废话,上码. var url = & ...
- [原]命令模式在MVC框架中的应用
其实在项目开发中,我们使用了大量的设计模式,只是这些设计模式都封装在框架中了,如果你想要不仅仅局限于简单的使用,就应该深入了解框架的设计思路. 在MVC框架中,模式之一就是命令模式,先来看看模式是如何 ...
- 命令模式在MVC框架中的应用
事实上在项目开发中,我们使用了大量的设计模式,不过这些设计模式都封装在框架中了,假设你想要不只局限于简单的使用,就应该深入了解框架的设计思路. 在MVC框架中,模式之中的一个就是命令模式,先来看看模式 ...
- asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析
下面我用一个实例来和大家分享一下我的经验,asp.net MVC 框架中控制器里使用Newtonsoft.Json对前端传过来的字符串进行解析. using Newtonsoft.Json; usin ...
- 在ASP.NET MVC 框架中调用 html文件及解析get请求中的参数值
在ASP.NET MVC 框架中调用 html文件: public ActionResult Index() { using (StreamReader sr = new StreamReader(P ...
- MVC框架中的值提供机制(三)
在MVC框架中NameValueCollectionValueProvider采用一个NameValueCollection作为数据源,DictionnaryValueProvider的数据源类型自然 ...
- MVC框架中的值提供机制(二)
在MVC框架中存在一些默认的值提供程序模板,这些值提供程序都是通过工厂模式类创建;在MVC框架中存在需要已Factory结尾的工厂类,在值提供程序中也存在ValueProviderFactories工 ...
- MVC框架中的值提供机制(一)
在MVC框架中action方法中的Model数据的绑定的来源有很多个,可能是http请求中的get参数或是post提交的表单数据,会是json字符串或是路径中的相关数据;MVC框架中针对这些不同的数据 ...
- MVC 框架中的缓存
在程序中加入缓存的目的很多是为了提高程序的性能,提高数据的查找效率,在MVC框架中也引入了非常多的缓存,比如Controller的匹配查找,Controller,ControllerDescripto ...
随机推荐
- 在Design界面直接拖放控件的时候,提示AS- This view is not constrained vertically. At runtime it will jump to the left/(0,0) unless you
AS- This view is not constrained vertically. At runtime it will jump to the left/(0,0) unless you ad ...
- Linux报错之ping: www.baidu.com: Name or service not known
Linux报错之ping: www.baidu.com: Name or service not known 出现这个以后,首先去ping下主机Ip,发现能ping通,但是出现另一个问题Destina ...
- 本地新建git仓库后与远端仓库关联
背景说明:如果你想把自己的一个项目开源到,需要新建一个本地代码仓库,然后与远端代码库建立关.不想使用git clone 命令去克隆远端新建代码仓库,然后再将我们写好的代码copy到克隆下来的文件夹里, ...
- Pycharm 常用快捷键与设置
pycharm高频率使用的快捷键 Ctrl+Shift+F10 运行当前的页面 Ctrl + / 注释(取消注释)选择的行 Ctrl+Shift+F 高级查找 Shift + Enter 开始新行 T ...
- 日常遇错之Unable to save settings: Failed to save settings. Please restart PyCharm
将工程的.ideas目录删掉,重启pycharm即可.
- Eclipse导入已有的项目后项目报错的解决办法
第一种:jsp报错 选择windows-->preference-->列表找到Validation-->点击Disable All ->> Apply ->> ...
- LDAP&it's usage
LDAP: 的英文全称是Lightweight Directory Access Protocol,简称为LDAP.LDAP是轻量目录访问协议[1],它是基于X.500标准的,但是简单多了并且可以根据 ...
- JDBC-Oracle连接教程
前言 本文通过一个在Eclipse平台中搭建的小项目,在项目中使用一条静态命令来查询Oracle数据库测试用户“scott”下emp表中的几个字段,来学习JDBC连接数据库的方法.看完之后读者可以基本 ...
- req和resp常用的方法
req: 1. setAttribute()在Request域中存储数据 2. setCharacterEncoding()设置请求参数的编码方式,只对post请求有效 3. getMethod() ...
- Pac-Man 吃豆人
发售年份 1980 平台 街机 开发商 南梦宫(Namco) 类型 迷宫 https://www.youtube.com/watch?v=dScq4P5gn4A