かずきのBlog

C#やJavaやRubyとメモ書き

目次

Blog 利用状況

ニュース

わんくまBlogが不安定になったため、前に書いてたはてなダイアリーにメインを移動します。
かずきのBlog@Hatena
技術的なネタは、こちらにも、はてなへのリンクという形で掲載しますが、雑多ネタははてなダイアリーだけに掲載することが多いと思います。
コメント
プログラマ的自己紹介
お気に入りのツール/IDE
プロフィール
経歴
広告
アクセサリ

書庫

日記カテゴリ

[EntLib][C#]IUnityContainerを拡張してしまおうぜ

Unityは、結構お気に入りになりそうな感じなんだけど、あまりいじれてない。UnityContainerは、UnityContainerExtensionというものを使って簡単に拡張?出来るようになっている。

ということで、早速やってみた。

下準備として、IUnityContainerに登録するクラス群を定義する。とりあえず単純な足し算引き算を行うクラスを作った。

    interface ICalc
    {
        int Execute(int lhs, int rhs);
    }
    class AddCalc : ICalc
    {
        public int Execute(int lhs, int rhs)
        {
            return lhs + rhs;
        }
    }
    class MinusCalc : ICalc
    {
        public int Execute(int lhs, int rhs)
        {
            return lhs - rhs;
        }
    }

計算というものをICalcとしてインターフェースにしてみたよ。まぁ、サンプルなんでこんなもんで。次に、UnityContainerExtensionを継承したクラスを定義する。Initializeというメソッドをオーバーライドすることになるので、ここにコンテナの初期化処理を書く。今回は、AddUnityContainerExtensionとMinusUnityContainerExtensionを定義した。
上で作ったICalcやAddCalc,MinusCalcをUnityContainerに追加するためのクラスとして使える。

    // 計算といえば足し算用でしょ拡張
    class AddUnityContainerExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            Container.RegisterType<ICalc, AddCalc>();
        }
    }

    // 計算といえば引き算でしょ拡張
    class MinusUnityContainerExtension : UnityContainerExtension
    {
        protected override void Initialize()
        {
            Container.RegisterType<ICalc, MinusCalc>();
        }
    }

UnityContainerExtensionにはContainerというプロパティが定義されているので、Containerプロパティを通じてUnityContainerにアクセスしている。ここで作ったUnityContainerExtensionは、AddNewExtensionメソッドを使ってUnityContainerに設定できるようになっている。

要は、各々のExtensionを追加することで、関連のあるものをUnityContainerに一括で登録できてしまうといったものみたいだ。早速作ったものを使ってみようと思う。

その前に、ICalcを使うクラスを定義してみた。

class CalcClient
{
    [Dependency]
    public ICalc Calc { get; set; }

    public void Execute()
    {
        Console.WriteLine(Calc.Execute(10, 3));
    }
}

計算して出力するだけ!シンプル。

長かったけど、やっとUnityContainerを使うよ!といってもコードは数行…

// コンテナを作って
var c = new UnityContainer();
// 足し算の計算用に拡張?して
c.AddNewExtension<AddUnityContainerExtension>();

// CalcClientをコンテナ経由で取得
c.RegisterType<CalcClient, CalcClient>();
var client = c.Resolve<CalcClient>();
client.Execute(); // -> 13

これを実行すると、10 + 3なので13と表示される。AddNewExtensionの部分をMinusUnityContainerExtensionに変更すると7 と表示される。

// コンテナを作って
var c = new UnityContainer();
// 引き算の計算用に拡張?して
c.AddNewExtension<MinusUnityContainerExtension>();

// CalcClientをコンテナ経由で取得
c.RegisterType<CalcClient, CalcClient>();
var client = c.Resolve<CalcClient>();
client.Execute(); // -> 7

今回のサンプルでは、一種類しかクラスを登録していなかったからありがたみは無いかもしれないけど、たとえばフレームワーク等で、関連のあるクラス群を一気に登録してしまおうというときにいいかもしれない。

投稿日時 : 2008年7月31日 23:17

Feedback

# [C#][EntLib]UnityContainerの拡張について 2008/11/05 13:08 かずきのBlog

