5 ms·
What I am gathering from this is the author is displeased with the concept of bundling modeling of the application domain with the mutation of the state indicat
by bob1029 7y ago
What I am gathering from this is the author is displeased with the concept of bundling modeling of the application domain with the mutation of the state indicated by the same model properties. This is more-or-less the original ideal of OOP: Put all of your properties as well as the methods to act on those properties into a single, well-defined class.
In my experience as a C# developer, this is not an ideal approach. I assume non-OOP developers have a perspective that this is how we actually go about things. In reality, my strategy is generally to declare POCOs - Plain Old CLR Objects - which effectively just serve to model the business domain as collections of basic properties (basic data types, collections, enums, other model types) without any methods, constructors, attributes, etc.
If you look in the Models folder in any project I currently work on, you will not find a single method declaration in any of the class files. All of the functionality is broken out into crosscutting abstractions such as a Logic or Rules namespace. The general idea is this: Why should I build a model specifically for this one context of usage and have the properties tightly-coupled to their mutators, when I can completely decouple these things and have the models operate with any arbitrary state mutators? In my experience, the modeling of all possible business facts can be done completely independently of how those facts should mutate over time. Another thing to consider is that not all business facts are relevant at all times, but that doesn't mean you can't catalog them all within the same logical business model (e.g. a 'Customer.cs' POCO). Null is a very powerful tool if used responsibly.
Taking this a step further, you can have a business model project (class library/Nuget) that is shared across multiple projects within an organization. Since you have included no specific implementation in the model classes pertaining to how their properties are read/written, you can use these unencumbered in any situation. Mix in a little bit of JSON serialization and you get a really quick and easy way to distribute a common contract and interoperate between various business systems. These projects can also effectively serve as your principal documentation of the business domain model.
- 0x445442 7y agoOne thing I hate though, in a language like Java, is when I see "utility" classes with static methods which take objects as parameters, then perform some calculation based entirely on the state of the passed in object. In my opinion, if the object can reason about its own state and return an answer based on that reasoning, that method/logic should be in the object, not elsewhere. Another one that bothers me are these transformation static methods which take type A and return type B based on nothing but the state of type A. The languages were talking about, C#/Java already provide a facility for this called a constructor. If the development approach is going to completely remove operational methods from data types then I'd think long and hard about using a language which supports this instead of a language, like Clojure, which does not.
- TravHatesMe 7y agoI think you made a good point: it is easy to shoot yourself in the foot with OOP. It is difficult to shoot yourself in the foot with FP. But when OOP is done correctly it offers useful features.
- zozbot234 7y ago> that method/logic should be in the object, not elsewhere Only if it's involved in preserving some object invariants, or accessing parts of its state that are abstracted away in the public interface. Otherwise, you're breaking encapsulation by putting some logic in the object that doesn't belong there, and making it hard to change the implementation later. > The languages were talking about, C#/Java already provide a facility for this called a constructor. There are sensible reasons to avoid using constructors for this, at least in the general case.
- lazulicurio 7y ago> Only if it's involved in preserving some object invariants, or accessing parts of its state that are abstracted away in the public interface. Otherwise, you're breaking encapsulation by putting some logic in the object that doesn't belong there, and making it hard to change the implementation later. I've been pondering this thread all day, and I think this is the crux of the issue. Ideally, classes would only encapsulate state and you'd use namespaces/modules to encapsulate functionality. But most Java/C# OOP examples use classes to encapsulate both state and functionality, which gets you stuck in the morass that the article discusses.
- jayd16 7y agoWhat you ask for is mostly impossible because you might not know about every possible object you'll be getting your construction params from....and besides that's horribly coupled. What about circular dependencies? C# helps you to some degree with extension methods so you get the same instance.Method() syntax.
- denisw 7y ago> Why should I build a model specifically for this one context of usage and have the properties tightly-coupled to their mutators, when I can completely decouple these things and have the models operate with any arbitrary state mutators? Because otherwise, your domain model is not able to express which states are valid. A core idea behind encapsulation is that by controlling state mutations, it is possible to enforce invariants, thereby making invalid states impossible to construct. This idea is not unique to object orientation; for instance, in the static functional programming community, this is known as "making illegal state unrepresentable" [1]. By creating a "dumb" domain model and allowing business rule modules to do arbitrary mutations, each model is required to know about and enforce the domain model’s invariants itself, which dramatically increases the amount of code that potentially can break these invariants, and thus needs to be debugged if something goes wrong. > Taking this a step further, you can have a business model project (class library/Nuget) that is shared across multiple projects within an organization. This can work in some situations, but there are good reasons not to do this. Different teams may have differing and sometimes conflicting meanings for some of the terms of the domain (especially for generic concepts such as "user") as well as different data needs depending on the use case. This is why for instance Eric Evans’ "Domain-Driven Design" [2] argues for limiting domain model unification to "bounded contexts" [3] of which there may be multiple in the organization, and have explicit translation ("anti-corruption“) layers between the boundaries of these contexts. [1]: https://fsharpforfunandprofit.com/posts/designing-with-types-making-illegal-states-unrepresentable/ https://fsharpforfunandprofit.com/posts/designing-with-types... [2]: https://www.goodreads.com/book/show/179133.Domain_Driven_Design https://www.goodreads.com/book/show/179133.Domain_Driven_Des... [3]: https://www.martinfowler.com/bliki/BoundedContext.html https://www.martinfowler.com/bliki/BoundedContext.html
- jdmichal 7y agoThe problem is that people never properly understood which operations were supposed to go on objects, and they just started accumulating everything that was even loosely related. My favorite example is registering for university courses. So you have a Student object and a Course object... Which one does the register function go on? Objects should only contain those methods which are required for the invariants of that object. A course registration function is a workflow that involves two types, but does not control their invariants, so it belongs on neither. Most business logic falls into this bucket, and that bucket really works best as a functional paradigm where one can freely recombine the logic to create new workflows. (The worst answer I've seen is that both end up with a register method, with one just reversing the parameters. So Student.register(Course) just calls Course.register(Student).)
- kingdomcome50 7y agoThere are well-defined patterns for such behavior. Domain services are introduced to protect invariants _between_ entities. Implementation is simple really. Student “fills out course registration form” (VO) and then Course “completes registration with filled out registration form”. Essentially the behavior of registering for the course is distributed between Student and Course as appropriate. The point I’m trying to make is that the problem is not inherit in OOP. The problem is that domain modeling is hard. If a developer cannot synthesize the above “solution”, I’m not sure a different paradigm is going to help.
- jdmichal 7y agoAnd why does a Course register a student? Why does it know what a "course registration form" is? None of these things have anything to do with the Course object or is invariants... The Course is just one of many parameters into the workflow. Why is it therefore responsible for the actual act of registration? In real life, the aptly named "student registrar" is responsible for performing the task. But they're not an entity within this system... Rather they are the context in which this system is running. So a non-bound function actually makes perfect sense... It's bound to the top-level registrar context of the system. In BPMN, the registrar would be one swimlane, and the registration the process, with additional swimlanes for other actors like the student. The registrar still owns the process, but BPMN allows for other actors to also play parts within the system. A relational view would introduce a Registration which would join a Course and Students. The workflow would create a Registration, which defacto means that an individual Registration cannot be responsible for executing this.
- gnaritas 7y agoThat's just old school procedural programming which suffers from all the issues OO was created to solve.
- kingdomcome50 7y agoThis is the antithesis of OOP. Behavior should be modeled with data, not around data. Given a new use-case (some mutation of state), how can you be sure that an invariant is not bypassed if invariants are modeled at the level of each use-case? Any solution to the above begs the question, as all you will have done is distributed your “domain object” across the filesystem. Your system would be better served to simply put all the rules and objects into a single file which brings us back to original claim.