present@わんくま

本家はこちら

目次

Blog 利用状況

ニュース

アクセサリ

書庫

日記カテゴリ

ギャラリ

リンク

UnityContainer を拡張する

はじめに

UnityContainer を拡張するために UnityContainerExtension クラスが用意されています。 このクラスを継承して独自の Extension を作れば、UnityContainer の挙動を拡張できます。

例として、先日のサンプルを書き換えてみます。

独自の Extension を作成

public class AnimalExtension : UnityContainerExtension
{
    protected override void Initialize()
    {
        // 動物クラスを複数まとめて登録する
        Container.RegisterType<IAnimal, Dog>("Dog");
        Container.RegisterType<IAnimal, Cat>("Cat");
        Container.RegisterType<IAnimal, Wankuma>("Wankuma");
    }
}

Container だけでなく、Policies や Strategies にもアクセスできます。 Policies や Strategies は後日調べる予定。

作成した Extension を UnityContainer に登録する

static void Main(string[] args)
{
    // DI コンテナ生成
    IUnityContainer container = new UnityContainer();

    // Extension を登録
    container.AddNewExtension<AnimalExtension>();

    // 依存性注入済みのインスタンスを生成
    Person person = container.Resolve<Person>();

    person.CallPet();
}

もちろん構成ファイルで Extension を指定可能

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration,Version=1.1.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
    </configSections>
    <unity>
        <containers>
            <container name="Sample">
                <extensions>
                    <add type="ExtensionSample.AnimalExtension, ExtensionSample"/>
                </extensions>
            </container>
        </containers>
    </unity>
</configuration>

まとめ

自社フレームワークなんかに UnityContainer を採用するなら、 まとめて型を登録できるように Extension を用意した方が良さそうです。 Enterprise Library の各 Application Block も、 構成ファイルの内容を元に型を登録する Extension を提供していますしね。

投稿日時 : 2008年11月4日 11:40

コメントを追加

# ivermectin 6mg 2021/09/28 16:05 MarvinLic

stromectol cvs http://stromectolfive.online# stromectol 3 mg

# ivermectin 3 mg 2021/11/01 1:43 DelbertBup

ivermectin 10 mg https://stromectolivermectin19.com/# ivermectin 6 mg tablets
ivermectin 50 mg

# ivermectin 3 mg tabs 2021/11/03 18:04 DelbertBup

buy ivermectin uk http://stromectolivermectin19.online# ivermectin price canada
ivermectin generic cream

# online prednisone 5mg https://prednisonesnw.com/#
buy prednisone online fast shipping 2021/11/13 9:40 Prednisone

online prednisone 5mg https://prednisonesnw.com/#
buy prednisone online fast shipping

# how many sildenafil 20mg can i take https://viasild24.online/
2021/12/07 19:24 Nyusjdh

how many sildenafil 20mg can i take https://viasild24.online/

# sildenafil 20 mg tablet uses 2021/12/10 14:29 JamesDat

http://iverstrom24.online/# stromectol human dosage

# bimatoprost 2021/12/12 5:52 Travislyday

https://plaquenils.com/ plaquenil 200 mg tablet

# bimatoprost buy online usa 2021/12/13 1:19 Travislyday

https://baricitinibrx.com/ where to buy baricitinib

# best place to buy careprost https://bimatoprostrx.com
careprost bimatoprost ophthalmic best price
2021/12/13 16:09 Hksfnjkh

best place to buy careprost https://bimatoprostrx.com
careprost bimatoprost ophthalmic best price

# bimatoprost generic 2021/12/13 21:05 Travislyday

http://plaquenils.com/ plaquenil 200mg tablet cost

# careprost for sale 2021/12/14 16:34 Travislyday

http://bimatoprostrx.com/ bimatoprost buy

# buy bimatoprost 2021/12/16 5:33 Travislyday

http://baricitinibrx.com/ barinat

# stromectol tablets for humans 2021/12/17 22:10 Eliastib

hxltzn https://stromectolr.com ivermectin humans

# stromectol 3 mg price 2021/12/18 23:08 Eliastib

lnowxr https://stromectolr.com stromectol 3mg cost

# ivermectin for covid 2021 update 2022/03/16 3:24 Tommiegathe

https://stromectolfastusa.com/# ivermectin half life

# buy ivermectin online 2022/03/16 21:58 Tommiegathe

https://stromectolfastusa.com/# ivermectin injectable dose for goats

# finasteride without prescription https://finasteridemen.com/
2022/05/11 23:51 Finasteride

finasteride without prescription https://finasteridemen.com/

# doxycycline tetracycline https://buydoxycycline.icu/ 2022/10/08 17:15 Doxycycline

doxycycline tetracycline https://buydoxycycline.icu/

# What side effects can this medication cause? What side effects can this medication cause?
https://edonlinefast.com
Long-Term Effects. Prescription Drug Information, Interactions & Side. 2023/02/17 11:50 EdPills

What side effects can this medication cause? What side effects can this medication cause?
https://edonlinefast.com
Long-Term Effects. Prescription Drug Information, Interactions & Side.

# canadian meds no prescription https://pillswithoutprescription.pro/# 2023/05/16 11:33 PillsPro

canadian meds no prescription https://pillswithoutprescription.pro/#

# ed meds online https://edpillsotc.store/# - erection pills 2023/10/08 2:51 EdPills

ed meds online https://edpillsotc.store/# - erection pills

# plavix medication https://plavix.guru/ buy clopidogrel bisulfate 2023/10/24 1:59 Plavixxx

plavix medication https://plavix.guru/ buy clopidogrel bisulfate

# paxlovid cost without insurance https://paxlovid.bid/ paxlovid buy 2023/10/26 0:38 Paxlovid

paxlovid cost without insurance https://paxlovid.bid/ paxlovid buy

# ed pills that work https://edpills.tech/# buy ed pills online 2023/12/23 9:32 EdPills

ed pills that work https://edpills.tech/# buy ed pills online

# no prescription online prednisone https://prednisonepharm.store/ no prescription online prednisone 2024/01/20 18:04 Prednisone

no prescription online prednisone https://prednisonepharm.store/ no prescription online prednisone

# abella danger video https://abelladanger.online/ Abella Danger
2024/03/06 11:18 Adella

abella danger video https://abelladanger.online/ Abella Danger

タイトル
名前
URL
コメント