我正在學習MVP,測試驅動方法和工廠模式。我想寫幾個簡單的類來維護一個人的數據和存儲庫。這個人的數據將被存儲在sql中並用xml進行測試。我閱讀了關於StructureMap的內容,但不想使用它,而是想使用一個簡單的工廠實現,最終也可以幫助我連接單元測試用例。這裏是我的課程:理解MVP模式的測試驅動方法?
class Person
{
int id;
string name;
}
interface IPersonRepository
{
Person GetPerson(int id)
{
}
}
class PersonRepositorySql : IPersonRepository
{
Person GetPerson(int id)
{
//Fetch from sql
}
}
class PersonRepositoryXML : IPersonRepository
{
Person GetPerson(int id)
{
//Fetch from XML
}
}
static class PersonRepositoryFactory
{
static PersonRepositorySql Create()
{
return new PersonRepositorySql();
}
static PersonRepositoryXML CreateTest()
{
return new PersonRepositoryXML();
}
}
class Presenter
{
Presenter(View _view)
{
}
void DoSomething()
{
IPersonRepository fact = PersonRepositoryFactory.Create();
//fact.GetPerson(2);
}
}
class PresenterTest
{
void Test1()
{
IPersonRepository fact1 = PersonRepositoryFactory.CreateTest();
//fact1.GetPerson(2);
}
}
請告訴我,如果我採取的方法是正確的和任何其他建議。另外,因爲我沒有在構造函數中傳遞對象,所以這不作爲依賴注入的例子嗎?
沒有什麼是基於注入的抽象。這是測試和TDD的核心組件。我認爲你應該重新閱讀你的參考資料,特別是關於緊耦合和具體實現的部分..以及依賴注入。 –
請檢查... – user2645830