present@わんくま

本家はこちら

目次

Blog 利用状況

ニュース

アクセサリ

書庫

日記カテゴリ

ギャラリ

リンク

Unity を使ってコードで依存性を注入する

はじめに

前回は UnityContainer に登録したインスタンスを取り出すだけだったので、 今回は依存性注入の簡単なサンプルを作成してみました。

インタフェースを用意

public interface IAnimal
{
    // 鳴き声を出力する
    void Cry();
}

鳴き声を出力するメソッドだけのインタフェース。

インタフェースを実装するクラスを用意

// ネコ
public class Cat : IAnimal
{
    public void Cry()
    {
        Console.WriteLine("ニャ~");
    }
}

// イヌ
public class Dog : IAnimal
{
    public void Cry()
    {
        Console.WriteLine("バウ!");
    }
}

依存性を注入するクラスを用意

public class Person
{
    [Dependency("Dog")]
    public IAnimal Pet { get;set; }

    // ペットを呼ぶ
    public void CallPet()
    {
        Pet.Cry();
    }
}

Pet プロパティに Dependency 属性を付けています。 DI コンテナに登録した中で "Dog" に対応するインスタンスが、 このプロパティにセットされます。

依存性注入します!

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

    // 名前を指定してインスタンスを登録
    container.RegisterInstance<IAnimal>("Dog", new Dog());
    container.RegisterInstance<IAnimal>("Cat", new Cat());

    Person person = new Person();

    // 依存性を注入する
    person = container.BuildUp<Person>(person);

    // ペットを呼ぶ
    person.CallPet();

    Console.ReadLine();
}

このコードを実行すると

と出力されます。

Dependency 属性を変更

[Dependency("Cat")]
public IAnimal Pet { get;set; }

と変更すると

と出力されます。

最後に

今回はコードで依存性を注入しました。 属性を使った方法では、クラスを切り替えるにはコードを修正してリビルドする必要があるので、 使う場面は少ないでしょう。

次は構成ファイルを使って依存性を注入する予定。

投稿日時 : 2008年10月29日 11:43

コメントを追加

# re: Unity を使ってコードで依存性を注入する 2008/10/30 7:37 中博俊

>今回はコードで依存性を注入しました。属性を使った方法では、クラスを切り替えるにはコードを修正してリビルドする必要があるので、使う場面は少ないでしょう。

そんなことはないですよ。
設定ファイルなんて古いです。

# re: Unity を使ってコードで依存性を注入する 2008/10/30 9:41 なかむら

>設定ファイルなんて古いです。

そうなんですか…。

「設定ファイルはできるだけ書かない」という考えが今の主流なんでしょうか?
例えば Java の Seaser2 は「Less Configration」を主張しているようですし。

# re: Unity を使ってコードで依存性を注入する 2008/10/30 9:48 なかむら

×Seaser2 → ○Seasar2

# 構成ファイルを記述して依存性を注入する 2008/11/02 23:27 Nakamura Blog

構成ファイルを記述して依存性を注入する

# UnityContainer を拡張する 2008/11/04 11:40 Nakamura Blog

UnityContainer を拡張する

# UnityContainer を拡張する 2008/11/04 11:45 Nakamura Blog

UnityContainer を拡張する

# leilei123 2018/08/29 16:49 leilei3915

