我正在创建一个存储库,我必须处理两个数据提供程序(IFileDataAccess,IDbDataAccess)来构建我的模型对象(MyModel)。
我已经写了下面的代码,但我觉得它不正确,让MyModel类具有所有书面属性非常重要,因为它们全部一起使用。
请提出一个很好的方法来实现这一目标。
谢谢!
class MyModel
{
public Schedule Schedule { get; set; }
public Rates Rates { get; set; }
public Balance Balance { get; set; }
}
class MyRepository
{
private IFileDataAccess _fileDataAccess;
private IDbDataAccess _dbDataAccess;
public MyClass (IFileDataAccess fileDataAccess, IDbDataAccess dbDataAccess)
{
_fileDataAccess= fileDataAccess;
_dbDataAccess=dbDataAccess;
}
public MyModel GetMyModel()
{
return new MyModel(){Schedule=_dbDataAccess.GetSchedule(),Rates=_fileDataAccess.GetRates(), Balance=_fileDataAccess.GetBalance()};
}
}
答案 0 :(得分:1)
你是否有可能质疑你的存储库类,因为“它做得太多了”?看看这个变化,例如......
class MyModel
{
public Schedule Schedule { get; set; }
public Rates Rates { get; set; }
public Balance Balance { get; set; }
}
class MyFileDataRepository
{
private IFileDataAccess _fileDataAccess;
public MyFileDataRepository(IFileDataAccess fileDataAccess)
{
_fileDataAccess = fileDataAccess;
}
public Rates GetRates()
{
return _fileDataAccess.GetRates();
}
public Balance GetBalance()
{
return _fileDataAccess.GetBalance();
}
}
class MyDbDataRepository
{
private IDbDataAccess _dbDataAccess;
public MyDbDataRepository(IDbDataAccess dbDataAccess)
{
_dbDataAccess = dbDataAccess;
}
public Schedule GetSchedule()
{
return _dbDataAccess.GetSchedule();
}
}
class MyModelProvider
{
private MyFileDataRepository _fileDataRepository;
private MyDbDataRepository _dbDataRepository;
public MyModelProvider(MyFileDataRepository fileDataRepository, MyDbDataRepository dbDataRepository)
{
_fileDataRepository = fileDataRepository;
_dbDataRepository = dbDataRepository;
}
public MyModel GetModel()
{
return new MyModel
{
Schedule = _dbDataRepository.GetSchedule(),
Rates = _fileDataRepository.GetRates(),
Balance = _fileDataRepository.GetBalance()
};
}
}