I am trying to learn the Rules Design Pattern as described here: http://www.michael-whelan.net/rules-design-pattern/ (towards the bottom of the webpage). I have written a class like this:
public class OfferCalculator : IOfferCalculator
{
public List<IOfferRule> _rules;
public RulesOfferCalculator(List<IOfferRule> rules)
{
_rules = rules;
_rules.Add(new AgeRule());
}
public virtual List<string> CalculateOffers(Application application)
{
List<string> offers = new List<string>();
foreach (var rule in _rules)
{
offers.Add(rule.getOffer(application));
}
return offers;
}
}
and a class like this:
public interface IOfferRule
{
string getOffer(Application application);
}
and a class like this:
public class AgeRule : IOfferRule
{
public String getOffer(Application application)
{
if (application.Age >= 50)
{
return "PremierCard";
}
else
{
return "NormalCard";
}
}
}
If the applicants age is 50 or greater then they are offered a Premier Card and if it is less than 50 then they are offered a normal card.
I need to record what offer they are entitled to in the database, which looks like this:
CREATE TABLE Card (id int identity not null, name varchar(100))
insert into card (name) values ('PremierCard');
insert into card (name) values ('NormalCard');
CREATE TABLE CardPerson (PersonID int not null, FOREIGN KEY (PersonID) REFERENCES Person(ID) cardid,FOREIGN KEY (CardID) REFERENCES Card(ID) int not null, primary key (personid,cardid))
CREATE TABLE Person (id int identity not null, name varchar(100))
I believe AgeRule should return a card entity rather than a string? How do I create a card object in the Business Layer? Do I just inject a repository into AgeRule and run the following SQL statement (to populate the object):
select * from card where name = 'Premiercard'
This does not look "correct" to me. How do I get a Card object in the domain layer? Is there a "better" pattern for this?
GetOffer()
to return anOffer
object.