NET5 HTTP PUT to call HTTP GET

wavemaster 311 Reputation points
2021-05-03T20:13:54.933+00:00

How do I call GetServiceSingle action method from my PUT action method and populate svcItem?

HTTP PUT

[Route("Update/{svcGridItem}")]
        [HttpPut]
        public async Task<ActionResult<ServiceNew>> UpdateService(ServiceGrid svcGridItem)
        {

            Service svcItem = new Service();

            *??
            call GetServiceSingle with svcGridItem.ServiceId and populate svcItem
            ??*

            //peform the update
            svcItem.ServiceName = svcGridItem.ServiceName;
            svcItem.ShortForm = svcGridItem.ShortForm;


            context.Entry(svcItem).State = EntityState.Modified;
            await context.SaveChangesAsync();
            return NoContent();
        }

HTTP GET

[Route("[action]")] 
 [HttpGet]
        public async Task<ActionResult<List<ServiceSingle>>> GetServiceSingle(int id) => await context.Services
            .Where(s => s.ServiceId == id)
            .Select(s => new ServiceSingle
            {
                ServiceId = s.ServiceId,
                ServiceName = s.ServiceName,
                RoleId = s.RoleId,
                IsProcedure = s.IsProcedure,
                ShortForm = s.ShortForm,
                Maincat = s.Maincat,
                Subcat = s.Subcat,
                Strength = s.Strength,
                Package = s.Package,
                Size = s.Size,
                Note = s.Note
            })
            .ToListAsync();
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,014 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Duane Arnold 3,221 Reputation points
    2021-05-03T21:43:11.65+00:00

    @wavemaster

    The code for both those action methods could be put into a class in the Models folder, and the controller action methods could call the methods in the Models' class in an async manner as needed, or one method in the Models' class can call the other method in the Models' class in an async manner, as examples.

    It's about seperation of concerns.

    https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/architectural-principles

    https://en.wikipedia.org/wiki/Separation_of_concerns

    <copied>

    In computer science, separation of concerns (SoC) is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program.

    <end>

    So don't get SoC twisted, becuase the wiki link talks about layers that is another form of using SoC.

    It's about getting the code out of controller action methods, keeping the controller thin and have controller action methods call methods on classes/objects in the Models folder by using SoC principles.

    0 comments No comments

  2. wavemaster 311 Reputation points
    2021-05-03T23:17:36.033+00:00

    Thanks for your big picture suggestion, currently I am working on a very small scope which is getting data in and out of the db. Maintainability concerns, or best practices are light years away.

    First I need to:
    master the crud techniques
    add authentication/authorization
    add Blazor WASM
    add PWA functionality
    add offline capabilities
    learn to publish

    This app modernization effort is progressing very slowly as my team is very small: me, myself and I (and we all work on the same thing).

    Refinement comes some point down the road.

    Unless what I have cannot work, I need to stay the course and accept that my implementation is not ideal for the time being.

    0 comments No comments

  3. Duane Arnold 3,221 Reputation points
    2021-05-04T01:52:08.513+00:00

    Thanks for your big picture suggestion, currently I am working on a very small scope which is getting data in and out of the db. Maintainability concerns, or best practices are light years away.

    You seem to miss the point about code reuse. It's not about maintainability and best practices. which shouldn't be ignored either. It's about seeing how to use code in more than one place instead of it being tightly coupled to one usage in an action method in a controller that is forcing you to ask the question you are asking and seeking a way out. IMHO, you have been given the answer you seem to ignore.

    https://codeburst.io/interfaces-in-c-write-reusable-and-maintainable-code-that-scales-a144ca06b1f7

    You should come out the gate right instead maybe trying to think about backtracking and do something on a refactoring at a later time that's probably not going to happen.

    0 comments No comments