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. pycharm(pythoon3)_django2.0_xadmin创建测试用例后台管理系统

    1.测试用例的app名字:Testcase 2.Testcase文件夹下各个文件的代码: 2.1. __init__.py: default_app_config = "TestCase.a ...

  2. mathematics of deep learning (paper reading)

    1.数学上,不变性 2.信息论上

  3. ubuntu安装tensorboardx

    先安装tensorboardX,因为tensorboard依赖于tensorflow中的一些东西,所以安装完tensorboard之后,需要再安装tensorflow pip install tens ...

  4. Convolutional Neural Network Architectures for Matching Natural Language Sentences

    interaction  n. 互动;一起活动;合作;互相影响 capture vt.俘获;夺取;夺得;引起(注意.想像.兴趣)n.捕获;占领;捕获物;[计算机]捕捉 hence  adv. 从此;因 ...

  5. 锚点的animate使用过程中定位不准确的问题小记

    源码: $('html, body, .S').animate({ scrollTop: $('.a1').offset().top - 133}, { duration: 1500, easing: ...

  6. 关于Spring中的<context:annotation-config/>配置(开启注解)

    当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如: 使用@Autowired注解,必须事先在Spring容器中声明AutowiredA ...

  7. java-多线程(下)&GUI

    ###25.01_多线程(单例设计模式)(掌握) * 单例设计模式:保证类在内存中只有一个对象. * 如何保证类在内存中只有一个对象呢?     * (1)控制类的创建,不让其他类来创建本类的对象.p ...

  8. 关于mysql中存储json数据的读取问题

    在mysql中存储json数据,字段类型用text,java实体中用String接受. 返回前端时(我这里返回前端的是一个map),为了保证读取出的数据排序错乱问题,定义Map时要用LinkedHas ...

  9. vue使用axios 时 后台接收不到数据

    后台用django 时,默认接收的数据格式为formdata ,前端如果传了其他格式会出现后台收不到参数的情况. 前端参数转 fromdata 代码如下 let formData = new Form ...

  10. thinkphp5.0.22远程代码执行漏洞分析及复现

    虽然网上已经有几篇公开的漏洞分析文章,但都是针对5.1版本的,而且看起来都比较抽象:我没有深入分析5.1版本,但看了下网上分析5.1版本漏洞的文章,发现虽然POC都是一样的,但它们的漏洞触发原因是不同 ...