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 ;}}}}