I'm wondering how you would impliment in your SqlRepository namespace (eg. the Transaction class) a unique field during a save.
So image the class Commerce.Data.SqlRepository.Transactions has a new, make believe property called ReceiptNumber, and it's a string that can contain .. um .. lets say numbers and characters. Nothing to complex. Eg. ReceiptNumber: CA0001Ax4
Ok. now in the database, that table has a Unique Key Index on the new ReceiptNumber field (on that Transaction table). This is because we want to make sure that the receipt number is always unique in the system, and the user had made that 'number' up on
the fly .. lets say.
So when you now save a new order (SqlOrderRepository.cs line 339) ... what do we need to do to make sure that the receipt doesn't exist in the system BEFORE the save?
Do we do a simple
int id = (from t in db.Transactions
where t.ReceiptNumber.Equals(receiptNumber)
select t.Id).SingleOrDefault() ?? 0;
then save if it's 0, otherwise throw an exception or something?
I know i can just set it and then save and allow the database to throw the error and i sorta bubble that back up .. but i was hoping to be a bit more proactive.
thoughts? Or ... should we worry about these type of database constraints in the repository level if we have prior-knowledge of it?
NOTE: this is against the MVC Storefont Preview 1A code from codeplex.
thanks folks!
:: Never underestimate the predictability of stupidity ::
I am doing something similar and was thinking of having a two step approach.
<div mce_keep="true">Validate the entry of the ReceiptNumber on the form. I am going to add a Status IsReceiptNumberValid(string receipt) method to my OrderService. This checking routine will check for correct structure with a regex as well as checking
for uniqueness. The Status result is just a simple class which incorporates Good/Bad result with a message if the status is Bad. This way I can give the user descriptive feedback as soon as possible. </div>
<div mce_keep="true">Just let the Save fail if the uniqueness constraint is violated. It seems to me that you always have to handle the save failing because however much you check before saving there may be a race condition with another user which can
still cause the save to fail. Of course, you want to catch the exception generated during the save and let the user know.</div>
Basically, I don't think the repository is the right place to check this as it is really business logic and I would like to be able to test it easily and keep my repositories simple.
i haven't :( i was looking at this again, over the entire weekend (yeah .. my private life == coding also when the missus and son are away). I'm been tempted to go your 2nd option for all the reasons u've stated. I cringe at the though of knowing the database
could throw exceptions under 'normal' conditions (as opposed to some edge case). Another example of this problem has been for Url slugs -- making sure that when u save this into the db, it's unique.
:: Never underestimate the predictability of stupidity ::
pure.krome
Member
532 Points
349 Posts
Question about Commerce.Data.SqlRepository.Transactions class.
Dec 14, 2008 05:02 AM|LINK
Hi Rob & storefront friends!
I'm wondering how you would impliment in your SqlRepository namespace (eg. the Transaction class) a unique field during a save.
So image the class Commerce.Data.SqlRepository.Transactions has a new, make believe property called ReceiptNumber, and it's a string that can contain .. um .. lets say numbers and characters. Nothing to complex. Eg. ReceiptNumber: CA0001Ax4
Ok. now in the database, that table has a Unique Key Index on the new ReceiptNumber field (on that Transaction table). This is because we want to make sure that the receipt number is always unique in the system, and the user had made that 'number' up on the fly .. lets say.
So when you now save a new order (SqlOrderRepository.cs line 339) ... what do we need to do to make sure that the receipt doesn't exist in the system BEFORE the save?
Do we do a simple
int id = (from t in db.Transactions where t.ReceiptNumber.Equals(receiptNumber) select t.Id).SingleOrDefault() ?? 0;then save if it's 0, otherwise throw an exception or something?
I know i can just set it and then save and allow the database to throw the error and i sorta bubble that back up .. but i was hoping to be a bit more proactive.
thoughts? Or ... should we worry about these type of database constraints in the repository level if we have prior-knowledge of it?
NOTE: this is against the MVC Storefont Preview 1A code from codeplex.
thanks folks!
Paul Linton
Star
13591 Points
2571 Posts
Re: Question about Commerce.Data.SqlRepository.Transactions class.
Jan 12, 2009 12:16 AM|LINK
How did you end up handling this?
I am doing something similar and was thinking of having a two step approach.
Basically, I don't think the repository is the right place to check this as it is really business logic and I would like to be able to test it easily and keep my repositories simple.
Paul
pure.krome
Member
532 Points
349 Posts
Re: Question about Commerce.Data.SqlRepository.Transactions class.
Jan 12, 2009 02:43 AM|LINK
i haven't :( i was looking at this again, over the entire weekend (yeah .. my private life == coding also when the missus and son are away). I'm been tempted to go your 2nd option for all the reasons u've stated. I cringe at the though of knowing the database could throw exceptions under 'normal' conditions (as opposed to some edge case). Another example of this problem has been for Url slugs -- making sure that when u save this into the db, it's unique.