Despite not being a big fan of Beans generally, I do find that there are situations in which they are useful. One of those is as configuration objects to pass in to a component. I can add the data in one packaged set.
In reading about Clojure recently, one of the big advantages of Clojure is immutable variables. I wouldn't want immutable variables in ColdFusion all of the time, but it would be really nice sometimes.
So, I made Beany as my Bean implementation and made it to allow for immutable data.
Here is a basic implementation of Beany:
Now, I can get the FirstName using
I can change the first name using
If I wanted to make sure that the values on my Bean could no longer be changed, I would call:
If I wanted to implement this Bean as immutable from the outset, I could do the following:
I also sometimes extend Beany and put the data directly in the file. I especially like to do this for data that is sensitive. That way, I can .gitignore the file that holds that data and keep it out of the repository.
For example, I have an AWSCredentials.cfc file:
region = "us-east-1",
SMTP_username = "REDACTED",
SMTP_password = "REDACTED"
I call the component with this code:
That allows me to pass that into any component that connects with AWS without having those codes in the repository.
Once Beany is immutable, it will throw an exception any time any code attempts to change a value in Beany.
So, that is the basics of Beany. Beany is part of my free com.sebtools package for ColdFusion. Let me know what you think!