When is a class with no methods poor design?
From what I've read, a class with no methods (i.e. no behaviors) (AKA dumb class) is poor design, with the exception of Data Transfer Objects (DTOs). This is due to the purpose of DTOs being to reduce the overhead when transferring data to a remote interface (Local DTO). There appears to be some debate on DTOs and Plain Old Class Object (POCO VS DTO) and debate going further to anemic design (Anemic Model Domain).
So, where the dumb class in question is a local object (i.e. not for transferring data) are you generally better to refactor the attributes of the dumb class and implement them as a collection (e.g. Dictionary)? To quote Bill K (How can i write DAOs for resources with extensible properties), "Where you would have used an object, use a hashtable. For your attribute names use keys, for the attribute values, use the value in the hashtable."
My thinking when designing this dumb class was that of composition. Another class is composed of multiple dumb class objects (i.e. a collection of dumb class objects). Was my thinking wrong? If I am to implement the dumb class as a collection, I would have a collection of a collection of attributes.
I am of the understanding collections of collections of collections etc. is also poor design. Is there some guiding principle to balance these apparent choices of poor design?
As always any insight or guidance is appreciated.
Regards Shannon
Hot and cold air conditioners can regulate the temperature according to your liking, both in summers and winters. The air conditioner simply reverses its function, allowing the AC to run efficiently during winters, thus throwing warm air in the room.
a/ c is an abbreviation for air-conditioning.
AC. 1. Abbreviation for Altocumulus - a cloud of a class characterized by globular masses or rolls in layers or patches, the individual elements being larger and darker than those of cirrocumulus and smaller than those of stratocumulus. These clouds are of medium altitude, about 8000-20,000 ft (2400-6100 m). 2.
The first modern air conditioner was invented in 1902 by Willis Haviland Carrier, a skilled engineer who began experimenting with the laws of humidity control to solve an application problem at a printing plant in Brooklyn, NY.
If a class has no logic, then that would be considered a data-structure. For the question about whether to use a generic collection vs a type, I would choose creating types because of their expressiveness.
Take the following example.
var employee = new Dictionary<string, object>();
employee["FirstName"] = "Steve";
employee["LastName"] = "McQueen";
employee["DOB"] = new DateTime(1942, 1, 5);
employee["Salary"] = 215000m;
Here you have the problem of
Contrast this with.
var employee = new Employee {
FirstName = "Steve",
LastName = "McQueen",
DOB = new DateTime(1942, 1, 5),
Salary = 215000m
};
You get the benefit of
DOB
to DateOfBirth
without having to do a search and replace)Though the one downside to this is that you have to define a class, which means more typing, though I feel the benefits greatly outweigh the costs.
To elaborate on my examples above, suppose you wrote a method from a repository that returned an employee.
var employee = employeeRepository.GetById(25);
If employee
were a dictionary in this example, I would have no Intellisense to know which attributes an employee has, let alone their types, this is also illustrated by @HighCore's comment. The only ways to determine this is to either
In the scenario of persisting a new employee
, the dictionary runs into the same pitfalls and now you're at the peril of hoping the method you just executed returns meaningful error messages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With