-
Notifications
You must be signed in to change notification settings - Fork 16
HasValidConstructor
Severity - Fail
Check method - Here
The HasValidConstructor
check ensures that any BHoM object which implements a constructor with parameters contains all of the parameters it requires to satisfy the Serialisation requirement.
Constructors should only exist on objects implementing the IImmutable
interface. Objects with this interface should have properties which are get
only (no set
accessor). All of these get
only properties should be parameters to the constructor, with the parameter name matching the property name following the usual lowercase conventions for parameter names.
Consider the following IImmutable
object, which does not have a constructor.
public class MyObject : BHoMObject, IImmutable
{
public virtual int MyInt { get; }
public virtual string MyString { get; }
public virtual Point MyPoint { get; set; }
}
This object will not correctly deserialise, as it will not be able to adequately set the properties MyInt
and MyString
. Therefore, a constructor must be provided with the parameter names matching, so the deserialisation can correctly align the deserialised data to the object property.
The property MyPoint
does not have to be a parameter to the constructor, as it implements a set
accessor. This is true for any property, including those inherited from the base BHoMObject
.
As such, a valid constructor would look like this:
public MyObject(int myInt, string myString)
{
//Constructor logic
}
The entire class, in its valid form, would look like this:
public class MyObject : BHoMObject, IImmutable
{
public virtual int MyInt { get; }
public virtual string MyString { get; }
public virtual Point MyPoint { get; set; }
public MyObject(int myInt, string myString)
{
//Constructor logic
}
}
If the constructor does not contain input parameters for all of the properties which implement only the get
accessor, this will flag as a failure under this check. The following object is therefore incompliant, as only MyInt
has a matching input parameter:
public class MyObject : BHoMObject, IImmutable
{
public virtual int MyInt { get; }
public virtual string MyString { get; }
public virtual Point MyPoint { get; set; }
public MyObject(int myInt)
{
//Constructor logic
}
}
More information on the use of IImmutable
interface within the BHoM can be found here.
-
Introduction to the BHoM:
What is the BHoM for?
Structure of the BHoM
Technical Philosophy of the BHoM -
Getting Started:
Installing the BHoM
Using the BHoM
Submitting an Issue
Getting started for developers -
Use GitHub & Visual Studio:
Using the SCRUM Board
Resolving an Issue
Avoiding Conflicts
Creating a new Repository
Using Visual Studio
Using Visual Studio Code -
Contribute:
The oM
The Engine
The Adapter
The Toolkit
The UI
The Tests -
Guidelines:
Unit convention
Geometry
BHoM_Engine Classes
The IImmutable Interface
Handling Exceptional Events
BHoM Structural Conventions
BHoM View Quality Conventions
Code Versioning
Wiki Style
Coding Style
Null Handling
Code Attributes
Creating Icons
Changelog
Releases and Versioning
Open Sourcing Procedure
Dataset guidelines -
Foundational Interfaces:
IElement Required Extension Methods -
Continuous Integration:
Introduction
Check-PR-Builds
Check-Core
Check-Installer -
Code Compliance:
Compliance -
Further Reading:
FAQ
Structural Adapters
Mongo_Toolkit
Socket_Toolkit