Castle Windsor 项目中快速使用

新建项目如下:

一个模型类,一个接口,一个实现方法。我的目的很明确就是在UI层通过Castle 调用数据访问层的方法。

添加项目引用

CastleDemo.DataAccess 引用 CastleDemo.Domain

CastleDemo.WebUI 引用 CastleDemo.Domain(不需要引用CastleDemo.DataAccess)

安装组件

CastleDemo.DataAccess和 CastleDemo.Domain 都需安装 Castle.Core , Castle.Windsor

CastleDemo.DataAccess 安装 EntityFramework

CastleDemo.Domain

IRepository:

 public interface IRepository<T>  where T : class
{
/// <summary>
/// 查询
/// </summary>
/// <param name="condition">查询条件</param>
/// <param name="order">排序条件</param>
/// <returns></returns>
IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T, object>> order = null);
}

Product:

  public class Product
{
public string productId { get; set; } public string productName { get; set; }
}

IoCContainer:

 internal class IoCContainer
{
private static readonly object syncRoot = new object(); private IoCContainer() { } private static IWindsorContainer _Instance; public static IWindsorContainer Instance
{
get
{
lock (syncRoot)
{
if (_Instance == null)
{
_Instance = new WindsorContainer().Install(Configuration.FromAppConfig());
}
return _Instance;
}
}
}
}

Factories:

 public class Factories
{
public static T Repository<T>() where T : class
{
return IoCContainer.Instance.Resolve<T>();
}
}

CastleDemo.DataAccess

BaseRepository:

     public class BaseRepository<T> : IRepository<T> where T : class
{
private castledemoContext context = Factories.Repository<castledemoContext>(); public IEnumerable<T> Find(Expression<Func<T, bool>> condition, Expression<Func<T,object>> order = null)
{
if (order == null)
{
return context.Set<T>().Where(condition);
} return context.Set<T>().Where(condition).OrderByDescending(order);
}
}

IocInstaller:

     public class IoCInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<castledemoContext>().LifeStyle.PerWebRequest); container.Register(Classes.FromThisAssembly()
.InNamespace("CastleDemo.DataAccess.RepositoryImpl", true)
.LifestylePerWebRequest()
.WithService.AllInterfaces());
}
}

CastleDemo.WebUI

HomeController >> Index:

  public ActionResult Index()
{
Expression<Func<Product, bool>> condition = m => m.productName.Contains("产品");
Expression<Func<Product, object>> order = m => m.productId; var products = Factories.Repository<IRepository<Product>>().Find( order: order,condition: condition); return View(products);
}

Web.config里还需在相应的地方添加一些配置:

  <configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,Castle.Windsor" />
</configSections> --分割--
<httpModules>
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
</httpModules> --分割-- <castle>
<installers>
<install type="CastleDemo.DataAccess.IoCInstaller,CastleDemo.DataAccess" />
</installers>
</castle>

在运行代码前,我们还差最后一步,在前面的添加引用的时候WebUI层没有添加对CastleDemo.DataAccess层的引用,那么这个时 候我们需要把CastleDemo.DataAccess层的EntityFramework.dll 和 CastleDemo.DataAccess.dll拷贝到WebUI的BIN里面,但又有个问题来了,那我们每次对 CastleDemo.DataAccess层代码的改动都要重新拷贝,多麻烦啊,解决方法请参考我的另一篇文章VS XCOPY

最后运行看下效果:

下载:CastleDemo

参考Windsor Tutorial:

http://docs.castleproject.org/Windsor.Windsor-tutorial-ASP-NET-MVC-3-application-To-be-Seen.ashx

分类: Castle IoC

Castle Windsor 项目中快速使用的更多相关文章

  1. [js高手之路]gulp教程-从入门到项目中快速上手使用

    在这之前,我已经分享过一个webpack的全系列,相对于webpack, gulp使用和配置起来非常的简单. gulp是什么? gulp 是基于 node 实现 Web 前端自动化开发的工具,利用它能 ...

  2. 在vue项目中快速使用element UI

    推荐使用npm安装 1.安装:npm install element-ui -S 2.整体引入: 在你项目的main.js中写入: import ElementUI from 'element-ui' ...

  3. Castle Windsor常用介绍以及其在ABP项目的应用介绍

    最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castle Windsor项目常用方法介绍和关于ABP的使用总结 1.下载 ...

  4. 在ABP项目的应用Castle Windsor

    Castle Windsor常用介绍以及其在ABP项目的应用介绍 最近在研究ABP项目,有关ABP的介绍请看阳光铭睿 博客,ABP的DI和AOP框架用的是Castle Windsor下面就对Castl ...

  5. 我是如何在公司项目中使用ESLint来提升代码质量的

    ESLint:你认识我吗 ESLint是一个语法规则和代码风格的检查工具. 和学习所有编程语言一样,想要入门ESLint,首先要去它的官网看看:https://eslint.org/. ESLint的 ...

  6. JavaEE-09 Ajax与jQuery在JavaEE项目中的使用

    学习要点 JavaScript实现Ajax jQuery实现Ajax JSON JSON-LIB FastJSON JavaScript实现Ajax 认识Ajax 旧版百度地图 百度搜索自动补全 百度 ...

  7. 说说ABP项目中的AutoMapper,Castle Windsor(痛并快乐着)

    这篇博客要说的东西跟ABP,AutoMapper和Castle Windsor都有关系,而且也是我在项目中遇到的问题,最终解决了,现在的感受就是“痛并快乐着”. 首先,这篇博客不是讲什么新的知识点,而 ...

  8. 在ASP.NET MVC中使用Castle Windsor

    平常用Inject比较多,今天接触到了Castle Windsor.本篇就来体验其在ASP.NET MVC中的应用过程. Visual Studio 2012创建一个ASP.NET MVC 4网站. ...

  9. MVC中使用Castle.Windsor

    我在MVC中使用Castle.Windsor是这样用的. 首先在UI层安装Install Castle.Windsor 在App_Start中增加一个类WindsorActivator,用于注册和销毁 ...

随机推荐

  1. 开始 space viking 之旅

     设备 cocos2d-v2 眼下cocos2d-v3也不太稳定,它在很大程度上仍然是变化的功能. 对于稳定.我们仍然使用 v2 wget -c http://cocos2d-iphone.goo ...

  2. curl 简单使用

    cURL -- Command Line URL viewer -u username:password 以 Basic 方式发送用户名和密码 -d 以 POST 方式发送数据 -X 支持其它动词, ...

  3. asp.net 获得客户端 mac 地址

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  4. fast-json.jar的用法

    fast-json.jar 解析json数据:一种json数据解析方式是这种,点击这里下载jsonfast.jar+fastjsonAPI文档 [ { "id": 6378, &q ...

  5. 将已有的工程项目添加到Xcode到Git管理中

    在Xcode中创建工程的时候,我们很容易的可以将新创建的工程添加到Git中,如图: 但是如果是本地已经有的工程,那该如何添加到Git中呢? 首先终端进入到该工程的目录. 然后: git init gi ...

  6. NGUI ScrollView动态加入和删除对象。

    动态加入,基本思想是: 1.先把要加入的元素在编辑器中编辑好,制作成一个prefab. 2.在代码中,动态的生成一个新的对象增加到Grid对象的子对象中.这里利用到了Resources对象,这个对象的 ...

  7. Linux之ftp命令使用

    一:前言 在达内參加暑期社会实践,达内公司免费教授了一星期的课,当时认为老师用ftp命令用的非常爽.所以回来学学了. 二:分类 有关FTP(client,server搭建这里不讲)有非常多,大体分为命 ...

  8. TCP/UDP差异

    首先,它 TCP是面向连接的.有序可靠的协议,然后UDP同TCP相对,那张脸无序连接不可靠的协议. 首先,为什么TCP它是面向连接的.由TCP如果传输是需要进行三次握手,这是client为了服务发送数 ...

  9. leetcode第四题--Add Two Numbers

    Problem: You are given two linked lists representing two non-negative numbers. The digits are stored ...

  10. iOS:由URL成员UIImage

    很多时候,我们只能得到URL.然后,需要建立一个UIImage. 在正常情况下,.我们一般通过SDWebImage直接施工UIImageVIew的image,如何使用URL直接施工UIImage它? ...