Microsoft enterprise database 5.0 Study Notes (9) ASP. NET web applications

Source: Internet
Author: User

In ASP. NET web forms applications, the recommended method is to save the containerApplicationThe global status provided by the dictionary object. When needed, you can access the container, or even use the HTTP module to automatically inject controls on the page.

Generally, you should useApplicationDictionary object to store a single instance of the container. You may decide to create sub-containers of the main container and store them in the session object of each user, or even every request, to register custom types and mappings in these sub-containers. However, this may reduce the performance of the application and avoid creating additional containers as much as possible.

Technical Methods and restrictions for instantiating containers for ASP. NET applications:

1. Basic method. Suitable for small projects

2. recommended method for dependency injection. The page control can be automatically injected at runtime.

3. Restrictions and alternative approaches.
Basic Method

Use the global. asax FileApplication_startEvent to create and build a container.Protected void application_start (Object sender, eventargs e) <br/>{< br/> application. lock (); <br/> try <br/> {<br/> var mycontainer = application ["entlibcontainer"] As iunitycontainer; <br/> If (mycontainer = NULL) <br/>{< br/> mycontainer = new unitycontainer (); <br/> mycontainer. addextension (New enterpriselibrarycoreextension (); <br/> // Add your own regim registrations and mappings here as required <br/> application ["entlibcontainer"] = mycontainer; <br/>}< br/> finally </P> <p >{< br/> application. unlock (); <br/>}< br/>

 

Recommended Method for dependency Injection

Although the method provided above provides a way for you to access the container, there are some restrictions. This means that you must write code to process the instances of the classes you need, and reduce the discoverability and testability of the Code. If you use the unity container, a better way is to useBuildupMethod To build dependencies. This means you only need to care about the appropriate attributes of your space and class applications, or configure injection registration and ing in the container. Inject the required Enterprise Library objects and your own classes at runtime.

One way is to create an HTTP module and automatically inject all dependencies when a page is loaded. To do this, you need an HTTP module to execute code During page initialization and execution to fill in dependencies. You can also use global. the application_start method in asax is used to create and fill the container. An extension method of the httpapplicationstate class exposes the container to the application code. once these items are created, you only need to apply a row in the configuration to enable automatic dependency injection for controls and classes in your application.

Prepare an ASP. NET application for automatic dependency Injection

1.Create a new ASP. net http module class in the project (for example, nameUnityhttpmodule) CapturePrerequesthandlerexecuteEvent and execute the code to traverse the full control tree of the current page request, and apply unityBuildupMethod to each control.

2. Edit the Web. config file of the application to add a new HTTP module and put it before other HTTP modules, so that it can be executed first. The following describes how to add the HTTP module in the previous step.<Httpmodules> <br/> <Add name = "unitymodule" type = "unity. web. unityhttpmodule, unity. web "/> <br/>... other HTTP modules defined here <br/> </pttpmodules> <br/>

 

 

3. Create a class implementationHttpapplicationstate (provides a method to access the container) extension method. This method should create a unity container and store it in the application dictionary,If yes, its instance is returned.

4.Add a global application class file (in global. asax) to your application and add it to global. asax. CS.Application_start event processing code. The following shows how to add registration to a specific type,
For example, imyservice,Customerservice,Dataservice
protected void Application_Start(object sender, EventArgs e){  IUnityContainer myContainer = Application.GetContainer();  myContainer.AddExtension(new EnterpriseLibraryCoreExtension());   myContainer.RegisterType<IMyService, CustomerService>();  myContainer.RegisterType<IMyService, DataService>("CustomerData",                           new ContainerControlledLifetimeManager());  myContainer.RegisterInstance("myArray", new string[] {"Item 1", "Item 2" });}
Restrictions and alternatives

The suggested technology does not automatically construct controls and classes injected to the page. It usesBuildupMethod, because ASP. NET has created control and class instances. This means that automatic construction injection will not happen. Instead, you can use property (setter) injection, or create controls and class methods for the type and define parameters for resolution, and then define them as injectionmethods registered using properties or containers. This constructs a latency structure. In the entire container, unity determines the parameter type and calls the method. Of course, you can also store an instance to process objects in your code.

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.