Use Microsoft. Practices. Unity to implement simple dependency Injection

Source: Internet
Author: User

Step 1: Write a front-endCode

 

 
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using itest; namespace testunity {public partial class _ default: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void button#click (Object sender, eventargs e) {iservice service = websingleton. applicationcontainer. resolve <iservice> (); If (service. userlogin (textbox1.text, textbox2.text) {response. write ("success ");}}}}
 
Step 2: Write an interface to provide the injection entry
Using system; using system. Collections. Generic; using system. LINQ; using system. Text; namespace itest {public interface iservice {bool userlogin (string uname, string PWD );}}
 
Step 3: implement this interface
 
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace itest {public class service: iservice {# region iservice member public bool userlogin (string uname, string PWD) {return true ;}# endregion }}
 
Step 4: Call the class of dependency Injection
Using system; using system. collections. generic; using system. LINQ; using system. text; using Microsoft. practices. unity; using system. web; namespace itest {public class websingleton {public static iunitycontainer applicationiner iner {get {return httpcontext. current. application ["Container"] As iunitycontainer;} set {httpcontext. current. application ["Container"] = value ;}}}}
 
 

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.