我必須創建一個將業務數據上傳到FTP站點的程序。我正在用C#開發這個程序。業務層業務層的程序邏輯
所以我創建了相關的類對這一計劃如下
IData的 - 從數據庫
IDataTransform獲取數據 - 轉換數據從數據庫接收到特定的格式按客戶要求
IFtpOperation - 上傳文件從本地磁盤
ISettings-它擁有所有相關的配置,例如連接字符串,FTP服務器位置,憑證等,
這裏是它的代碼
public interface IFtpOperation
{
void Upload(string localFilePath);
}
public interface IDataTransform<in T1, out T2>
{
T2 Transform(T1 input);
}
public interface IData<in T1, out T2>
{
T2 GetData(T1 input);
}
public interface ISettings
{
T GetValue<T>(string key);
}
然後,我創建了另一個類來連接這些操作。上述接口作爲構造函數參數傳遞給此類。該類包含一個方法UploadCustomerData,它根據需要調用依賴項中的方法。
下面是代碼
public class UploadFileViaFTP
{
private readonly IFtpOperation _ftp;
private readonly IData<int, IList<CustomerData>> _customerData;
private readonly IDataTransform<CustomerData, string> _transformer;
private readonly ISettings _settings;
public UploadFileViaFTP(IFtpOperation ftp, IData<int, IList<CustomerData>> customerData,
IDataTransform<CustomerData, string> transformer, ISettings settings)
{
_ftp = ftp;
_transformer = transformer;
_customerData = customerData;
_settings = settings;
}
public void WriteToDisk(IList<CustomerData> customers, string localFilePath)
{
using (var writer = new StreamWriter(localFilePath))
{
foreach (var customer in customers)
{
writer.WriteLine(_transformer.Transform(customer));
}
}
}
private void UploadCustomerData(int accountId)
{
var customerData = _customerData.GetData(accountId);
if (customerData.Count > 0)
{
var localPath = _settings.GetValue<string>("LocalFilePath");
WriteToDisk(customerData, localPath);
_ftp.Upload(localPath);
}
else
{
_notificationMessage.AppendLine(
string.Format("There could be an error or no data avaiable to upload at {0} for account {1}.", DateTime.Now, accountId));
}
}
}
現在的問題是哪裏該類UploadFileViaFTP生活。在服務層還是業務層?請解釋這是爲什麼?
我希望詳細解釋一下。