これは、あなたがのIoCを使用してそれを行うだろうか、一般的である:
public interface IDataProvider
{
User GetUserByID(int userID);
}
...
class Client
{
// Client gets the IDataProvider as a mandatory constructor parameter
public Client (IDataProvider dataProvider)
{
this.dataProvider = dataProvider;
}
public User GetUserByID(int userID)
{
return dataProvider.GetUserById (userID);
}
private IDataProvider dataProvider;
}
...
void Main()
{
// create IoC container of your choice...
IoCContainer container = new IoCContainer();
// ... and then configure it (from a config. file or programmatically)
container.Configure();
// create the Client instance using the container
// note that the container takes care of creating appropriate IDataProvider
// for you
Client client = container.GetService<Client>();
User user = client.GetUserByID ("john doe");
}
あなたの例との唯一の問題は、あなたのクライアントクラスのメソッドは実際にコードに任意の値を追加していないということである - それは単に転送しますデータプロバイダへの呼び出し。
+1。あなたのコメントについては、それが何らかの抽象を提供するなら、それは価値があるかもしれません。それ以上の文脈なしに判断するのは難しいです。 –