标签:param sele lin sum 根据 text repo efault uid
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 > 0) { 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 > 0) { 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中的使用已经介绍完毕了。
标签:param sele lin sum 根据 text repo efault uid
原文地址:http://www.cnblogs.com/ZXH-CZBK/p/7814980.html