Friend access modifier #7578
Replies: 4 comments 34 replies
-
See: #2073 |
Beta Was this translation helpful? Give feedback.
-
I'm not commenting on your proposal, but on the example you gave. I don't think the |
Beta Was this translation helpful? Give feedback.
-
I would assume the new “unsafe accessors” feature in .NET 8 would be Good Enough(tm) for such a situation. It’s not perfect, but it would work. |
Beta Was this translation helpful? Give feedback.
-
Relevant: If |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello,
When I design applications using DDD principles, I often encounter the following problem:
Let's imagine what I have an Order aggregate and an OrderItem entity inside it. E.g. this Order and this OrderItem.
Order has
public void AddOrderItem(int productId, string productName, decimal unitPrice, decimal discount, string pictureUrl, int units = 1)
method,and OrderItem has
public void AddUnits(int units)
method.According to DDD rules (see comment of AddOrderItem)
there is the only way to manipulate units count in OrderItem - througth the agregate root, i.e. using AddOrderItem in Order class.
And this is wery powerfull thing in tactical DDD, witch prevent a lot of error in my expirience.
But, AddUnits method in OrderItem is public. and can be accidentally called directly and this may cause loss of aggregate consistency.
We can define AddUnits as internal, but then we need create assembly per aggregate and this approach not prevent all unwanted calls.
It would be great to have a friend access modifier. Its behavior should resemble the internal modifier (including combinations with protected), but not for the entire assembly, but only for the specified list of classes.
It might look something like this:
Then we can use AddUnits only in Order class, what we wanted to achieve.
A feature like this should not be difficult to implement, since it is similar to the internal modifier. And unlike similar functionality in C++, it does not violate encapsulation.
Beta Was this translation helpful? Give feedback.
All reactions