I'm a newbie and I'm new to C# (thus statics and copy-paste) and I'm painfully aware that this code smells. I'm trying to implement a web service that handles a similar pass-through logic with three different data structures that are processed by three corresponding different methods of an external service.
How can I avoid copy-paste here?
public class OutBoundSvcSender {
public static string sendDataToOutBoundSvc(AbcRecs[] request) {
StringBuilder result = new StringBuilder();
try {
AbcImportResponse response = sendAbcDataToOutBoundSvc(request);
} catch (Exception ex) {
result.Append("\nException Message:\n");
result.Append(ex.Message);
result.Append("\nStack Trace:\n");
result.Append(ex.StackTrace);
return result.ToString();
}
return "OK";
}
public static string sendDataToOutBoundSvc(BcdRecs[] request) {
StringBuilder result = new StringBuilder();
try {
BcdImportResponse response = sendBcdDataToOutBoundSvc(request);
} catch (Exception ex) {
result.Append("\nException Message:\n");
result.Append(ex.Message);
result.Append("\nStack Trace:\n");
result.Append(ex.StackTrace);
return result.ToString();
}
return "OK";
}
public static string sendDataToOutBoundSvc(CdeRecs[] request) {
StringBuilder result = new StringBuilder();
try {
CdeImportResponse response = sendCdeDataToOutBoundSvc(request);
} catch (Exception ex) {
result.Append("\nException Message:\n");
result.Append(ex.Message);
result.Append("\nStack Trace:\n");
result.Append(ex.StackTrace);
return result.ToString();
}
return "OK";
}
private static AbcImportResponse sendAbcDataToOutBoundSvc(AbcRecs[] request) {
var Recs = new AbcRecord[request.Length];
OutBoundSvcImportServiceClient client = new OutBoundSvcImportServiceClient();
string token = client.Login(new AuthorizationRequest {
Login = ConfigurationManager.AppSettings["OutBoundSvcServiceLogin"],
Password = ConfigurationManager.AppSettings["OutBoundSvcServicePassword"]
}).Token;
for (int i = 0; i < Recs.Length; i++) {
AbcRecs received = request[i];
Recs[i] = new AbcRecord {};
Recs[i].Id = received.Id;
Recs[i].Model = received.Model;
Recs[i].Type = received.Type;
Recs[i].Category = received.Category;
Recs[i].YearOfManufacture = received.YearOfMan.ToString();
Recs[i].Company = received.CompanyId;
Recs[i].AdditionalDocs = received.AddDocs;
}
AbcImportRequest abcRequest = new AbcImportRequest {
AuthorizationToken = token,
AbcRecords = Recs
};
return client.AbcImport(abcRequest);
}
private static BcdImportResponse sendBcdDataToOutBoundSvc(BcdRecs[] request) {
var Recs = new BcdRecord[request.Length];
OutBoundSvcImportServiceClient client = new OutBoundSvcImportServiceClient();
string token = client.Login(new AuthorizationRequest {
Login = ConfigurationManager.AppSettings["OutBoundSvcServiceLogin"],
Password = ConfigurationManager.AppSettings["OutBoundSvcServicePassword"]
}).Token;
for (int i = 0; i < Recs.Length; i++) {
BcdRecs received = request[i];
Recs[i] = new BcdRecord {};
Recs[i].Id = received.Id;
Recs[i].FirstName = received.FirstName;
Recs[i].LastName = received.LastName;
Recs[i].Initials = received.Initials;
Recs[i].Position = received.Position;
Recs[i].Company = received.Company;
}
BcdImportRequest bcdRequest = new BcdImportRequest {
AuthorizationToken = token,
BcdRecords = Recs
};
return client.BcdImport(bcdRequest);
}
private static CdeImportResponse sendCdeDataToOutBoundSvc(CdeRecs[] request) {
var Recs = new CdeRecord[request.Length];
OutBoundSvcImportServiceClient client = new OutBoundSvcImportServiceClient();
string token = client.Login(new AuthorizationRequest {
Login = ConfigurationManager.AppSettings["OutBoundSvcServiceLogin"],
Password = ConfigurationManager.AppSettings["OutBoundSvcServicePassword"]
}).Token;
for (int i = 0; i < Recs.Length; i++) {
CdeRecs received = request[i];
Recs[i] = new CdeRecord { };
Recs[i].CdeId = received.CdeId;
Recs[i].CdeNumber = received.CdeNumber;
Recs[i].CdeCompanyId = received.CdeCompanyId;
Recs[i].CdeCompanyName = received.CdeCompany;
}
CdeImportRequest cdeRequest = new CdeImportRequest {
AuthorizationToken = token,
CdeRecords = Recs
};
return client.CdeImport(cdeRequest);
}
}