Subclassing BaseEntity: Strongly Typed Generated Subclasses
The architecture of MemberJunction extends beyond the foundational BaseEntity through a powerful subclassing mechanism. This approach is key to enhancing the functionality and specificity of the BaseEntity, leading to the creation of strongly-typed generated subclasses.
Subclassing BaseEntity is has two or more “tiers” involved, starting with the automatic generation of a subclass from the BaseEntity. This automatically generated subclass is known as a strongly- typed, generated subclass. It represents a customized extension of the BaseEntity, tailored to specific data entities.
Strongly Typed Generated Subclasses
The strongly typed generated subclasses are an integral part of MemberJunction’s approach to handling data entities. These subclasses are designed to be consistent with the current database schema, ensuring that they always accurately represent the data structure and types.
- Automated Generation: The generation of these subclasses is automated through MemberJunction’s CodeGen tool. This tool is pivotal in maintaining the synchronization between the subclasses and the database schema. Every time the schema is updated, CodeGen regenerates the strongly typed subclasses to reflect these changes.
- Enhanced Developer Experience: The use of strongly typed generated subclasses significantly enhances the developer experience. Unlike the generic methods of the BaseEntity, these subclasses provide specific getters and setters for each field. For example, instead of generic set or get methods, a subclass would have direct access methods like myObject.firstName or myObject.lastName. This not only provides clarity but also aids in error prevention and easier maintenance.
// The below is an example, you actually can't directly instantiate BaseEntity, it is shown for example purposes only
const myObject: BaseEntity;
myObject.Set('FirstName', 'Jane');
myObject.Set('LastName', 'Smith');
const md = new Metadata();
const person = md.GetEntityObject<PersonEntity>("Persons");
person.NewRecord();
person.FirstName = 'Jane';
person.LastName = 'Smith';
person.Save();
Compile-Time Checking and IntelliSense
One of the most impactful features of using strongly typed generated subclasses is the introduction of compile-time checking and IntelliSense and similar IDE support that comes from adding documentation into a code base. This is a substantial improvement over the more generic approach of the BaseEntity.
- Compile-Time Checking: With strongly typed subclasses, developers benefit from compile-time checking, which ensures that only valid properties and methods are accessed, and that they align with the database schema.
- IntelliSense Support: IntelliSense or similar code-completion tools in integrated development environments (IDEs) can utilize the detailed structure of these subclasses. This provides developers with real-time suggestions and information, making the coding process more efficient and less prone to errors.
The Impact on Development
The transition from a generic BaseEntity approach to using strongly typed generated subclasses greatly impacts the development process. It makes the code more robust by ensuring type safety and alignment with the database schema. Additionally, the enhanced development experience leads to more efficient coding, better error handling, and a smoother overall development process.
AI-Driven Development
In a world where code is increasingly being automatically generated by AI tools like Co-Pilot, having structure in a framework like MemberJunction provides incredible advantages because the AI tools are able to understand the framework itself and the generated code including the embedded documentation (which is emitted into the generated sub-classes via JSDoc/TSDoc notation).
Updated 7 months ago