简体   繁体   中英

checking duplication of the title of an entity

I have a project in Asp net mvc. An entity called product can not have similar titles. As I am following repository pattern where is the best place to check the duplication?

I can do it in controller but it will lead to a fat controller.

I presume you're using a DB server? Put a unique constraint on the DB column. Doing this in the repository or controller introduces concurrency issues (another transaction, which you can't see since it's not committed yet, could have already inserted a duplicate value). Constraints can see through this.

If it is a very hard and fast rule, backing it with a database constraint is highly advisable--that will back-stop your code and make sure no slip-ups get through.

Insofar as enforcing the rule in code, how are you currently handling validation? This definitely falls into that category.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM