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. 突破IP限制动态替换代理ip。

    须要导入的两个jar包 实现的javabean <span style="font-size:18px;">package com.jx.po; public clas ...

  2. poj 1556 zoj1721 BellmanFord 最短路+推断直线相交

    http://poj.org/problem?id=1556 The Doors Time Limit: 1000MS   Memory Limit: 10000K Total Submissions ...

  3. SQL2005性能分析一些细节功能你是否有用到?(二)

    原文:SQL2005性能分析一些细节功能你是否有用到?(二) 上一篇:SQL2005性能分析一些细节功能你是否有用到? 我简单的提到了些关于SQL性能分析最基本的一些方法,下面的文章我会陆续补充.前面 ...

  4. 关于linux 原始套接字编程

    关于linux 网络编程最权威的书是<<unix网络编程>>,但是看这本书时有些内容你可能理解的不是很深刻,或者说只知其然而不知其所以然,那么如果你想搞懂的话那么我建议你可以看 ...

  5. SQL表连接

    背景 在上次的自考科目<数据库系统原理>中.已经接触到了关于数据库表连接的一些知识,近期的学习过程中又用到了关于数据库表的连接问题,趁此再跟大家一起回想一下. 导图总结 首先用一张思维导图 ...

  6. jQuery 添加 删除 改动select option

    jQuery获取Select选择的Text和Value: 1. var checkText=jQuery("#select_id").find("option:selec ...

  7. 编译预处理 -- 带参数的宏定义--【sky原创】

    原文:编译预处理 -- 带参数的宏定义--[sky原创] 如有转载请注明出处   编译预处理  --  带参数的宏定义 前面为输出文件,后面为输入文件 gcc -E -o test.i test.c ...

  8. PHP 5:PHP语法导向

    原文:PHP 5:PHP语法导向 代码                                                                                  ...

  9. xfire集成spring构建webservice

    前言:xfire.spring都是比较流行的技术,这里就不再赘述他们各自的优点:本文着重介绍xfire和spring的整合,不会做太深入的探究. 服务端 1. web.xml配置 spring配置部分 ...

  10. Servlet配置load-on-startup

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...