- 浏览: 111992 次
- 来自: ...
文章分类
最新评论
-
Juanjuel:
Django多表查询 -
chenokia:
那么在\grails-app\conf 下的DataSourc ...
Grails如何使用外部配置文件 -
viking2439:
有没有方法去指定用户去获得某个权限呢?
[django]总结Django中的用户权限模块
我们知道在我们使用Castle IOC的时候,若你的类库在容器中的时候,你只需要通过公开属性或构造时设置参数后,Castle容器就会自动的根据配置文件中的服务为您实例化相应的类。但他并不支持Asp.Net的后置代码类。那么在Asp.Net中应如何获取容器中的服务呢?
我们可以通过如下方式:
我曾通过这个方式写了一个页面基类,页面继承该类后,就可以通过公开属性的方式来得到服务。
你也可以看 自由、创新、研究、探索…… 的博客上也有相应的介绍:在asp.net页面上得到Castle容器的实例 。
这是我以前的做法,今天所要讲的是另一种使用方式,通过HttpHandler的方式将每个页面的HttpHandler增加到Castle容器中。(以下代码来自互联网)
这样在我们的后置代码类中就可以通过公开属性的方式来获取容器中的服务。
我们可以通过如下方式:
IWindsorContainer container = ContainerAccessorUtil.GetContainer();
UserManage um = (UserManage)container["usermanage"];
其中usermanage就是配置文件中的component的ID.UserManage um = (UserManage)container["usermanage"];
我曾通过这个方式写了一个页面基类,页面继承该类后,就可以通过公开属性的方式来得到服务。
private BindingFlags BINDING_FLAGS_SET
= BindingFlags.Public
| BindingFlags.SetProperty
| BindingFlags.Instance
| BindingFlags.SetField
;
protected override void OnInit(EventArgs e)
{
//判断退出
if (IsCheckLogin== true && this.UserId == -1)
{
Response.Redirect("~/index.aspx");
}
IWindsorContainer container = ObtainContainer();
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
foreach (PropertyInfo propertie in properties)
{
string pname = propertie.Name;
if (container.Kernel.HasComponent(pname))
{
propertie.SetValue(this, container[pname], null);
}
}
base.OnInit(e);
}
public IWindsorContainer ObtainContainer()
{
IContainerAccessor containerAccessor =
HttpContext.Current.ApplicationInstance as IContainerAccessor;
if (containerAccessor == null)
{
throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
}
IWindsorContainer container = containerAccessor.Container;
if (container == null)
{
throw new ApplicationException("HttpApplication 得不到容器的实例");
}
return container;
}
= BindingFlags.Public
| BindingFlags.SetProperty
| BindingFlags.Instance
| BindingFlags.SetField
;
protected override void OnInit(EventArgs e)
{
//判断退出
if (IsCheckLogin== true && this.UserId == -1)
{
Response.Redirect("~/index.aspx");
}
IWindsorContainer container = ObtainContainer();
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
foreach (PropertyInfo propertie in properties)
{
string pname = propertie.Name;
if (container.Kernel.HasComponent(pname))
{
propertie.SetValue(this, container[pname], null);
}
}
base.OnInit(e);
}
public IWindsorContainer ObtainContainer()
{
IContainerAccessor containerAccessor =
HttpContext.Current.ApplicationInstance as IContainerAccessor;
if (containerAccessor == null)
{
throw new ApplicationException("你必须在HttpApplication中实现接口 IContainerAccessor 暴露容器的属性");
}
IWindsorContainer container = containerAccessor.Container;
if (container == null)
{
throw new ApplicationException("HttpApplication 得不到容器的实例");
}
return container;
}
你也可以看 自由、创新、研究、探索…… 的博客上也有相应的介绍:在asp.net页面上得到Castle容器的实例 。
这是我以前的做法,今天所要讲的是另一种使用方式,通过HttpHandler的方式将每个页面的HttpHandler增加到Castle容器中。(以下代码来自互联网)
public class PageHandler : IHttpHandler, IRequiresSessionState
{
private static readonly ILog log = LogManager.GetLogger(typeof(PageHandler));
IHttpHandler Members#region IHttpHandler Members
/**//// <summary>
/// Process the aspx request. This means (eventually) rewriting the url and registering the page
/// in the container.
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
string rawUrl = context.Request.RawUrl;
log.Info("Starting request for " + rawUrl);
DateTime startTime = DateTime.Now;
// Rewrite url
//UrlRewriter urlRewriter = new UrlRewriter(context);
//string rewrittenUrl = urlRewriter.RewriteUrl(rawUrl);
// Obtain the handler for the current page
string aspxPagePath = rawUrl.Substring(0, rawUrl.IndexOf(".aspx") + 5);
IHttpHandler handler = PageParser.GetCompiledPageInstance(aspxPagePath, null, context);
// Register the page in the container
handler = RegisterAspxPage(handler, context);
// Process the page just like any other aspx page
handler.ProcessRequest(context);
// Remove the page from the container
RemoveAspxPage(handler, context);
// Log duration
TimeSpan duration = DateTime.Now - startTime;
log.Info(String.Format("Request finshed. Total duration: {0} ms.", duration.Milliseconds));
}
/**//// <summary>
///
/// </summary>
public bool IsReusable
{
get { return true; }
}
#endregion
private IHttpHandler RegisterAspxPage(IHttpHandler handler, HttpContext context)
{
if (handler is IRuixinPage)
{
string pageKey = Guid.NewGuid().ToString();
IWindsorContainer container = ContainerAccessorUtil.GetContainer();
container.Kernel.AddComponent(pageKey, handler.GetType());
if (container.Kernel.HasComponent(handler.GetType()))
{
IHttpHandler newHandler = (IHttpHandler)container[handler.GetType()];
handler = newHandler;
}
}
return handler;
}
private void RemoveAspxPage(IHttpHandler handler, HttpContext context)
{
if (handler is IRuixinPage)
{
IWindsorContainer container = (context.ApplicationInstance as IContainerAccessor).Container;
container.Release(handler);
}
}
}
{
private static readonly ILog log = LogManager.GetLogger(typeof(PageHandler));
IHttpHandler Members#region IHttpHandler Members
/**//// <summary>
/// Process the aspx request. This means (eventually) rewriting the url and registering the page
/// in the container.
/// </summary>
/// <param name="context"></param>
public void ProcessRequest(HttpContext context)
{
string rawUrl = context.Request.RawUrl;
log.Info("Starting request for " + rawUrl);
DateTime startTime = DateTime.Now;
// Rewrite url
//UrlRewriter urlRewriter = new UrlRewriter(context);
//string rewrittenUrl = urlRewriter.RewriteUrl(rawUrl);
// Obtain the handler for the current page
string aspxPagePath = rawUrl.Substring(0, rawUrl.IndexOf(".aspx") + 5);
IHttpHandler handler = PageParser.GetCompiledPageInstance(aspxPagePath, null, context);
// Register the page in the container
handler = RegisterAspxPage(handler, context);
// Process the page just like any other aspx page
handler.ProcessRequest(context);
// Remove the page from the container
RemoveAspxPage(handler, context);
// Log duration
TimeSpan duration = DateTime.Now - startTime;
log.Info(String.Format("Request finshed. Total duration: {0} ms.", duration.Milliseconds));
}
/**//// <summary>
///
/// </summary>
public bool IsReusable
{
get { return true; }
}
#endregion
private IHttpHandler RegisterAspxPage(IHttpHandler handler, HttpContext context)
{
if (handler is IRuixinPage)
{
string pageKey = Guid.NewGuid().ToString();
IWindsorContainer container = ContainerAccessorUtil.GetContainer();
container.Kernel.AddComponent(pageKey, handler.GetType());
if (container.Kernel.HasComponent(handler.GetType()))
{
IHttpHandler newHandler = (IHttpHandler)container[handler.GetType()];
handler = newHandler;
}
}
return handler;
}
private void RemoveAspxPage(IHttpHandler handler, HttpContext context)
{
if (handler is IRuixinPage)
{
IWindsorContainer container = (context.ApplicationInstance as IContainerAccessor).Container;
container.Release(handler);
}
}
}
这样在我们的后置代码类中就可以通过公开属性的方式来获取容器中的服务。
发表评论
-
IBatisNet系列-介绍篇
2006-03-25 14:56 981IBatisNET从名字就可以看出,他是从Java的IBati ... -
IBatisNet系列二-QuickStart篇
2006-03-25 14:59 936本节我参考官方网站上的Quick Start Guide,网址 ... -
IBatisNet系列-执行存储过程
2006-03-25 15:24 1123映射xml文件书写如下 <?xml version=& ... -
IBatis.Net系列-Mapped Statements的语法
2006-03-29 15:25 852语法:<statement id="state ... -
Castle- Startable Facility 简单理解
2006-03-30 17:56 842最近在研究Castle开源项目,就下午学习Startable ... -
IBatis.Net系列-多参数的SQL语句的配置
2006-04-15 12:15 1419由于本人对IBatis.Net的并不是很精通,也是在边学边工作 ... -
MonoRail学习-介绍篇
2006-04-24 10:22 722刚刚结束了使用Castle MonoRail的一个项目,所以想 ... -
MonoRail学习-入门实例篇
2006-04-24 11:51 1270MonoRail学习目录实例代码下载:/Files/maply ... -
MonoRails学习- Layouts
2006-04-24 12:14 790MonoRail学习目录实例代码:/Files/maplye/ ... -
MonoRail学习-Component
2006-04-24 13:30 693MonoRail学习目录在我们asp.net中,经常有界面代码 ... -
Which Style of Workflow When?[转]
2006-05-24 16:58 719http://blogs.msdn.com/davegreen ... -
WF in Asp.Net
2006-05-26 11:19 935这几天在研究Windows Workflow Foundati ... -
WF学习-Console Workflow Application
2006-05-26 16:51 888环境必备:.NET Framework 2.0VS.Net 2 ... -
WF学习-WorkFLow中的参数
2006-05-27 15:46 1115系统和WorkFlow交互有很多种方式,今天介绍的是通过Wor ... -
Castle ActiveRecord.chm
2006-05-31 18:06 723收集 TerryLee's Tech Space 和 SHY5 ... -
使用UserControl时,在Page中定义方式
2006-06-24 10:02 878我们通常,会将一些通用的页面模块作成用户控件,然后加载到页面中 ... -
ASP.Net2.0中自定义控件在page中的注册
2006-06-24 14:15 787今天在网上看到ASP.Net 2.0中注册自定义控件的好方法, ... -
Asp.Net中NHiernate的Session的管理
2006-06-26 11:08 644NHibernate中的Session,在我的理解似乎就相当于 ... -
GridView使用一些记录
2006-06-30 10:45 911现在有个项目开始使用ASP.NET 2.0开发,在开发过程中关 ... -
NHiernate中自定义Generator
2006-07-04 13:50 876我的这个自定义的Generator设置如下: <g ...
相关推荐
castle项目的aop框架。 全英文教程。覆盖了项目中的大部分接口和类 面向切面编程
在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行分页 通过作业,定时同步两个数据库 SQLSERVER高级注入技巧 利用反射实现ASP.NET控件和...
《ASP.NET设计模式》:演示如何使用GOF设计模式来改进ASP.NET代码说明如何在企业级ASP.NET站点中采用Fowler的企业模式详述如何对ASP.NET应用程序进行分层以及如何分离关注点和职责详述如何采用JQuery、JSON以及WCF...
Throughout, you’ll work with open source projects that complement ASP.NET MVC, including NHibernate, MVCContrib, and Castle Windsor Container. These tools speed up developing database components, ...
asp.net mvc2.0+前台Jquery+IbatisNet+Castle 自己做的Demo,放在网上存起来~供一起学习参考,内有sql数据库脚本
利用Nhibernet+Castle以及AOP+IOC...实现俩俩PK时投票,可以把这个比较当作比较综合的框架来学习Nhaibernet和Castle.net。数据库时需要自己创建一个数据库,名称和配置文件中一致,然后运行后自动通过模型创建数据表。
如果你想使用Castle.ActiveRecord,但又不想使用.NET Framework 3.0/3.5/4.0或更高版本,那么这个就是你所需要的,For .NET FrameWork 2.0,我整理了好久,自己从官方一个资源库找到并整理好的一套。 -------------...
商城系统 ... 注意sql server类和access数据库可以使用vs自带的数据库启动,不需要安装庞大的sql安装包。 ...admin 123456 ... 积分管理模块 (添加余额和积分规则,...Castle+Asp.net mvc L.ShopVIPSystem.Domain 实体类 L.Sh
商城系统 ... 注意sql server类和access数据库可以使用vs自带的数据库启动,不需要安装庞大的sql安装包。 ...admin 123456 ... 积分管理模块 (添加余额和积分规则,...Castle+Asp.net mvc L.ShopVIPSystem.Domain 实体类 L.Sh
asp.net RSA 私钥加密公钥解密 能解 php Java 实现RSA加密互通 ,PKCS#8 格式的的密钥文件 ,公钥解密 依赖于BouncyCastle.Crypto.dll
asp.net mvc2.0+前台Jquery+IbatisNet+Castle 自己做的Demo,放在网上存起来~供一起学习参考,内有sql数据库脚本
BouncyCastle 加密算法.net组件 版本10.0
ASP.NET MVC implements the Model-View-Controller pattern on the ASP.NET runtime. It works well with open source projects like NHibernate, Castle, StructureMap, AutoMapper, and MvcContrib. ASP.NET MVC...
ASP.NET MVC 是微软官方提供的以MVC模式为基础的ASP.NET Web应用程序(Web Application)框架,它由Castle的MonoRail而来。ASP.NET MVC 4已经正式发布,并内置于Visual Studio 2012,新版本ASP.NET MVC版本新增了手机...
虽然已经非常熟悉这种架构模式,但是放在ASP.NET环境中,他不能充分利用ASP.NET的优势,我们更清晰与一种既有MVC架构,同时能充分发挥ASP.NET优点的架构,查阅大量资料之后,最终选择了Castle + iBastisNet + Castle...
The ASP.NET MVC Framework was the vision of Scott Guthrie in early 2007. With a prototype demonstration in late 2007 and a key hire of Phil Haack as the Senior Program Manager of the feature team, Mr....
Asp.net Mvc 是微软官方提供的Mvc模式编写Asp.netWeb应用程序的一个框架,它由Castle的MonoRail而来.目前已经历经数个版本
此系统基于asp.net的图书管理系统,是利用castle开发的。另外还附上数据库。只是数据库表和表之间没有关联起来。设计的时候没有设计好。不过有兴趣的朋友还是可以看看。
Castle是针对.NET平台的一个开源项目,从数据访问框架ORM到IOC容器,再到WEB层的MVC框架、AOP,基本包括了整个开发过程中的所有东西,为我们快速的构建企业级的应用程序提供了很好的服务。4月份以来,Terrylee写了一...