物件間の依存性をどのように隔離するか

3759 ワード

public interface IAccountDao
{
    string GetPassword(string id);
}

public interface IHash
{
    string GetHashResult(string password);
}

public class AccountDao : IAccountDao
{
    public string GetPassword(string id)
    {
        throw new NotImplementedException();
    }
}

public class Hash : IHash
{
    public string GetHashResult(string password)
    {
        throw new NotImplementedException();
    }
}

public class Validation
{
    private IAccountDao _accountDao;
    private IHash _hash;

    public Validation(IAccountDao dao, IHash hash)
    {
        this._accountDao = dao;
        this._hash = hash;
    }

    public bool CheckAuthentication(string id, string password)
    {
        //  ,id                        
        var passwordByDao = this._accountDao.GetPassword(id);

        //  password, hash 
        var hashResult = this._hash.GetHashResult(password);

        //  hash , 
        return passwordByDao == hashResult;
    }
}

http://msdn.microsoft.com/zh-tw/library/dn148215.aspx