Search code examples
c#asp.net.netnhibernatefluent-nhibernate

Hiding property setters while using an OR Mapper


I am using Fluent-NHibernate to manage all the data persistence layer and I am overall very pleased (and grateful to the NHibernate community). I plan on continuing to use OR mappers. I've developed an API around the POCOs that are being mapped. The downside is that all the properties are both gettable and settable by the UI developers; when what I really want is to hide properties from non-middle tier development and only show the provided API methods to perform operations.

Does anyone have a good strategy for this?

Overly simple example:

member.FName = "Julian";    /// Don't do this because it avoids the my checking
member.LName = "King";


member.setName("Julian", "King");   /// Yes - this will throw an error if this person already exist 

Solution

  • private string _fName;
    public string FName
    {
       get { return _fName;}
    }
    
    private string _lName;
    public string LName
    {
      get { return _lName;}
    }
    
    public void SetName(string fName, string lName){
    
       // check for nulls here and or validate pre-conditions
    
      _fName = fName;
      _ lName = lName;
    
    // check for post conditions here
    
    }
    

    or better yet use value object (see definition of value object in DDD)

    public void SetName(Name name){
    
          _fName = name.FirstName;
          _ lName = name.LastName;
    
        }