Replies: 4 comments 9 replies
-
If I understand your question correctly, your ValidDepartment rule will run a command object (or something similar) and if the rule returns 'false' it will make the object invalid and hence you can't save the object. |
Beta Was this translation helpful? Give feedback.
-
I can think of 2 ways of doing this.
|
Beta Was this translation helpful? Give feedback.
-
Yes, I think the switch based on location is your best bet. There is a RunModes enum that you can use to define where your rule can run and I wondered if you could use that. However, it doesn't cater for the specific scenario you want - you can stop a rule running on the server, but you can't stop it running on the client. One thing you could do is cache the list against which you are running on the client, so that the rule still runs, but does so against cached data. Then do a lookup against the database on the server. However, unless you are using a database transaction and locking the table against which you are testing, there is still a chance that the database changes between when you run the rule and when you apply the change to the database even if you hit the database as part of the server-side save operation. I suppose you have to bear in mind that you are getting into the realm of diminishing returns. How hard you should work to completely resolve the issue depends on the scenario. The most agile of approaches might be to try ignoring the problem and see if it happens in production. Fix it only if it really happens, when you have more visibility of real world usage. |
Beta Was this translation helpful? Give feedback.
-
Running the rule on the server still doesn't guarantee that in the millisecond after the rule completes but before the save to the database someone else hasn't deleted it. If the database is relational it will enforce the foreign key integrity anyway, so this is the ultimate validation check for something which ultimately is extremely unlikely to happen. In my opinion, once the object is in the process of being saved on the server the assumption should be that the data is valid, and let the database handle FKs, etc. If a business rule fails on the server, it's not a very nice user experience either, because it looked like everything was fine when they clicked Save. So, if really you want to validate the existence of the department, you should run it |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hopefully I can explain this in a meaningful way!
I have been looking at the BlazorExample solution
There doesn't seem to be an example Rule showing what happens when a data lookup is required to validate the data
For example if the Person BO had a DepartmentID column that references the Department table in the database
On the Blazor UI it could obtain a list of departments and populate a dropdown for the user to choose a department
When a department was chosen the DepartmentID field in the BO would be set. At this stage there could be a ValidDepartment Rule that checked that the department existed in the List used to populate the Department dropdown OR it could just assume that because the Department had been selected from the dropdown then it existed (which of those is correct is not the point of this post). In either case there is no need to go back to the server /database to get the latest Department list if you want the UI to be as reactive as possible
When the Person BO is then saved back at the Blazor server it would need to run the ValidDepartment Rule to validate against the actual Department data in the database in case the chosen Department had been deleted or made invalid by another user whilst this user had been editing the Person (i.e. the server side Rule would always use live data as data from the client shouldn't be assumed to be valid)
I'm not sure how the Rule could be written so that it operated differently under Blazor Client vs Blazor Server or perhaps I should be thinking about this another way
Beta Was this translation helpful? Give feedback.
All reactions