I have a CFML ColdBox framework model service which needs to build links. However, models don't have access to the framework SuperType thus don't have access to event.buildLink()
.
How can I give services within my model the ability to create links? Is there a way to make the buildLink()
functionality available through some kind of dependency injection?
Sample model service:
component
singleton
{
function getLinkToUser( required numeric userId ) {
return event.buildLink( "users.#arguments.usersId#" );
}
}
--Update--
Many of the comments suggest that embedding framework functionality into the model may be a mistake, and that buildLink()
should really only be used within views. For the most part, I agree, and feel introducing framework services into the model violates encapsulation and concerns.
That being said, let's expand the above code example to a more real-world situation: Let's say you have a model service which generates emails to customers and the content of those emails is very much determined by complex business rules. In this case, I could see an argument for generating the email content in the model because that is where business rules live.
If you instead generate the email content in the view, you would be executing business logic into a layer which should really only be used for display/output.
Assuming that generating the email body text in the model layer is the right thing to do, doesn't it also make sense that it should also be able to build HTML links based on framework routes within those emails there as well?