17. 適用前
メインルーチン
public void Main ()
{
var user = new User (DateTime.Now);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
18. USER
USER CLASS
public class User
{
private DateTime _birthday;
public User (DateTime birthday)
{
_birthday = birthday;
}
public int Age{ get { GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
var repogitory = new UserRepogitory ();
repogitory.Save (this);
}
}
24. インタフェースを使う
USER
public class User
{
private DateTime _birthday;
public User (DateTime birthday)
{
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
IUserRepogitory repogitory = new UserRepogitory ();
repogitory.Save (this);
}
}
25. インタフェースを使う
USER
public class User
{
private DateTime _birthday;
public User (DateTime birthday)
{
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
IUserRepogitory repogitory = new UserRepogitory ();
repogitory.Save (this);
}
}
あれ?結局UserRepogitoryに依存している?
29. 依存性の注入
USER
public class User
{
private IUserRepogitory _repogitory;
private DateTime _birthday;
public User (DateTime birthday,IUserRepogitory repogitory)
{
_repogitory = repogitory;
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
_repogitory.Save (this);
}
}
30. 依存性の注入
USER
public class User
{
private IUserRepogitory _repogitory;
private DateTime _birthday;
public User (DateTime birthday,IUserRepogitory repogitory)
{
_repogitory = repogitory;
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
_repogitory.Save (this);
}
}
フィールド変数にする
31. 依存性の注入
USER
public class User
{
private IUserRepogitory _repogitory;
private DateTime _birthday;
public User (DateTime birthday,IUserRepogitory repogitory)
{
_repogitory = repogitory;
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
_repogitory.Save (this);
}
}
コンストラクタで実装オブジェクトを受け取る
32. 依存性の注入
USER
public class User
{
private IUserRepogitory _repogitory;
private DateTime _birthday;
public User (DateTime birthday,IUserRepogitory repogitory)
{
_repogitory = repogitory;
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
_repogitory.Save (this);
}
}
フィールド変数に設定
41. 依存性の注入
USER
public class User
{
private IUserRepogitory _repogitory;
private DateTime _birthday;
public User (DateTime birthday,IUserRepogitory repogitory)
{
_repogitory = repogitory;
_birthday = birthday;
}
public int Age{ get { return GetAge (); } }
private int GetAge(){
// 計算
return 9; // 永遠の9歳
}
public void Save(){
_repogitory.Save (this);
}
}
誰がこのフィールド変数に実装オブジェクトを設定するのか
49. 例えば
メインルーチンで依存性注入の例
public void Main ()
{
var repogitory = new UserRepogitory ();
var user = new User (DateTime.Now, repogitory);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
50. 例えば
メインルーチンで依存性注入の例
public void Main ()
{
var repogitory = new UserRepogitory ();
var user = new User (DateTime.Now, repogitory);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
どのデータアクセスを使うか
51. 例えば
メインルーチンで依存性注入の例
public void Main ()
{
var repogitory = new UserRepogitory ();
var user = new User (DateTime.Now, repogitory);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
USERクラスが使うデータアクセスを指定
52. 例えば
メインルーチンで依存性注入の例
public void Main ()
{
var repogitory = new UserRepogitory ();
var user = new User (DateTime.Now, repogitory);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
データアクセスを使ってオブジェクトを永続化
56. クラウドコンピューティング
アプリケーション2EXEのメインルーチン
public void Main ()
{
var repogitory = new UserRepogitoryCloud ();
var user = new User (DateTime.Now, repogitory);
Console.WriteLine("あなたの年齢は " + user.Age.ToString() + " です。");
user.Save (user);
}
別データベースへのアクセス