您如何将我当前的业务层转换为IOC风格?

时间:2009-05-13 13:49:01

标签: inversion-of-control

您如何将我当前的业务逻辑层方法转换为IOC?

public static List<User> GetUsers()
{
    MyDBProvider db = new MyDBProvider();

    return db.GetUsers();
}

2 个答案:

答案 0 :(得分:3)

List<User> users = GetUsers(new MyDbProvider());

List<User> GetUsers(IDataProvider provider){
    return provider.GetUsers();
}

答案 1 :(得分:0)

我可能会摆脱静态方法,并为初学者使用更面向对象的方法。

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 { ... }