In examples of value Objects, I see that they validate before creating the object and thus avoid an invalid object.


Example:
That is fine when a user is entering a record, but what happens when the record is created through a DB mapper and there is invalid data? It will fail and the user will not know why
What is the strategy to follow?
If you don’t trust the data in your database, you sort of have larger problems than, “what does the UI do when the user is given garbage through no fault of their own”.
You’ll have to have some kind of validation for updating data. Read the (potential) garbage out of the database and immediately do not trust anything that might be coming back as an update. Verify that the object being saved is complete before an update is allowed back in.
This might mean you have to have your mappings allow incomplete models, or if you want to be severe about it, you might have to fail the load and tell the user that they can’t do what they want to do because the underlying data is broken.
At the end of the day, you do want a mechanism to fix your data and probably don’t want to rely on random user requests to find/fix the broken bits.
Business rules change and that can make the data stored correctly no longer valid.
Maybe a strategy would be to have a method that does the validation, but it doesn’t seem like a clean solution to me
First of all Automapper will not be able to create your object. You do not have empty public constructor. Once you introduce it you will be able to create bad DB record without automapper. So problem is not related to automapper.


So problem is not related to automapper.
That I know, it’s just to set an example
C# devs
null reference exceptions

source

Categories: .NetCodingCsharp