This code is a chunk of big project, so I am sorry in advance for not adding everything as it's just not possible.
[DataContract]
public class Companionship
{
[DataMember]
public int ID;
[DataMember]
public string ClientID;
[DataMember]
public string UserSID;
[DataMember]
public string Role;
[DataMember]
public string Status;
[DataMember]
public string StartDate;
[DataMember]
public string EndDate;
[DataMember]
public bool AddedInBP;
public int Add(Companionship pC)
{
int clientID = -1;
var gda = new USDataAccess();
if (string.IsNullOrEmpty(pC.ClientID))
clientID = new gDataAccess().GetClientByURL(SPContext.Current.Web.Site.Url).ClientID;
else
clientID = Convert.ToInt32(pC.ClientID);
pC.Status = "Active";
DateTime? dtEnd = string.IsNullOrEmpty(pC.EndDate) ? (DateTime?)null : DateTime.Parse(pC.EndDate);
DateTime dtStart = string.IsNullOrEmpty(pC.StartDate) ? DateTime.Now : DateTime.Parse(pC.StartDate);
gda.AddCompanionship(clientID, pC.UserSID, pC.Role, pC.Status, dtStart, dtEnd);
new DirectoryGroup().Add(clientID, pC.UserSID, "ChangeMe In Companionship");
var cDetails = new Company().Get(clientID);
if (cDetails != null && !string.IsNullOrEmpty(cDetails.CompanyID) && pC.AddedInBP)
{
SendApprovalRequest();
}
return 1;
}
public int AddCollection(Companionship[] pCs)
{
foreach (var c in pCs)
this.Add(c);
return 1;
}
private void SendApprovalRequest()
{
string url = "https://portal.gov.com/CompanyManagement/_vti_bin/sync/sync.svc";
ICSync ws = Utils.GetProxy<ICSync>(url);
var user = Utils.ADUserToObject(ADUser.LoadBySid(this.UserSID));
user.Companionships[0] = this;
var dd = Utils.UserToDirectoryGroupDetails(user);
ws.AddDirectoryGroup(SPContext.Current.Web.Site.Url, SPContext.Current.Web.CurrentUser.Email, dd);
}
}
This code is in a utilities project, it's called from WCF services to interact with Companionship
s, it adds them to the database with some business logic and sends a message to another web service.
Looking at the structure I think this can be improved.
I am calling it like this:
new Companionship().AddCollection(pCPs);
I am more worried about the way I am interacting with Companionship
class.