【发布时间】:2020-09-12 12:13:59
【问题描述】:
在我的类库中有一个类 A 有方法“添加”
Class A{
//method A
bool Add (string Username, string ItemDescription, int Quantity){
CheckStock checkStock = new CheckStock();
if (!checkStock.isAvailble) return false;
RePositoryA rePositoryA= new RePositoryA();
if (rePositoryA.GetUserID<=0) return false;
RePositoryB rePositoryB= new RePositoryB();
if (!rePositoryA.AddInvoice) return false;
return ture;
}
}
class RePositoryA {
//get userID based on username
int GetUserID (username){
//connect to database and get id
}
class RePositoryB {
//add invoice
bool AddInvoice(Invoice myInvoice){
//connect to database and add invoice to dabase
}
class CheckStock {
bool isAvailble(string ItemDescription){
//connect to webAPi and return if its in stock
}
}
}
我的问题是
-
如何重构“Add”方法,使我们不直接实例化新的 RePositoryA 、 RePositoryB 和 CheckStock? 2.我知道一种方法做三件事违反“只做一件事政策”所以上面的代码可能需要分解为三种方法? 喜欢
bool Add(){ CheckStock(); GetUserID(); AddInvoice();}
感谢您的帮助!!!
【问题讨论】:
标签: c# design-patterns refactoring class-library