Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:43:42+00:00 2026-05-26T13:43:42+00:00

I am developing a web api using WCF Web Api preview 5. At the

  • 0

I am developing a web api using WCF Web Api preview 5. At the moment I have a resource class fully functional, how ever I noticed my methods inside this resouce are getting complex.

For example:

[WebInvoke(UriTemplate = "{EhrID}/PhysicalTest",Method="POST")]
    public HttpResponseMessage<DTO.PhysicalTest> PostPhysicalTest(int EhrID, DTO.PhysicalTest PhysicalTestDTO)
    {
        var EHR = repository.FindById(EhrID);
        var PhysicalTest = Mapper.Map<DTO.PhysicalTest, PhysicalTest>(PhysicalTestDTO);

        if (PhysicalTest == null)
        {                
            var response = CreateResponseForException("No object to store", HttpStatusCode.BadRequest);
            throw new HttpResponseException(response);
        }

        try
        {
            if (EHR.PhysicalTests == null)
            {
                EHR.PhysicalTests = new List<PhysicalTest>();
            }
            PhysicalTest.CreationDate = DateTime.Now;
            EHR.PhysicalTests.Add(PhysicalTest);
            _unitOfWork.Commit();
            return new HttpResponseMessage<DTO.PhysicalTest>(PhysicalTestDTO, HttpStatusCode.Created);
        }
        catch (Exception ex) {                
            var response = CreateResponseForException("Cannot create Physical Test", HttpStatusCode.InternalServerError);
            throw new HttpResponseException(response);
        }
    }

As you may notice this method has the task of posting a new Physical Test, but it’s actually validating my model too (I’m missing lots of validations still, property validations), which should not be this class concern. If there any approachable way to reduce the complexity of the methods inside de resource?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:43:43+00:00Added an answer on May 26, 2026 at 1:43 pm

    I would split it up into smaller more focused methods. I might also start using instance variables instead of passing all these arguments around, but for the sake of this post I’ve rewritten it without pushing stuff to instance variables.

    [WebInvoke(UriTemplate = "{EhrID}/PhysicalTest",Method="POST")] 
    public HttpResponseMessage<DTO.PhysicalTest> PostPhysicalTest(int EhrID, DTO.PhysicalTest PhysicalTestDTO) 
    { 
        var EHR = repository.FindById(EhrID); 
        var PhysicalTest = Mapper.Map<DTO.PhysicalTest, PhysicalTest>(PhysicalTestDTO); 
    
        if (PhysicalTest == null) 
        {                 
            var response = CreateResponseForException("No object to store", HttpStatusCode.BadRequest); 
            throw new HttpResponseException(response); 
        } 
    
        PostPhysicalTest(EHR, PhysicalTest);
        return new HttpResponseMessage<DTO.PhysicalTest>(PhysicalTestDTO, HttpStatusCode.Created);
    }
    
    private void PostPhysicalTest(EHR ehr, PhysicalTest physicalTest)
    {
        try 
        { 
            CreatePhysicalTest(ehr, physicalTest);
        } 
        catch (Exception ex) {                 
            var response = CreateResponseForException("Cannot create Physical Test", HttpStatusCode.InternalServerError); 
            throw new HttpResponseException(response); 
        }
    }
    
    private void CreatePhysicalTest(EHR ehr, PhysicalTest physicalTest)
    {
        if (ehr.PhysicalTests == null) 
        { 
            ehr.PhysicalTests = new List<PhysicalTest>(); 
        }
    
        physicalTest.CreationDate = DateTime.Now; 
        ehr.PhysicalTests.Add(physicalTest); 
        _unitOfWork.Commit();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The background I'm developing a REST API for a C#.NET web application using WCF.
I am developing an custom API for a web solution and I am using
I'm developing a web service using NHibernate, WCF and Oracle 11g R1. The web
I am developing web-application using Google Maps API and ASP.NET Ajax. Here is my
I am developing a mobile web app using dojo. I have a view with
I've been developing Web applications for a while now and have dipped my toe
I've been developing web applications for 2 years now, and have been into simpler
I'm getting started in developing web services using JAX-WS. I'm trying to implement classes
currently, I am developing web-apps using Eclipse. The build is done using Maven. The
I'm developing a Web API, and one of the test I came up with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.