问题
After this answer :
Why are these hashcodes equals?
I realized that GetHashCode intend is not to provide a unique identifier for an object.
The purpose of this test was that I have a business function with 6 parameters :
customerId
serviceId
startDate
EndDate
cmsThematicId
And I don't want to be able to call this function more than once with the same values
These parameters are inserted in the database, I could query with (customerId = @customerId
and serviceId = @serviceId
...), but I need to be efficient with a lot of combination so this is not a solution.
Edit :
Example :
Let's say I have a super user he need to register customers. A registration is made of 5 parameters : customerId, serviceId, startDate, EndDate, cmsThematicId. The process of registering is like this :
- you select the service (for instance "show hi with a big red button")
- you select the customer (who bought the service)
- you select the cmsThematicId (the web page if you want)
- you select the startDate (in a drop down list)
- you select the endDate (in a drop down list)
My form can't show a set of parameters that was already used.
For instance once the customer 1 is registered for the service "big red button" on the page "holidays in new york" for the month of january, the super user won't be see these set of parameters in the form.
So my process did this : - create all the possibility - compute each possibility's hashcode in a List - get the hashcode of each already used possibility (from the db) - remove the already used possibility from the list - display the form
problem is : hashcode are not unique , so I might remove a item even if it was not used.
is it clearer ?
回答1:
After each call, store your 6 values in a history table with a composed index. In your business function I would add a code to verify if those values are already in your history table and immediately exit if I found those values.
LATER EDIT: This is how you should query the History table:
IF EXISTS
(SELECT customerId FROM HIstory WITH (NOLOCK) WHERE customerId='<value>' AND serviceId='<value>' AND <add all your fields here>)
SELECT 1
ELSE
SELECT 0
This way, if there is already a record (meaning your business function has been already called with the params you are testing), the above query returns 1, otherwise 0.
So if 1, you need to stop.
If 0, you need to add the values to history table and execute the business logic. OR: execute the business logic THEN add the values to history table.
EDIT2: Initially, your History table is empty. Each call to your function will:
Query the Hitory table for existing values that match the values passed as parameters to your function
Populate the History table if needed.
That is, first thing your function does is to query the History table and (if needed) to insert a new set of values in that table. Basically the query will get executed each time you call the function you implemented because the query is done inside that function.
来源:https://stackoverflow.com/questions/14599976/how-to-prevent-parameters-to-be-used-again