1

I am use DI in my controller class as documented here. The object I am passing with DI has to be used as dbset.

public class ValuesController : ControllerBase
{
    private readonly RPContext _context;
    private IResource _resource;

    public ValuesController(RPContext context, IResource resource)
    {
        _context = context;
        _resource = resource;
    }

    [HttpPost("{id}/{resourceName}")]
    public void Post([FromRoute] string id, [FromRoute] string resourceName, [FromBody] JObject Request)
    {
        _resource.Id = id;
        _resource.Name = resourceName;
        _resource.Location = (string)Request["location"];
        _context.Resources.Add(_resource);
        _context.SaveChangesAsync();
    }

I cannot use "_resource" in the above code with the Add method of Dbcontext object because it is an interface and not of class type. How else should I be doing this if I don't want to create new objects in my controller class?

4
  • This question contains a LOT of things: 1. The controller inherit to ControllerBase instead of Controller. 2. Using asynchronous task but returning void. 3. Try to cast an interface to a class... I suggest you to read the documentation careful and follow step-by-step to understand how it works. Or just download the project and run (via the link you provide). Commented Nov 10, 2018 at 7:34
  • async was a copy-paste mistake made while adding code here from older version, removed it. When you create web api project through Visual Studio, it only inerits from controllerbase. Seems like Controller is for MVC views and models which I don't need. Commented Nov 10, 2018 at 9:53
  • @TânNguyễn About ControllerBase: learn.microsoft.com/nl-nl/aspnet/core/web-api/… Commented Nov 10, 2018 at 14:04
  • @RuardvanElburg Indeed, we can use almost everything in Web API with a class that inherits from Controller, not just ControllerBase (I'm sure). And the question about the difference(s) may be in another post. I said that because: when I create new controller in my project (.net core 2.1), it inherits from Controller. Commented Nov 10, 2018 at 14:45

1 Answer 1

1

Please note that IResource is the interface of a service, not an object itself. So IResource isn't a new object. If you need a new object you can take a factory approach:

var resource = _resource.Create();

To add an entity to the context:

[HttpPost("{resourceName}")]
public async Task<IActionResult> Post([FromRoute]string resourceName, [FromBody]JObject Request)
{
    var resource = new Data.Entities.Resource
    {
        Name = resourceName,
        Location = (string)Request["location"]
    };
    _context.Resources.Add(resource);
    _context.SaveChangesAsync();

     return CreatedAtRoute("Get", new { id = resource.Id }, resource.Id);
}

Where the result links to the created object.

I noticed that you've included an Id for this request. But it seems that a new resource is created, so I've omitted the Id because it is probably set in the database.

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.