Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Dtos immutable #693

Merged
merged 1 commit into from
Feb 23, 2023
Merged

Conversation

IvanAntipov
Copy link
Contributor

The Problem

Immutable objects are safe to use.

Example:

void Foo(Dto dto)
{
   var res1 = service1.Bar(dto);
   var res2 = service2.Bar(dto);
   ...
   Some logic with res1
   ...
   Some logic with res2
   ...
}

If dto is immutable, one can safely remove or change the call to service1 and be sure, that logic related to res2 is not affected. But if dto is muttable, we have to investigate all possible call chains on service1 in order to make the refactoring, because I don't know in advance how dto is mutated during the call to service1.

The good practice is not to mutate objects which are passed as arguments to public methods. This makes maintenance much easier.

Currently, Dtos (under src/Application) are not immutable because they contain public setters fields of mutable types like IList

I'd like

Let's replace set with init Dtos.
Let's replace IList with IReadOnlyCollection all Dtos.

Alternatives you've considered

No alternatives :)

We can use arguably IReadonlyList instead of IReadOnlyCollection, but I would prefer the former because it is more minimalistic.

@IvanAntipov IvanAntipov marked this pull request as ready for review August 23, 2022 21:21
@jasontaylordev jasontaylordev merged commit e5eb97b into jasontaylordev:main Feb 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants