How would you convert my current business logic layer method into IOC?
public static List<User> GetUsers()
{
MyDBProvider db = new MyDBProvider();
return db.GetUsers();
}
How would you convert my current business logic layer method into IOC?
public static List<User> GetUsers()
{
MyDBProvider db = new MyDBProvider();
return db.GetUsers();
}
List<User> users = GetUsers(new MyDbProvider());
List<User> GetUsers(IDataProvider provider){
return provider.GetUsers();
}
I'd probably get rid of the static method and use a more object oriented approach for starters.
public class UserRepository {
private DBProvider provider;
public DBProvider getProvider() { return provider; }
public void setProvider(DBProvider provider) { this.provider = provider; }
public List<User> getUsers() { return getProvider().getUsers(); }
}
public interface DBProvider {
List<User> getUsers();
}
public class MyDbProvider implements DBProvider { ... }