Validate duplicate keys, is it possible?

Last post 10-07-2009 6:53 PM by Sgro. 2 replies.

Sort Posts:

  • Validate duplicate keys, is it possible?

    10-07-2009, 2:38 PM
    • Participant
      762 point Participant
    • Sgro
    • Member since 06-10-2006, 3:55 AM
    • Posts 342

    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.


    Thanks.

    Matteo Mosca
    Web Developer
    IWA Member
  • Re: Validate duplicate keys, is it possible?

    10-07-2009, 3:06 PM
    • Star
      14,584 point Star
    • david wendelken
    • Member since 07-27-2005, 7:47 PM
    • Fayetteville, NC, USA
    • Posts 2,076
    • Moderator

     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.

    If this answered your question, be sure to mark it as the answer. That way, everybody after you will know it's the answer also!
  • Re: Validate duplicate keys, is it possible?

    10-07-2009, 6:53 PM
    • Participant
      762 point Participant
    • Sgro
    • Member since 06-10-2006, 3:55 AM
    • Posts 342

    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.

    Matteo Mosca
    Web Developer
    IWA Member
Page 1 of 1 (3 items)