Validate duplicate keys, is it possible? RRS feed

  • Question

  • User1102450110 posted

    I was trying to create a custom validator for validation blocks, to validate a value against values contained in the data store.

    My idea was somehow passing my IQueryable to the validator, and it would internally use it to see if the repository already has an item with the same key.

    I can't seem to find the right approach to make it, so I was wondering if the Validation Blocks architecture can support a scenario like this.

    Anyone has tried to make it, or maybe succeeded? It seems like a very common scenario to me.


    Wednesday, October 7, 2009 2:38 PM

All replies

  • User1187105292 posted

     Recognize that even if you can do this, it CANNOT provide a definitive answer in a multi-user application.   You will still have to code to handle that error condition when the insert is issued.

    Wednesday, October 7, 2009 3:06 PM
  • User1102450110 posted

    Actually my point was quite different. Consider this simple scenario where we have Customer-Users with a one-to-many relationship, and my facade provides a method to insert customers.

    Let's say I insert a customer. I validate it through validation block, then before calling the data-layer savechanges, I check for duplicate key with a query, something like

    if (Context.Customers.Any(c => c.ID == newCust.ID)) // handle not valid

    In this case everrything is smooth. But what if my new customer also contains one or more new user? Something like

    Customer newCust = new Customer
    Name = "testCustomer",
    Users = new List<User>
    new User { Username = "test1" },
    new User { Username = "test2" }

    This is a common scenario when using the entity framework or something like it. Now let's repeat the procedure with this customer. Everything will be validated, even the users properties thanks to the ObjectCollectionValidator. What will NOT be validated is the "check" for duplicate keys for the users.

    But, if I had a duplicate key validator, it would be called like any other validator when the ObjectCollectionValidator kicks in.

    Since I consider this a very common scenario, I am wondering if no one else had this problem. I'm sure someone solved it in a good way, and I'd love to hear about it.

    Wednesday, October 7, 2009 6:53 PM