Architecture or Pattern for handling properties with custom setter/getter?

Posted by Shelby115 on Programmers See other posts from Programmers or by Shelby115
Published on 2014-05-25T22:28:55Z Indexed on 2014/05/29 21:58 UTC
Read the original article Hit count: 164

Filed under:
|
|

Current Situation:

I'm doing a simple MVC site for keeping journals as a personal project. My concern is I'm trying to keep the interaction between the pages and the classes simplistic. Where I run into issues is the password field. My setter encrypts the password, so the getter retrieves the encrypted password.

public class JournalBook
{
    private IEncryptor _encryptor { get; set; }
 private String _password { get; set; }

    public Int32 id { get; set; }
    public String name { get; set; }
    public String description { get; set; }
    public String password 
 { 
     get
    {
            return this._password;
     }
        set
        {
            this.setPassword(this._password, value, value);
        }
    }
    public List<Journal> journals { get; set; }

    public DateTime created { get; set; }
    public DateTime lastModified { get; set; }

    public Boolean passwordProtected
    {
        get
        {
            return this.password != null && this.password != String.Empty;
        }
    }

    ...

}

I'm currently using model-binding to submit changes or create new JournalBooks (like below). The problem arises that in the code below book.password is always null, I'm pretty sure this is because of the custom setter.

[HttpPost]
public ActionResult Create(JournalBook book)
{
    // Create the JournalBook if not null.
    if (book != null)
        this.JournalBooks.Add(book);

    return RedirectToAction("Index");
}

Question(s): Should I be handling this not in the property's getter/setter? Is there a pattern or architecture that allows for model-binding or another simple method when properties need to have custom getters/setters to manipulate the data?

To summarize, how can I handle the password storing with encryption such that I have the following,

  • Robust architecture
  • I don't store the password as plaintext.
  • Submitting a new or modified JournalBook is as easy as default model-binding (or close to it).

© Programmers or respective owner

Related posts about c#

Related posts about design