Domain model - modeling a template class and the instance class?

115 Views Asked by At

I'm writing a program where someone can set up an email (To, CC, Subject, Body, etc.). They can then set up various triggers that will cause an email to be sent using the set up they defined.

How would that be modeled? Would I have an EmailTemplate class that would store the email setup, and then and EmailMessage class that would represent an actual instance of an email that was sent?

To make this more applicable to others who may find this question, how do you model a class that is used to create an instance of another class (did I word that correctly?)?

Is there a better way? Am I over-complicating it?

3

There are 3 best solutions below

0
On

Beside what @MikeSW have already said, you seem to try to do BDUF. Try with a simple version first - then it will be easier to imagine how you would like your clients to use it, and then redesign and refactor using small steps (which makes TDD applicable, as noted by MikeSW).

Regarding your question about a class that creates instances of another class - you're referring to the Factory pattern. And in fact, this seems like a good way to go for an email API. Consider this design: http://www.limilabs.com/blog/send-email-with-attachment - it is designed in this way ("builder" is a nickname for "factory").

0
On

Seems like you can use any EmailMessage as a template, copy it to a new EmailMessage, then allow the user to change it as needed.

1
On

If the message never changes, the EmailTemplate is enough. If the user can change the actual email, then you need the EmailMessage. It seems to be a very simple scenario, at least with the details you've told us.

TDD can help here too, write some expectations in form of tests and see where it leads to.