Mi sento come se avessi un "qualcosa" riutilizzabile qui e non sono sicuro se pensarlo come un pattern o un algoritmo (o nessuno dei due).
È caratterizzato dall'avere una quantità sconosciuta di lavoro per portare a termine un'attività perché le sottoattività possono incontrare varie condizioni che causano l'aggiunta di "Problemi" a una coda globale.
Quindi, il comando viene eseguito ripetutamente accoppiato con un round di "Issue Fixing" fino a quando non ci sono problemi o il numero di problemi non cambia.
Sto solo mettendo abbastanza codice qui per mostrare di cosa sto parlando - c'è un po 'di più (fammi sapere se dovrei postare di più)
public void FindNewCampaigns()
{
var findNewCampaigns = Locator.Get<ICommand<IEnumerable<Campaign>>>("FindNewCampaigns");
var campaigns = findNewCampaigns.Execute();
var issues = Locator.Get<ICollection<Issue>>("CommandIssues");
while (issues.Count > 0)
{
int before = issues.Count;
FixIssues(issues);
issues.Clear();
campaigns = findNewCampaigns.Execute();
int after = issues.Count;
if (before == after)
{
System.Console.WriteLine("No issues got fixed, quitting ({0}/{1}).", before, after);
break;
}
}
if (issues.Count == 0)
{
CreateCampaigns(campaigns);
}
}
private void FixIssues(ICollection<Issues.Issue> issues)
{
foreach (var issue in issues)
{
System.Console.WriteLine("ISSUE: " + issue.GetType().Name + " - " + issue.ToString());
if (issue is AdvertiserDoesNotExist)
{
var specificIssue = (AdvertiserDoesNotExist)issue;
var command = Locator.Get<ICommand<string, DirectAgents.SynchService.Model.EomDatabase.Advertiser>>("CreateAdvertiser");
command.Execute(specificIssue.AdvertiserName);
}
else if (issue is AccountManagerDoesNotExist)
{
var specificIssue = (AccountManagerDoesNotExist)issue;
var command = Locator.Get<ICommand<string, DirectAgents.SynchService.Model.EomDatabase.AccountManager>>("CreateAccountManager");
command.Execute(specificIssue.AccountManagerName);
}
else if (issue is AdManagerDoesNotExist)
{
var specificIssue = (AdManagerDoesNotExist)issue;
var command = Locator.Get<ICommand<string, DirectAgents.SynchService.Model.EomDatabase.AdManager>>("CreateAdManager");
command.Execute(specificIssue.AdManagerName);
}
else if (issue is MediaBuyerDoesNotExist)
{
var specificIssue = (MediaBuyerDoesNotExist)issue;
var command = Locator.Get<ICommand<string, DirectAgents.SynchService.Model.EomDatabase.MediaBuyer>>("CreateMediaBuyer");
command.Execute(specificIssue.MediaBuyerName);
}
}
}
Ecco il codice per FindNewCampaigns
. Aggiunge problemi mentre li trova. Un numero dovrebbe essere qualcosa che deve accadere prima che una nuova campagna possa effettivamente essere creata in un negozio di destinazione.
public class FindNewCampaigns : Command<IEnumerable<Campaign>>
{
private IFactory<CakeEntities> cakeEntities;
private IFactory<DADatabase> eomDatabase;
public FindNewCampaigns(IFactory<CakeEntities> cakeEntities, IFactory<DADatabase> eomDatabase)
{
this.cakeEntities = cakeEntities;
this.eomDatabase = eomDatabase;
}
public override IEnumerable<Campaign> Execute()
{
using (var eom = eomDatabase.Create())
using (var cake = cakeEntities.Create())
{
// Get EOM campaigns
var campaigns = eom.Campaigns.Select(c => c.pid).ToList();
// Get Cake offers
var offers = cake.CakeOffers.Select(c => c.Offer_Id).ToList();
// Get Cake offers that don't match to EOM campaigns
var newOffers = offers.Except(campaigns).ToList();
// Get default values
int accountManagerID = DefaultAccountManagerId(eom);
int adManagerID = DefaultAdManagerId(eom);
int advertiserID = DefaultAdvertiserID(eom);
int campaignStatusID = DefaultCampaignStatus(eom);
// Create new campaigns in memory
var newCampaigns = (from offer in cake.CakeOffers
where newOffers.Contains(offer.Offer_Id)
select new
{
Offer = offer,
Campaign = new Campaign {
pid = offer.Offer_Id,
campaign_name = offer.OfferName,
campaign_status_id = campaignStatusID,
account_manager_id = accountManagerID,
ad_manager_id = adManagerID,
advertiser_id = advertiserID,
}
}).ToList();
// Set campaign status
var campaignStatus = eom.CampaignStatus.ToDictionary(c => c.name, c => c.id);
campaignStatus.Add("Private", campaignStatus["Active"]);
campaignStatus.Add("Apply To Run", campaignStatus["Active"]);
campaignStatus.Add("Inactive", campaignStatus["default"]);
foreach (var item in newCampaigns)
{
string statusName = item.Offer.StatusName;
if (campaignStatus.ContainsKey(statusName))
{
item.Campaign.campaign_status_id = campaignStatus[statusName];
}
else
{
AddIssue(new CampaignStatusDoesNotExist(statusName));
}
}
// Set advertiser
var cakeAdvertisers = cake.CakeAdvertisers.ToDictionary(c => c.Advertiser_Id);
foreach (var item in newCampaigns)
{
int offerAdvertiserID = int.Parse(item.Offer.Advertiser_Id);
var offerAdvertiser = cake.CakeAdvertisers.FirstOrDefault(c => c.Advertiser_Id == offerAdvertiserID);
string offerAdvertiserName = offerAdvertiser.AdvertiserName;
var eomAdvertiser = eom.Advertisers.FirstOrDefault(c => c.name == offerAdvertiserName);
if (eomAdvertiser != null)
{
item.Campaign.advertiser_id = eomAdvertiser.id;
}
else
{
AddIssue(new AdvertiserDoesNotExist(offerAdvertiserName));
}
}
// Set account manager
foreach (var item in newCampaigns)
{
int offerAdvertiserID = int.Parse(item.Offer.Advertiser_Id);
var offerAdvertiser = cake.CakeAdvertisers.FirstOrDefault(c => c.Advertiser_Id == offerAdvertiserID);
string offerAccountManager = offerAdvertiser.AccountManagerName;
if (offerAccountManager != null)
{
AccountManager am = eom.AccountManagers.ToList().SingleOrDefault(c => c.NameIsEquivalentTo(offerAccountManager));
if (am != null)
{
item.Campaign.account_manager_id = am.id;
}
else
{
AddIssue(new AccountManagerDoesNotExist(offerAccountManager));
}
}
else
{
AddIssue(new OfferHasNoAccountManager(item.Offer.OfferName));
}
}
// Set ad manager
foreach (var item in newCampaigns)
{
int offerAdvertiserID = int.Parse(item.Offer.Advertiser_Id);
var offerAdvertiser = cake.CakeAdvertisers.FirstOrDefault(c => c.Advertiser_Id == offerAdvertiserID);
string offerAdManager = offerAdvertiser.AdManagerName;
if (offerAdManager != null)
{
AdManager ad = eom.AdManagers.ToList().SingleOrDefault(c => c.NameIsEquivalentTo(offerAdManager));
if (ad != null)
{
item.Campaign.ad_manager_id = ad.id;
}
else
{
AddIssue(new AdManagerDoesNotExist(offerAdManager));
}
}
else
{
AddIssue(new OfferHasNoAdManager(item.Offer.OfferName));
}
}
return newCampaigns.Select(c => c.Campaign).ToList();
}
}