locked
How to check uniqueness of a request value? RRS feed

  • Question

  • User-1104215994 posted

    Hi guys,

    I have a requirement that I should check the uniqueness of <g class="gr_ gr_50 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="50" data-gr-id="50">referenceId</g> value which comes in the request. Is it efficient looking for the value into the database table? At the beginning when there is not too much data, it seems querying <g class="gr_ gr_309 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="309" data-gr-id="309">database</g> is enough. But what if there are millions of data? Any ideas?

    Thank you.

    Friday, May 3, 2019 3:35 PM

Answers

  • User475983607 posted

    I thought only one request is enough to trigger the validation .When client actually makes a request to the contoller method and magic happens inside of the ModelState is valid portion. If I need two requests then I rather choose to query the database inside of controller method and return message if reference id is used.

    [System.Web.Http.HttpPost, System.Web.Http.Route("initiation")]
            public async Task<IHttpActionResult> PostInitiate(InitiateRequest initiate)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

    No, that is not how remote validation works.  Please set aside enough time to read the linked documentation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 14, 2019 5:14 PM

All replies

  • User475983607 posted

    cenk1536

    But what if there are millions of data?

    Add an index to the referenceId column.

    https://docs.microsoft.com/en-us/sql/relational-databases/indexes/clustered-and-nonclustered-indexes-described?view=sql-server-2017

    Add a unique constraint if the column cannot contain duplicate values.

    Friday, May 3, 2019 4:18 PM
  • User-1104215994 posted

    Does it make sense if I can check uniqueness of <g class="gr_ gr_38 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="38" data-gr-id="38">referenceId</g> value in a delegating handler before it reaches the action method?

    Sunday, May 5, 2019 4:02 PM
  • User475983607 posted

    cenk1536

    Does it make sense if I can check uniqueness of referenceId value in a delegating handler before it reaches the action method?

    I do not understand what advantage this approach provides.  Did you add an index to the referenceId column?

    Sunday, May 5, 2019 6:54 PM
  • User-1104215994 posted

    I will add something like below on <g class="gr_ gr_48 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="48" data-gr-id="48">referenceId</g> entity:

    [Index( "INDEX_REF", IsClustered=true, IsUnique=true )]

    If <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="13" data-gr-id="13">client</g> sends the same referenceId, I want to check the database table and give an appropriate response saying <g class="gr_ gr_6 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="6" data-gr-id="6">referenceId</g> should be unique. <g class="gr_ gr_7 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="7" data-gr-id="7">Thats</g> why I am asking does it make sense to check database table in the handler?

    Monday, May 6, 2019 5:29 AM
  • User-1038772411 posted

    You Can Refer this link Hope you will get your solution

    Monday, May 6, 2019 8:38 AM
  • User-1104215994 posted

    Which one is more suitable/efficient;

    • checking uniqueness when <g class="gr_ gr_157 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="157" data-gr-id="157">request</g> comes to <g class="gr_ gr_178 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="178" data-gr-id="178">action</g> method or
    • checking <g class="gr_ gr_201 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="201" data-gr-id="201">uniquenessin</g> the handler?
    Tuesday, May 7, 2019 1:18 PM
  • User753101303 posted

    Hi,

    To me it's basically a model validation. You could even use maybe use remote validation to give the user an immediate feedback :

    https://www.c-sharpcorner.com/UploadFile/d87001/remote-validation-in-mvc/

    I'm not 100% sure to get the exact "handler" based design you are talking about but it seems it would be suitable for a lower level purpose than that...

    Tuesday, May 7, 2019 1:36 PM
  • User-1104215994 posted

    thank you PatriceSc

    Tuesday, May 7, 2019 1:47 PM
  • User-1104215994 posted

    Should I install <g class="gr_ gr_12 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="12" data-gr-id="12">nugetpackage</g>? Which one?

    Wednesday, May 8, 2019 5:33 PM
  • User-1104215994 posted

    I don't have MVC in my references.

    Sunday, May 12, 2019 10:00 AM
  • User36583972 posted


    Hi cenk1536,

     

    Which one is more suitable/efficient;

    • checking uniqueness when request comes to action method or
    • checking uniquenessin the handler?

    Message handlers are good for cross-cutting concerns that operate at the level of HTTP messages (rather than controller actions). For example, a message handler might:

    Read or modify request headers.

    Add a response header to responses.

    Validate requests before they reach the controller.

    HTTP Message Handlers in ASP.NET Web API
    https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/http-message-handlers


    Best Regards

    Yong Lu

    Monday, May 13, 2019 8:32 AM
  • User-1104215994 posted

    I want to try remote validation as PatriceSc suggested. But I think I have to install MVC from <g class="gr_ gr_81 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="81" data-gr-id="81">nuget</g>. Somebody help me :)

    Monday, May 13, 2019 8:40 AM
  • User36583972 posted

    I want to try remote validation as PatriceSc suggested. But I think I have to install MVC from nuget. Somebody help me :)


    Hi cenk1536,

    You need to import the two namespaces "using System.ComponentModel.DataAnnotations" and
    "using System.Web.Mvc".


    Best Regards

    Yong Lu

    Tuesday, May 14, 2019 8:27 AM
  • User-1104215994 posted

    thank you I will try.

    Tuesday, May 14, 2019 11:03 AM
  • User-1104215994 posted

    ModelState validation does not catch the used reference id. Here is my code:

    public class InitiateRequest
        {
            
            //Using Remote validation attribute   
            [Remote("IsAlreadyUsed", "Initiates", HttpMethod = "POST", ErrorMessage = "referenceId has already used!")]
            [Required(ErrorMessage = "Please add Reference ID to the request.")]
            [MaxLength(50, ErrorMessage = "The Reference ID can not have more than 50 characters")]
            public string referenceId { get; set; }
    ....
    [System.Web.Http.HttpPost]
            public JsonResult<bool> IsAlreadyUsed(string referenceId)
            {
    
                return Json(IsUserAvailable(referenceId));
    
            }
            public bool IsUserAvailable(string referenceId)
            {
                // Assume these details coming from database  
                var result = context.InitiateRequests
                    .Where((l => l.referenceId == referenceId))
                    .FirstOrDefault();
    
                bool status;
                status = result == null;
    
    
                return status;
            }

    Tuesday, May 14, 2019 1:45 PM
  • User475983607 posted

    You did not post the client code that invokes the remote validation.

    Tuesday, May 14, 2019 2:33 PM
  • User-1104215994 posted

    I am sending via Postman.

    Tuesday, May 14, 2019 2:35 PM
  • User475983607 posted

    I am sending via Postman.

    You are having trouble configuring PostMan to make a POST request to the "IsAlreadyUsed" validation action? 

    Keep in mind, the logic flow requires two requests.  The client must make a request to the IsAlreadyUsed() action and if the request is successful then make a second request to the the INSERT/UPDATE action.

    Generally, the client is a JavaScript application using MVC's standard unobtrusive validation.  The Model Validation docs cover the fundamentals.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-2.2

    If this is a REST App and you do not have control over the client then MVC remote model validation is not a good option because the client must make the validation request before invoking the INSERT Action.  

    Tuesday, May 14, 2019 2:50 PM
  • User-1104215994 posted

    I thought only one request is enough to trigger the validation <g class="gr_ gr_193 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="193" data-gr-id="193">.</g><g class="gr_ gr_193 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style replaceWithoutSep" id="193" data-gr-id="193">When</g> <g class="gr_ gr_192 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="192" data-gr-id="192">client</g> actually makes a request to the <g class="gr_ gr_76 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="76" data-gr-id="76">contoller</g> method and magic happens inside of the ModelState is valid portion. If I need two requests then I rather choose to query the database inside of <g class="gr_ gr_558 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="558" data-gr-id="558">controller</g> method and return <g class="gr_ gr_517 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="517" data-gr-id="517">message</g> if reference id is used.

    [System.Web.Http.HttpPost, System.Web.Http.Route("initiation")]
            public async Task<IHttpActionResult> PostInitiate(InitiateRequest initiate)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

    Tuesday, May 14, 2019 4:47 PM
  • User475983607 posted

    I thought only one request is enough to trigger the validation .When client actually makes a request to the contoller method and magic happens inside of the ModelState is valid portion. If I need two requests then I rather choose to query the database inside of controller method and return message if reference id is used.

    [System.Web.Http.HttpPost, System.Web.Http.Route("initiation")]
            public async Task<IHttpActionResult> PostInitiate(InitiateRequest initiate)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

    No, that is not how remote validation works.  Please set aside enough time to read the linked documentation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 14, 2019 5:14 PM
  • User-1104215994 posted

    Yep, thank you I <g class="gr_ gr_33 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" id="33" data-gr-id="33">learnt</g>. I am using asp.net web <g class="gr_ gr_85 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="85" data-gr-id="85">api</g> and I gonna query DB for the uniqueness of reference id in the controller method or in a handler.

    Tuesday, May 14, 2019 5:17 PM
  • User475983607 posted

    Yep, thank you I learnt. I am using asp.net web api and I gonna query DB for the uniqueness of reference id in the controller method or in a handler.

    I do not understand how a handler is a good design approach.   I assume there is one Web API endpoint that does the INSERT/UPDATE.  So this is just a simple validation check in that single method.  Don't make a simple process overly complicated.

    Create a basic service (business logic) If the validation check is used in many actions.

    Tuesday, May 14, 2019 5:59 PM
  • User-1104215994 posted

    Thank you, I will listen to your <g class="gr_ gr_23 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="23" data-gr-id="23">advise</g>. Please give some advice about my JWT refresh token question as well.

    Tuesday, May 14, 2019 6:01 PM
  • User-2054057000 posted

    You can apply any searching algorithm to make the search faster. 

    See Searching Algorithms tutorial.

    Sunday, May 19, 2019 1:40 PM