I have an empty list accepting string values.
When an element will be added I need to check if a string with the same value already exist in the list, if yes an exception should occur (ROLE 1 only unique value in the array).
Now I would like a user lets edit element in the list, if the new edited value is unique fine, otherwise an exception should occur.
My problem is: let’s imagine the user select the edit element but actually does not change the value when sending to the server. The system should detect that the element has not being changed and accept the value (even if is already present in the list).
PS I simplificate the problem, I’m actually using MVC and EF & linq. My problem is that I cannot check if the value inserted has been edited or not in the interface of my application.
Could you help me out to find an effective algo to solve this problem? Thanks
Let me know if the question is enough clear or you need more information
This is my solution:
Let the client check if the text was modified. If not, tell the server there was no modification. If this is not possible have the client send both the orinigial text and the modified text, and then the server will be able to check if it was modified. (if it wasn’t then there is no need to touch the data you are storing). This applies for both desktop and web enviroments.
Preferibly don’t use a list, use a set (may be a hashset). The set will only allow to have each item once. In case you can’t I guess you can continue using the list. If I understand correctly you are using a database, so if you could interface with the database engine directly instead of syncing the list (or set) your application will have a performance boost.
Convert the edition of the list/set into a add-remove pair (instead of setting the item). Have it check if the list/set contains the new value before doing any modification. If the new value is already present then you can throw an exception or send a message to the client (or whatever is more appropiate for your enviroment).
You may want to sync the access to your list/set, keep it simple: use a lock (Monitor). I would consider a read-write lock, but that is complicating things while you are learning. [If you interface directly with the database engine, you can let it handle that instead]. Note: there is no need for this if you will only have one single client… ever (unless that single client can send multiple concurrent requests… :P).