2018829 leilei3915
http://www.michaelkorsoutletsites.us.com
http://www.pandorasoutlet.us.com
http://www.canadagooseoutletsonline.us.com
http://www.pandoracharmsfactory.us.com
http://www.coachoutletss.us.com
http://www.christianlouboutin-uk.co.uk
http://www.michaelkorshandbagssale.us.org
http://www.michaelkorsoutlets.co.uk
http://www.pandora-jewelryclearance.us.com
http://www.katespadeoutlets.org
http://www.uggoutletsuggs.us.com
http://www.katespadeoutletinc.us.com
http://www.toryburchoutlet.us
http://www.asics-runningshoes.us.com
http://www.nhljerseyscom.us.com
http://www.michaelkorsoutletonlinesite.us.com
http://www.off--whiteclothing.us.com
http://www.cheapjordansfor-sale.us.com
http://www.michaelkorsoutletblackfriday.us.com
http://www.michaelkorsoutletfactoryus.us.com
http://www.goosecanada.name
http://www.cheapuggsboots.com.co
http://www.michaelkorsoutletfactorysale.us.com
http://www.raybansunglassesdesigner.us.com
http://www.nfljerseyswholesalenike.us.com
http://www.michaelkorsoutlet-store.us.org
http://www.canadagoosejacketscoat.us.com
http://www.canadagooseoutletcanadian.us.com
http://www.pandoraoutlets.us.com
http://www.pandoracharm.us.com
http://www.michaelkorsoutletk.us.com
http://www.ralphlauren-saleclearance.org.uk
http://www.oakleysunglasseswholesalec.us.com
http://www.katespadehandbagsinc.us.com
http://www.manoloblahnik.us.org
http://www.ferragamoshoessale.us.com
http://www.longchampoutletonlinestore.us.org
http://www.redbottomsshoes.us.org
http://www.uggoutletstores.ca
http://www.coachoutletonlineoutlet.us.com
http://www.valentinooutletstore.us.com
http://www.coachfactoryoutlet.com.co
http://www.coachoutletstoreonlines.com.co
http://www.michaelkorsoutlet-clearance.in.net
http://www.canadagooseoutletshop.us.com
http://www.nikeshoesuk.co.uk
http://www.michaelkorsoutletsclearance.us.com
http://www.true-religion.us.com
http://www.katespadeonlineoutlet.us.org
http://www.christianlouboutinoutletinc.us.com

# An outstanding share! I have just forwarded this onto a co-worker who has been doing a little research on this. And he in fact ordered me breakfast because I found it for him... lol. So allow me to reword this.... Thanks for the meal!! But yeah, thanks 2019/04/04 22:32 An outstanding share! I have just forwarded this o

An outstanding share! I have just forwarded this
onto a co-worker who has been doing a little research on this.
And he in fact ordered me breakfast because I found
it for him... lol. So allow me to reword this....
Thanks for the meal!! But yeah, thanks for spending the time
to talk about this issue here on your web page.

# I was recommended this website through my cousin. I'm not sure whether this publish is written by way of him as no one else recognize such special about my trouble. You're amazing! Thanks! 2019/05/01 17:12 I was recommended this website through my cousin.

I was recommended this website through my cousin. I'm not sure whether
this publish is written by way of him as no one else recognize such special
about my trouble. You're amazing! Thanks!

# Howdy! I could have sworn I've visited this blog before but after browsing through some of the articles I realized it's new to me. Anyways, I'm certainly happy I found it and I'll be book-marking it and checking back often! 2019/05/15 8:03 Howdy! I could have sworn I've visited this blog b

Howdy! I could have sworn I've visited this blog before but after browsing through some of the articles I realized
it's new to me. Anyways, I'm certainly happy I found it and I'll be
book-marking it and checking back often!

# Hi to every , as I am truly eager of reading this website's post to be updated regularly. It contains fastidious information. 2022/11/30 4:54 Hi to every , as I am truly eager of reading this

Hi to every , as I am truly eager of reading this website's post to be updated regularly.
It contains fastidious information.

# Hi! This is kind of off topic but I need some guidance from an established blog. Is it tough to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about creating my own but I'm not sure where to start. D 2022/12/03 20:33 Hi! This is kind of off topic but I need some guid

Hi! This is kind of off topic but I need some guidance from an established
blog. Is it tough to set up your own blog? I'm not very techincal but I
can figure things out pretty fast. I'm thinking about creating my own but I'm not sure where to start.

Do you have any ideas or suggestions? With thanks

# hydroxychloroquine online otc 2022/12/26 9:55 MorrisReaks

https://www.hydroxychloroquinex.com/ aralen chloroquine

タイトル
名前
URL
コメント