我有两个实体Publisher和ReferralOffer。优惠仅在2个月内有效。将另一个人作为Refferal的出版商获得了为该优惠描述的钱。一个出版商可以为特定报价带来尽可能多的人。在一段时间后,优惠到期并生成新的优惠。
1)现在问题是Publisher是我的根聚合,但是refferaloffer是Publisher聚合的一部分。或refferaloffer是单独的聚合。
2)refferaloffer是值对象。
3)我如何创建可以维护refferedTo和refeeredBy的类。
答案 0 :(得分:1)
我读过有关推介营销的wiki article,但这还不足以让我了解您的业务。
将尝试在代码中反映一些想法。
请记住 - 我对业务领域知之甚少。
省略/忽略巨大数量的详细信息(例如可能出现的技术难题,甚至是与推荐相关的内容相关的事情)。
但希望 - 这可能会给你一些想法。
public class Publisher:Root{}
/*
I believe it's a root, because multiple publishers can
bring referrals to one particular referral offer
it could be modeled as entity if offers were given for
publishers individually
it's certainly not a value object, because we care about identity
of particular offer disregarding it's state.
I'm ignoring possibility that there might be various incentives
with common structure/behavior
*/
public class ReferralOffer:Root{
public bool IsActive(){
return _startedOn.AddMonths(-_lengthInMonths)>DateTime.Now;
}
public void BringReferral(string referralName, Publisher boughtBy){
if (IsActive()) _referrals.Add(new Referral(referralName,boughtBy));
}
private List<Referral> _referrals;
public void SendBucksToLuckyBastards(IMoneyTransferService mts){
if (!IsActive()&&!_awardsSent){
_referrals.SelectMany(r=>r.BoughtBy)
.ForEach(p=>mts.SendToPublisher(p,_award));
_awardsSent=true;
}
}
public ReferralOffer(Money award, int lengthInMonths){
_award=award;
_lengthInMonths=lengthInMonths;
_startedOn=DateTime.Now;
}
//assuming it's amount for each referral and not total
private Money _award;
private int _lengthInMonths;
private DateTime _startedOn;
private bool _awardsSent;
}
/*
referral - person who is bought by publisher and who eventually
might become a publisher
if referral automatically becomes a publisher - likely this should
be modeled otherwise
entity, because it makes no sense w/o referral offer
*/
public class Referral:Entity{
public Publisher BoughtBy{get; private set;}
public string Name{get; private set;}
//internal, because only offer constructs them
internal Referral(string name,Publisher boughtBy){
BoughtBy=boughtBy;
Name=name;
}
}
var me=publisherRepository.ByFullName("Arnis Lapsa");
var offer=new ReferralOffer(200,2);
offer.BringReferral(me, "kamal");
//two months later
offer.SendBucksToLuckyBastards(moneyTransferService);
请记住 - 此代码只是为了帮助您。这远非好事。
尝试彻底理解代码并查看含义每行代码。
P.S。像往常一样欢迎批评