Basically, I have two separate tables, Campaign and CampaignDetails. They relate via CampaignID. I want a service that can handle both at the same time, as they're closely related and when one gets updated- so will the other.
At the moment I'm combining the two but it's starting to get a bit hard to look at
public interface ICampaignService
{
//INSERT
void InsertCampaign(Campaign campaign);
void InsertCampaignDetails(CampaignDetails campaign);
//UPDATE
void UpdateCampaign(Campaign campaign);
void UpdateCampaignDetails(CampaignDetails campaign);
//DELETE
void DeleteCampaign(Campaign campaign);
void DeleteCampaign(object id);
void DeleteCampaignDetails(CampaignDetails campaign);
void DeleteCampaignDetails(object id);
//ALL
IList<Campaign> AllCampaigns();
//FIND
Campaign FindCampaignBy(Expression<Func<Campaign, bool>> expression);
CampaignDetails FindCampaignDetailsBy(Expression<Func<CampaignDetails, bool>> expression);
//FILTER
IList<Campaign> FilterCampaignBy(Expression<Func<Campaign, bool>> expression);
IList<CampaignDetails> FilterCampaignDetailsBy(Expression<Func<CampaignDetails, bool>> expression);
//GET
Campaign GetCampaignByID(object id);
CampaignDetails GetCampaignDetailsByID(object id);
Campaign GetLatestCampaign();
Campaign GetMostRecentSentCampaign();
//SAVE
void Save();
}
This is just my interface, so the implementation is obviously a lot more.. And it seems a lot of similar code.
I came across this article http://bobcravens.com/2010/09/the-repository-pattern-part-2/ and I quite like the DataService that wraps all the individual services. But is it bad practise to say have one variable DataService mainService = new DataService(repo1, repo2, repo3, repo4)
that can the access every service?
So I should I split it into CampaignService and CampaignDetailsService or keep it as one?
EDIT
Example of update method
public void UpdateCampaign(Campaign campaignToUpdate)
{
_campaignRepository.Update(campaignToUpdate);
}
public void UpdateCampaignDetails(CampaignDetails campaignToUpdate)
{
_campaignDetailsRepository.Update(campaignToUpdate);
}
Doing the same thing basically but with different entities.
Example of using method above:
[HttpPost]
public ActionResult Create(Campaign campaign)
{
if (ModelState.IsValid)
{
try
{
_campaignService.InsertCampaign(campaign);
var campaignDetails = new CampaignDetails()
{
CampaignId = campaign.CampaignId
};
_campaignService.InsertCampaignDetails(campaignDetails);
_campaignService.Save();
return RedirectToAction("Index");
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, "Something went wrong. Message: " + ex.Message);
}
}
return View(campaign);
}
ICampaignService
is too low level. Post an example method that usesUpdateCampaignDetails(CampaignDetails)
and we will have a better idea what can be done to clean up the interface. \$\endgroup\$Campaign
? \$\endgroup\$