[C#][EntLib]UnityContainerの拡張について

# 2012 moncler jacket sale 2012/12/08 7:13 http://2012monclerdownjacket.webs.com/

Excellent web site. Plenty of useful info here. I am sending it to some friends ans also sharing in delicious. And certainly, thanks to your effort!

# longchamp le pliage 2012/12/14 20:44 http://www.soldesacslongchamp.info/category/sacs-l

gripping waters of opinions bursting through your photos.

# longchamp le pliage 2012/12/15 16:04 http://www.longchampbagoutlet.info/category/longch

Think it similar to a journal or perhaps magazine involving general interest proper to look over. Funny, witty, controversial, entertaining, useful, acerbic, thoughtful, serious, curious, unexpected comebacks tend to be welcome.

# sac burberry soldes 2012/12/15 23:03 http://www.sacburberryecharpe.fr/category/sac-burb

make these folks red having a yellow deer!!

# longchamp pliage hobo 2012/12/16 17:58 http://www.soldesacslongchamp.info/category/sac-lo

The only those who would search good making use of these fugly things could be Ferrari pit crew while in the pits:D

# burberry london 2012/12/17 2:56 http://www.ukburberryoutlet.info/category/burberry

Our pool should be fed together with those photopages that you choose to consider really worth becoming section of the "Best Brief review Collection".

# isabelle marant 2012/12/18 2:12 http://isabelmarantbottes.webnode.fr

I truly like em. It's not wish you don these out travelling town men and women.

# isabel marant baskets 2012/12/18 20:43 http://isabellemarantbaskets.webnode.fr

Thus, our shelves land up filled with stuffs that we delight in.

# michael kors bags 2012/12/21 6:34 http://michaelkorsuk.webeden.co.uk

I would enjoy take a closer look at some of that memorabilia!

# tt6262.com 2013/03/04 5:02 http://tt6262.com/

To the world you'll probably be a single person, but nonetheless , to customer you'll probably be the entire world. tt6262.com http://tt6262.com/

# air jordan retro 7 olympic 2013/03/04 23:18 http://www.jordanretro7air.com/

Can't connect with others who're relaxing to be with. Socialize that will trigger players to prise your own self out. air jordan retro 7 olympic http://www.jordanretro7air.com/

# casquette wati b 2013/03/14 8:53 http://www.a77.fr/

May be Our god hopes united states in order to meet a selection of inaccurate consumers preceding joining a good choice, to make certain that when i at long last meet the people, we can aren't able to indeed be head over heels. casquette wati b http://www.a77.fr/

# paristreet 2013/03/15 9:04 http://www.a88.fr/

Can't it's the perfect time whorrr re more comfortable to get along with. Socialize that will amount of force one to prise your body it. paristreet http://www.a88.fr/

# casquette swagg 2013/03/16 8:52 http://www.b77.fr/

Adoration is in fact weak in inception, it gets much better with age if it's the right way raised on. casquette swagg http://www.b77.fr/

# casquette wati b 2013/03/16 10:00 http://www.a44.fr/

Will not bring all of your bliss to a single significantly less happy compared to what all by yourself. casquette wati b http://www.a44.fr/

# destockchine 2013/03/24 0:37 http://d77.fr/

I need happened thanks to your identity, unfortunately thanks to who exactly I'm just lake in the morning along. destockchine http://d77.fr/

# casquette baseball 2013/03/24 11:21 http://e11.fr/

Have a passion for is going to be primary happy not to mention positive solution associated with individuals existence. casquette baseball http://e11.fr/

# Destockage vetement 2013/04/03 7:05 http://www.ruenike.com/autres-c-25.html/

It's possible Who prefers united states in order to reach two or three bad folk earlier achieving the correct one, to ensure whenever we inevitably match the man or woman, analysis are able to sometimes be thankful. Destockage vetement http://www.ruenike.com/autres-c-25.html/

# brandalley 2013/04/07 12:00 http://rueree.com/

Potentially Who preferences most of us in order to reach a number misguided families ahead of getting to know the right choice, make certain when i subsequently meet the one, heading to find out how to come to be relieved. brandalley http://rueree.com/

# coachoutletstore77.com 2013/04/07 17:11 http://www.coachoutletstore77.com/

Joy and happiness is the essence have the ability to dans le but de concerned with other consumers with out getting a a small amount of reduces concerned with that you are. coachoutletstore77.com http://www.coachoutletstore77.com/

# veste cuir 2013/04/07 18:02 http://www.marquefr.fr/

A that you choose to invest in by working with reveals might be purchased from you and your family. veste cuir http://www.marquefr.fr/

# qaGlcylxYSUmViJSA 2014/08/05 6:53 http://crorkz.com/

bgxp9A Really informative post. Great.

# re: [EntLib][C#]IUnityContainer?????????? 2021/08/09 15:56 hydroxychloroq

choloroquine https://chloroquineorigin.com/# hydroxychloroquine cures

# Test, just a test 2022/12/13 15:39 candipharm com

canadian customs pills vitamins http://candipharm.com/#

タイトル
名前
Url
コメント