Castle study Note 3: Get the castle container on the ASP. NET page

Source: Internet
Author: User

Reference: http://shanyou.cnblogs.com/archive/2005/10/28/264103.html

When using the castle IOC container in the project, you can obtain the castle container on the ASP. NET page as follows:

1. Implement icontaineraccessor in global. asax. CS

Global. asax

Public   Class Global: system. Web. httpapplication, icontaineraccessor
{
Private   Static Windsorcontainer container;
Protected   Void Application_start ( Object Sender, eventargs E)
{
// Cachemanager. loadcachefile (); Cache Management class
Container =   New Windsorcontainer ( New Xmlinterpreter ( New Configresource ()));
}
Protected   Void Application_end ( Object Sender, eventargs E)
{

}
PublicIwindsorcontainer container
{
Get{ReturnContainer ;}
}
}

2. Get the container instance

Containeraccessorutil

Public   Class Containeraccessorutil
{
Private Containeraccessorutil ()
{
}

///   <Summary>
/// Obtain all instances in the container
///   </Summary>
///   <Returns> List of all instances </Returns>
Public   Static Iwindsorcontainer getcontainer ()
{
Icontaineraccessor containeraccessor = Httpcontext. Current. applicationinstance As Icontaineraccessor;

If (Containeraccessor =   Null )
{
Throw   New Exception ( " You must extend the httpapplication in your web project "   +
" And implement the icontaineraccessor to properly expose your container instance "   +   " /N "   +
" You must implement the icontaineraccessor interface in httpapplication to expose container attributes. " );
}

Iwindsorcontainer container=Containeraccessor. ContainerAsIwindsorcontainer;

If (Container =   Null )
{
Throw   New Exception ( " The container seems to be unavailable in "   +
" Your httpapplication subclass "   +   " /N "   +   " Httpapplication cannot get the container instance " );
}

ReturnContainer;
}
}

3. Get the container instance through containeraccessorutil. getcontainer () in the base class of the specific page.

For example:

Getcontainer

  Public Isqlmapper sqlmap = (Containeraccessorutil. getcontainer ())[ " Sqlserversqlmap " ] As Isqlmapper;

Public StaticIcontractbiz=Containeraccessorutil. getcontainer ()["Cclog. contractbiz"]AsIcontractbiz;

Ibasicinfobiz=Containeraccessorutil. getcontainer ()["Cclog. basicinfobiz"]AsIbasicinfobiz;

//...

To assign ibatisnet to Castle for management, the isqlmapper instance must also be obtained from the castle container so that castle can truly manage ibatisnet.

How to obtain an isqlmapper instance from a container: basesqlmapdao. CS

Public Isqlmapper sqlmap = (Containeraccessorutil. getcontainer ())[ " Sqlserversqlmap " ] As Isqlmapper;

"Sqlserversqlmap" is specified when we configure ibatisnet facility.

< Facility ID = "Ibatis" Type = "Castle. Facilities. ibatisnetintegration. ibatisnetfacility, Castle. Facilities. ibatisnetintegration"   >
  < Sqlmap ID = "Sqlserversqlmap" Config = "Sqlmap. config"   />
</ Facility >

Appendix:

About the global. asax file:

The global. asax file is sometimes called an ASP. NET application.ProgramFile, provides a way to respond to application-level or module-level events at a central location. You can use this file to implement application security and other tasks.

Global. asax is located in the application root directory. The ASP. NET page framework can automatically identify any changes made to the Global. asax file.

Global. asax contains many events, which are often used for security and can be used to process application-level tasks, such as user authentication, application startup, and user session processing.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.