Property Conventions
Property Convention Definition:
Proper use of Data Members as Property Method Class level variables for Class and Interface signatures while using the correct access modifiers to comply with encapsulation using data hiding
Property Convention Justification:
Private and Public Properties hold Data State for Class consumption. State is managed as Class level fields for internal use and act as an access point for external consumption. Strongly enforced standards are required to ensure that external Software Entities cannot corrupt the internal workings of a Class type.
Property Convention Standards:
Class Level scoped Data Member fields are held in Property Methods using the get; and set; accessors.
When an Auto-Implemented or Auto-Property is used, the compiler will create a Private Anonymous backing field that can only be accessed through the property’s get; and set; accessors. Both get; and set; are required with an Auto-property but not if a converted to a Property Method with a private backing field.
These Methods are generally for Public or Private consumption but can be set as Internal, Protected or Protected Internal to restrict access when required.
Public Auto-Properties are used to manage State in Data Transfer Objects (DTOs) in the calling Software Entity
You can make a Auto-Property immutable by setting the access modifier for the set accessor to private. It can then only be set with the Type Constructor at instantiation time and cannot be modified after that action.
Property Best Practices:
-
Use Property Methods to empower the Property with the power of a C# Method.
– The Use of a Data Member is Not a Best Practice
-
private bool _isMockData { get; set; } // Property Method
-
private bool _isMockData; // Data Member
-
-
Keep any initialization logic as simple as possible
-
If you use initializing logic be absolutely certain that EVERY consuming Software Entities will ALWAYS use the initialization results you are mandating. If there is any doubt: Do not force a Setter result on EVERY Consuming Entity.
-
Never publicly expose Data Fields that are only consumed by the underlying Class. This will open the possibility for Class Corruption by outside Software Entities.
-
Always initialize Property Methods inside a Constructor to ensure Proper Object State for consumption
-
If in an Inheritance Hierarchy structure, use a Static Constructor to manage the instantiation of Base Class Properties dynamically from run-time events whenever available.
-
Understand the Instantiation sequence of Constructors
-
Calling Class Static Constructor
-
Using the base keyword to call:
-
Base Class Static Constructor
-
All Base Class Instance Constructors
-
-
Calling Class Instance Constructors
-
The this Keyword to pass properties to additional Constructors
-
-
Properties Provide State Data to Class Members and Base Classes…
… Always use the Proper Access Modifier to Prevent Class Behaviour Corruption
The following two tabs change content below.
I am a Principal Architect at Liquid Hub in the Philadelphia area specializing in Agile Practices as a Certified Scrum Master (CSM). I use Test Driven Development (TDD) and Acceptance Test Driven Development (ATDD) with Behavior Driven Development (BDD) as my bridge to Agile User Stories Acceptance Criteria in a Domain Driven Design (DDD) implementing true RESTful services
Latest posts by Brad Huett (see all)
- DevOps: A Bridge to Your DevOps Culture - March 25, 2016
- Embracing Test Driven Development (TDD) - March 25, 2016
- DevOps: Delivering Agile Projects - March 25, 2016