StructureMap經典的IoC/DI容器

来源:http://www.cnblogs.com/yangsofter/archive/2017/06/12/StructureMap.html
-Advertisement-
Play Games

文檔: "http://structuremap.github.io/documentation/" 下載: "https://sourceforge.net/projects/structuremap/" GitHub: "https://github.com/structuremap/struc ...


StructureMap是一款很老的IoC/DI容器,從2004年.NET 1.1支持至今。

一個使用例子

    //創建業務介面
    public interface IDispatchService { }
    public interface ICourier { }
    public interface IPaymentGateway { }
    public interface IPaymentMerchant { }

    //介面的實現
    public class DispacthService : IDispatchService
    {
        private ICourier _courier;
        public DispacthService(ICourier courier)
        {
            _courier = courier;
        }
        public override string ToString()
        {
            return _courier.ToString();
        }
    }
    public class FedExCourier : ICourier { }
    public class StreamLinePaymentMerchant : IPaymentMerchant { }
    public class PaymentGateway : IPaymentGateway
    {
        private IPaymentMerchant _paymentMerchant;
        public PaymentGateway(IPaymentMerchant paymentMerchant)
        {
            _paymentMerchant = paymentMerchant;
        }
        public override string ToString()
        {
            return _paymentMerchant.ToString();
        }
    }

    //業務使用
    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());
        }
    }

    //配置依賴關係
    public class BootStrapper
    {
        public static void ConfigureStructureMap()
        {
            ObjectFactory.Initialize(x => x.AddRegistry<ModelRegistry>());
        }
    }

    public class ModelRegistry : Registry
    {
        public ModelRegistry()
        {
            For<IPaymentGateway>().Use<PaymentGateway>();
            For<IPaymentMerchant>().Use<StreamLinePaymentMerchant>();
            For<IDispatchService>().Use<DispacthService>();
            For<ICourier>().Use<FedExCourier>();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BootStrapper.ConfigureStructureMap();//啟用配置


            OrderService orderService = ObjectFactory.GetInstance<OrderService>();
            Console.WriteLine(orderService.ToString());

            IPaymentGateway paymentGateway= ObjectFactory.GetInstance<PaymentGateway>();
            Console.WriteLine(paymentGateway);

            Console.ReadKey();
        }
    }

文檔:http://structuremap.github.io/documentation/
下載:https://sourceforge.net/projects/structuremap/
GitHub:https://github.com/structuremap/structuremap


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 自定義生成事件打開方式 通過指定自定義生成事件,可以在生成開始之前或在它完成之後自動運行命令。在Visual Studio中通過右鍵項目 》屬性 進入項目屬性菜單。 自定義生成事件的語法 生成事件遵循與 DOS 命令相同的語法。如 在啟動調試時在某個目錄下創建文件夾 通過內置的巨集列表可以更快速的輸入 ...
  • 作者:Parry 出處:http://www.cnblogs.com/parry/ 本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 這是一篇新手入門的一篇非常不錯的文章,強烈推薦!聯繫我加微信:jkxx123321 ...
  • 列出所有索引 現在來看看我們的索引 返回內容如下: 可以看到在集群中有一個索引 創建索引 現在讓我們創建一個名叫 customer 的索引,然後再次列出所有的索引 執行第一行返回以下內容,這裡我們使用PUT謂詞創建了一個名叫 customer 的索引,在後面跟上 pretty 表示如果有數據返回的話 ...
  • 一:中文資源 先去Nop官網,下載3.9中文包,此中文包翻譯不全,在定製的過程中,可以慢慢修改。官網地址 www.nopcommerce.com。 進去後臺 Configuration -> Languages 添加一條簡體中文的記錄,保存後導入中文包,頁面頂部切換語言即可。 二:UI漢化 Libr ...
  • “微服務架構(Microservice Architecture)”一詞在過去幾年裡廣泛的傳播,它用於描述一種設計應用程式的特別方式,作為一套獨立可部署的服務。目前,這種架構方式還沒有準確的定義,但是在圍繞業務能力的組織、自動部署(automated deployment)、端智能(intellig ...
  • 從很早時候,聽老師說 select * from table 比 select a,b,c,d from table 要慢很多。3年來從未測試。 今天沒事測一測, 不測不知道,一測嚇一跳。 當然 以下純屬測試,並未運用到實際項目中。如有什麼意見和建議,請指教。 測試代碼如下: ...
  • 反射是.net中非常有用的特性。 什麼是反射 可以用一句話來概括就是:基於程式集和元數據,可以動態創建某個類型的實例,調用方法,和訪問對象成員的一種能力。 為什麼要使用反射 在編譯時無法確定要調用的對象的時候,就不得不使用反射。 反射的應用場景 最常見的應用場景有: 1,ORM框架,因為它要面對的是 ...
  • 下載Redis https://github.com/MSOpenTech/redis 修改Redis.windows.conf,如果不修改,遠程不能訪問Redis 將bind 127.0.0.1 改成了bind 0.0.0.0。註意:進入生產環境時候,要啟用密碼,否則會是Redis漏洞. prot... ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...