In web api controller, I used to create instance within using keyword
so Once it come out from the using, GC
will be called and memory will be released.
The code which I'm using right now,
public class TemplateController : AutoVHCBaseApiController
{
[Route("{id}")]
[HttpGet]
[ResponseType(typeof(VHC.Core.Common.Beans.CheckTemplate))]
public IHttpActionResult Get(int id)
{
try
{
using(ITemplateManager manager=new TemplateManager())
{
CheckTemplate checkTemplate = manager.GetCheckTemplate(id, SiteCode);
return Ok(checkTemplate);
}
}
catch (ValidationException ex)
{
return BadRequest(ex.Message, FailureReason.ReasonCodeOptions.ValidationError);
}
}
}
One of my collegue asked me to modify like below:
public class TemplateController : AutoVHCBaseApiController
{
private readonly ITemplateManager manager;
public TemplateController()
{
manager = new TemplateManager();
}
[Route("{id}")]
[HttpGet]
[ResponseType(typeof(VHC.Core.Common.Beans.CheckTemplate))]
public IHttpActionResult Get(int id)
{
try
{
CheckTemplate checkTemplate = manager.GetCheckTemplate(id, SiteCode);
return Ok(checkTemplate);
}
catch (ValidationException ex)
{
return BadRequest(ex.Message, FailureReason.ReasonCodeOptions.ValidationError);
}
}
}
Why should i have to create a instance at constuctor? What is the difference of the both code to creating instance?