Reputation: 3259
I have to create a program that would upload business data to the FTP site. I am developing this program in C#.
So I have created dependent classes for this program as follows
IData - to fetch the data from the database
IDataTransform - convert the data received from database to specific format as client requested
IFtpOperation - uploads the file from local disk
ISettings- It holds all the relevant configurations such connection string, FTP server location, credential etc.,
Here is the code of it
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);
}
Then I created another class that wires these operations. The above interfaces being passed as constructor parameter to this class. This class contains a method UploadCustomerData that call the methods in dependencies as required.
Here is the code
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));
}
}
}
Now my question is where does this class UploadFileViaFTP lives. In service layer or business layer? And please explain why is that?
I hope explained in detail.
Upvotes: 1
Views: 280
Reputation: 1387
This depends on how you define "business logic" in scope of your application. From what I read I see that your application's "business" is to transform and upload data and not to operate it in any other way. So I would say that UploadFileViaFTP class contains what is essential business logic for your app and should be in business layer.
Upvotes: 0
Reputation: 64943
It seems like it's more like a façade (learn more here). And this is business layer.
After all, UploadFilesViaFTP should be available in service operations or in a single tier application (a good indication for living in business layer).
Upvotes: 3