The classic Ioc/di container--structuremap

Source: Internet
Author: User
StructureMap is an old Ioc/di container that has been supported since 2004. NET 1.1.

An example of use

    Create business Interface public interface Idispatchservice {} public interface Icourier {} public interface Ipaymentgateway {} public interface Ipaymentmerchant {}//Interface implementation public class Dispacthservice:idispatchservice {PR        Ivate Icourier _courier;        Public Dispacthservice (Icourier Courier) {_courier = Courier; } public override string ToString () {return _courier.        ToString (); }} public class Fedexcourier:icourier {} public class Streamlinepaymentmerchant:ipaymentmerchant {} pub        Lic class Paymentgateway:ipaymentgateway {private Ipaymentmerchant _paymentmerchant;        Public Paymentgateway (Ipaymentmerchant paymentmerchant) {_paymentmerchant = paymentmerchant;        } public override string ToString () {return _paymentmerchant.tostring (); }}//Business using public class OrderService {private Ipaymentgateway _payMentgateway;        Private Idispatchservice _dispacthservice; Public OrderService (Ipaymentgateway Paymentgateway, Idispatchservice dispacthservice) {_paymentgateway            = Paymentgateway;        _dispacthservice = Dispacthservice; } public override string ToString () {return string.        Format ("ipaymentgateway:{0} Idispatchservice:{1}", _paymentgateway.tostring (), _dispacthservice.tostring ());            }}//config dependency public class BootStrapper {public static void Configurestructuremap () {        Objectfactory.initialize (x = x.addregistry<modelregistry> ()); }} public class Modelregistry:registry {public Modelregistry () {for<ipaymentgate Way> ().            Use<paymentgateway> (); For<ipaymentmerchant> ().            Use<streamlinepaymentmerchant> (); For<idispatchservice> ().            Use<dispacthservice> (); for<icourier> ().        Use<fedexcourier> (); }} class Program {static void Main (string[] args) {Bootstrapper.configurestructuremap            ();//enable configuration OrderService OrderService = objectfactory.getinstance<orderservice> ();            Console.WriteLine (Orderservice.tostring ());            Ipaymentgateway paymentgateway= objectfactory.getinstance<paymentgateway> ();            Console.WriteLine (Paymentgateway);        Console.readkey (); }    }
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.