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. js 里面 写 C# 代码 遇到的问题

    js  代码块 必须放置在 body 里面

  2. 有空就写个C++程序

    近期工作变得轻松了非常多,有了一些空暇的时间,准备把大学时候的C++抓起来,而且研究研究算法: 第一个C++程序:计算圆的面积,也是看其它的博客写出来的C++程序. #include<iostr ...

  3. 吞吐量(Throughput)、QPS、并发数、响应时间(RT)对系统性能的影响

    首先对吞吐量().QPS.并发数.响应时间(RT)几个概念一直比较模糊,也不知道哪些指标可以较好的衡量系统的性能.今天特意查了些资料做一些记录:首先看一些概念(来自百度百科) 1. 响应时间(RT) ...

  4. 顶级jQuery树插件

    顶级jQuery树插件 顶级jQuery树插件 2013-03-05 17:20 139人阅读 评论(0) 收藏 举报 jsTree JsTree是一个基于jQuery的Tree控件.支持HTML.J ...

  5. hadoop搭建开发环境及编写Hello World

    hadoop搭建开发环境及编写Hello World   本文地址:http://www.cnblogs.com/archimedes/p/hadoop-helloworld.html,转载请注明源地 ...

  6. ECharts图表系统 特性总览

    最近在玩ECharts,感觉真心不错,在这里把官方的资料收集收集,给大家推荐一下下~ Architecture ECharts (Enterprise Charts 商业产品图表库) 提供商业产品常用 ...

  7. Visual Studio 2012使用水晶报表Crystal Report

    原文:Visual Studio 2012使用水晶报表Crystal Report SAP在 2013年1月14日 released SAP Crystal Reports,developer ver ...

  8. JavaScript语言基础知识8

    这篇文章是对前面学习的知识进行总结: 1.JavaScript支持多种数据类型,如数值类型.字符串类型.布尔类型等. 2.在JavaScript中,字符串是用引號括起来的字符系列,转义字符能够用来表示 ...

  9. 基于EF+MVC+Bootstrap的通用后台管理系统及架构

    分享基于EF+MVC+Bootstrap的通用后台管理系统及架构 基于EF+MVC+Bootstrap构建通用后台管理系统,集成轻量级的缓存模块.日志模块.上传缩略图模块.通用配置及服务调用, 提供了 ...

  10. ASP.NET MVC项目里创建一个aspx视图

    先从控制器里添加视图 视图引擎选"ASPX(C#)",使用布局或模板页不要选. 在Views\EAV目录里,生成的aspx是个单独的页面,没有代码文件,所以代码也要写在这个文件里. ...