1
public async Task<HttpResponseMessage> getOne(HttpRequestMessage request, int id)
{ 

   return CreateResponse(async () =>
    {
        var category = await _unitOfWork.Categories.GetSingleAsync(id);
        var categoryVm = Mapper.Map<Category, CategoryViewModel>(category);

        HttpResponseMessage response = request.CreateResponse<CategoryViewModel>(HttpStatusCode.OK, categoryVm);

        return response;
    });
}

Base Class

protected Task<IHttpActionResult> CreateResponse(Func<IHttpActionResult, Task> function)
{
    IHttpActionResult response = null;
    try
    {
        response = function.Invoke();

    }
}

1 Answer 1

1

Read up on Cross cutting concerns.

You are giving yourself unnecessary trouble. Your example can be reduced to :

public async Task<IHttpActionResult> getOne(int id) {
    var category = await _unitOfWork.Categories.GetSingleAsync(id);
    var categoryVm = Mapper.Map<Category, CategoryViewModel>(category);
    return Ok(categoryVm);
}

Try to keep controller lean.

Check this answer

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.