See Arguments against having a multiple classes for a single table for details. Option #3 violates the principle of encapsulation which states that a class should contain ALL the data and ALL the operations concerning the entity which it represents. See Arguments against having a single class for multiple tables for details. In my humble opinion option #1 violates the Single Responsibility Principle as a software object ends up by being responsible for more than one business object. Have the data and/or business rules for a single database table split across several classes, such as by implementing the Command Query Responsibility Segregation (CQRS) pattern which advocates using separate models (classes) for update and display.Have a class which is responsible for the data and business rules for a single database table. Have a class which is responsible for the data and business rules for more than one database table.I have been told many times by numerous people that having a separate class for each database table is not proper OO, that real OO programmers don't do it that way, but none of these people have ever provided a satisfactory answer to these simple questions: "Why not? What are the alternatives? Why is my chosen method worse than the alternatives?"Īs far as I can tell there are only three possibilities when it comes to the relationship between tables and classes: This article expands on what I wrote several years ago in Each database table requires its own class. I don't need to waste any time with OOD or DDD I don't need to waste time in designing class hierarchies I don't need to waste any time choosing which design patterns to use I don't need to waste time in building each table class from scratch I don't need to waste time in designing methods and properties for each table class I don't need to waste time with writing code to validate user input I don't need to waste time in writing code to perform standard SQL queries I don't need to waste time with designing Controllers for each of my Model classes I don't need to waste time with writing a separate Data Access Object (DAO) for each table I don't need to waste time designing and building HTML documents for each web page I don't need to waste time writing code for each of the components on a web page Additional Advantages of my approach Conclusion References Amendment History Comments Introduction
0 Comments
Leave a Reply. |