Controller with TemplateService

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am working on my template web api project.



I just wanna hear any suggestion how to improve this code. I heard that repository pattern is anti pattern so I want to make code without it. Any opinion will be great :D



Service



public class TemplateService : ITemplateService
{
private readonly ContextTemplate context;

public TemplateService(ContextTemplate context)

this.context = context;


public int AddTemplate(TemplateInput input)

var template = new TemplateModel()

Name = input.Name,
CreatedDate = DateTime.UtcNow
;

context.TemplateModel.Add(template);
context.SaveChanges();
return template.Id;


public int UpdateTemplate(TemplateInput input)

var template = GetTemplate(input.Id);
if (template != null)
template.Name = input.Name;
template.ModifiedDate = DateTime.UtcNow;
context.Update(template);
context.SaveChanges();

return (int)template?.Id;


public bool ArchiveTemplate(int templateId)

var isArchiveSuccessfull = false;
var template = GetTemplate(templateId);
if (template != null)

template.IsArchived = true;
isArchiveSuccessfull = true;
context.Update(template);
context.SaveChanges();

return isArchiveSuccessfull;


public IEnumerable<TemplateModelDto> GetTemplates()

return Mapper.Map<List<TemplateModel>, List<TemplateModelDto>>(this.context.TemplateModel.Where(x=>!x.IsArchived).ToList());


public TemplateModel GetTemplate(int templateId)

var template = context.TemplateModel.FirstOrDefault(x => x.Id == templateId);
return template;



Controller



 [Route("api/[controller]")]
public class TemplatesController : Controller

private readonly ITemplateService templateService;
private readonly ILogger logger;

public TemplatesController(ITemplateService templateService, ILogger<TemplatesController> logger)

this.templateService = templateService;
this.logger = logger;

// GET api/templates
[HttpGet]
public IEnumerable<TemplateModelDto> Get() => templateService.GetTemplates();

// GET api/templates/5
[HttpGet("templateId")]
public TemplateModel GetTemplate(int templateId) => templateService.GetTemplate(templateId);

// POST api/templates
[HttpPost]
public int Post([FromBody]TemplateInput input) => templateService.AddTemplate(input);

// PUT api/templates/5
[HttpPut("templateId")]
public int Put(int templateId, [FromBody]TemplateInput input) => templateService.UpdateTemplate(input);

// DELETE api/templates/5
[HttpDelete("templateId")]
public Boolean ArchiveTemplate(int templateId) => templateService.ArchiveTemplate(templateId);







share|improve this question



























    up vote
    1
    down vote

    favorite












    I am working on my template web api project.



    I just wanna hear any suggestion how to improve this code. I heard that repository pattern is anti pattern so I want to make code without it. Any opinion will be great :D



    Service



    public class TemplateService : ITemplateService
    {
    private readonly ContextTemplate context;

    public TemplateService(ContextTemplate context)

    this.context = context;


    public int AddTemplate(TemplateInput input)

    var template = new TemplateModel()

    Name = input.Name,
    CreatedDate = DateTime.UtcNow
    ;

    context.TemplateModel.Add(template);
    context.SaveChanges();
    return template.Id;


    public int UpdateTemplate(TemplateInput input)

    var template = GetTemplate(input.Id);
    if (template != null)
    template.Name = input.Name;
    template.ModifiedDate = DateTime.UtcNow;
    context.Update(template);
    context.SaveChanges();

    return (int)template?.Id;


    public bool ArchiveTemplate(int templateId)

    var isArchiveSuccessfull = false;
    var template = GetTemplate(templateId);
    if (template != null)

    template.IsArchived = true;
    isArchiveSuccessfull = true;
    context.Update(template);
    context.SaveChanges();

    return isArchiveSuccessfull;


    public IEnumerable<TemplateModelDto> GetTemplates()

    return Mapper.Map<List<TemplateModel>, List<TemplateModelDto>>(this.context.TemplateModel.Where(x=>!x.IsArchived).ToList());


    public TemplateModel GetTemplate(int templateId)

    var template = context.TemplateModel.FirstOrDefault(x => x.Id == templateId);
    return template;



    Controller



     [Route("api/[controller]")]
    public class TemplatesController : Controller

    private readonly ITemplateService templateService;
    private readonly ILogger logger;

    public TemplatesController(ITemplateService templateService, ILogger<TemplatesController> logger)

    this.templateService = templateService;
    this.logger = logger;

    // GET api/templates
    [HttpGet]
    public IEnumerable<TemplateModelDto> Get() => templateService.GetTemplates();

    // GET api/templates/5
    [HttpGet("templateId")]
    public TemplateModel GetTemplate(int templateId) => templateService.GetTemplate(templateId);

    // POST api/templates
    [HttpPost]
    public int Post([FromBody]TemplateInput input) => templateService.AddTemplate(input);

    // PUT api/templates/5
    [HttpPut("templateId")]
    public int Put(int templateId, [FromBody]TemplateInput input) => templateService.UpdateTemplate(input);

    // DELETE api/templates/5
    [HttpDelete("templateId")]
    public Boolean ArchiveTemplate(int templateId) => templateService.ArchiveTemplate(templateId);







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am working on my template web api project.



      I just wanna hear any suggestion how to improve this code. I heard that repository pattern is anti pattern so I want to make code without it. Any opinion will be great :D



      Service



      public class TemplateService : ITemplateService
      {
      private readonly ContextTemplate context;

      public TemplateService(ContextTemplate context)

      this.context = context;


      public int AddTemplate(TemplateInput input)

      var template = new TemplateModel()

      Name = input.Name,
      CreatedDate = DateTime.UtcNow
      ;

      context.TemplateModel.Add(template);
      context.SaveChanges();
      return template.Id;


      public int UpdateTemplate(TemplateInput input)

      var template = GetTemplate(input.Id);
      if (template != null)
      template.Name = input.Name;
      template.ModifiedDate = DateTime.UtcNow;
      context.Update(template);
      context.SaveChanges();

      return (int)template?.Id;


      public bool ArchiveTemplate(int templateId)

      var isArchiveSuccessfull = false;
      var template = GetTemplate(templateId);
      if (template != null)

      template.IsArchived = true;
      isArchiveSuccessfull = true;
      context.Update(template);
      context.SaveChanges();

      return isArchiveSuccessfull;


      public IEnumerable<TemplateModelDto> GetTemplates()

      return Mapper.Map<List<TemplateModel>, List<TemplateModelDto>>(this.context.TemplateModel.Where(x=>!x.IsArchived).ToList());


      public TemplateModel GetTemplate(int templateId)

      var template = context.TemplateModel.FirstOrDefault(x => x.Id == templateId);
      return template;



      Controller



       [Route("api/[controller]")]
      public class TemplatesController : Controller

      private readonly ITemplateService templateService;
      private readonly ILogger logger;

      public TemplatesController(ITemplateService templateService, ILogger<TemplatesController> logger)

      this.templateService = templateService;
      this.logger = logger;

      // GET api/templates
      [HttpGet]
      public IEnumerable<TemplateModelDto> Get() => templateService.GetTemplates();

      // GET api/templates/5
      [HttpGet("templateId")]
      public TemplateModel GetTemplate(int templateId) => templateService.GetTemplate(templateId);

      // POST api/templates
      [HttpPost]
      public int Post([FromBody]TemplateInput input) => templateService.AddTemplate(input);

      // PUT api/templates/5
      [HttpPut("templateId")]
      public int Put(int templateId, [FromBody]TemplateInput input) => templateService.UpdateTemplate(input);

      // DELETE api/templates/5
      [HttpDelete("templateId")]
      public Boolean ArchiveTemplate(int templateId) => templateService.ArchiveTemplate(templateId);







      share|improve this question













      I am working on my template web api project.



      I just wanna hear any suggestion how to improve this code. I heard that repository pattern is anti pattern so I want to make code without it. Any opinion will be great :D



      Service



      public class TemplateService : ITemplateService
      {
      private readonly ContextTemplate context;

      public TemplateService(ContextTemplate context)

      this.context = context;


      public int AddTemplate(TemplateInput input)

      var template = new TemplateModel()

      Name = input.Name,
      CreatedDate = DateTime.UtcNow
      ;

      context.TemplateModel.Add(template);
      context.SaveChanges();
      return template.Id;


      public int UpdateTemplate(TemplateInput input)

      var template = GetTemplate(input.Id);
      if (template != null)
      template.Name = input.Name;
      template.ModifiedDate = DateTime.UtcNow;
      context.Update(template);
      context.SaveChanges();

      return (int)template?.Id;


      public bool ArchiveTemplate(int templateId)

      var isArchiveSuccessfull = false;
      var template = GetTemplate(templateId);
      if (template != null)

      template.IsArchived = true;
      isArchiveSuccessfull = true;
      context.Update(template);
      context.SaveChanges();

      return isArchiveSuccessfull;


      public IEnumerable<TemplateModelDto> GetTemplates()

      return Mapper.Map<List<TemplateModel>, List<TemplateModelDto>>(this.context.TemplateModel.Where(x=>!x.IsArchived).ToList());


      public TemplateModel GetTemplate(int templateId)

      var template = context.TemplateModel.FirstOrDefault(x => x.Id == templateId);
      return template;



      Controller



       [Route("api/[controller]")]
      public class TemplatesController : Controller

      private readonly ITemplateService templateService;
      private readonly ILogger logger;

      public TemplatesController(ITemplateService templateService, ILogger<TemplatesController> logger)

      this.templateService = templateService;
      this.logger = logger;

      // GET api/templates
      [HttpGet]
      public IEnumerable<TemplateModelDto> Get() => templateService.GetTemplates();

      // GET api/templates/5
      [HttpGet("templateId")]
      public TemplateModel GetTemplate(int templateId) => templateService.GetTemplate(templateId);

      // POST api/templates
      [HttpPost]
      public int Post([FromBody]TemplateInput input) => templateService.AddTemplate(input);

      // PUT api/templates/5
      [HttpPut("templateId")]
      public int Put(int templateId, [FromBody]TemplateInput input) => templateService.UpdateTemplate(input);

      // DELETE api/templates/5
      [HttpDelete("templateId")]
      public Boolean ArchiveTemplate(int templateId) => templateService.ArchiveTemplate(templateId);









      share|improve this question












      share|improve this question




      share|improve this question








      edited Feb 7 at 10:25









      t3chb0t

      32.1k54195




      32.1k54195









      asked Feb 6 at 22:47









      Wiktor Dębski

      83




      83




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You have abstracted away any concrete dependencies from the controller. I say that is well done for decoupling the controller and would allow better testability of the controller.



          ContextTemplate is assumed to be DbContext derived, which already implements Unit of Work and Repository pattern so there is really no use in re-implementing the pattern.



          IMO I see no immediate problems with the above code apart from the non use of the explicitly injected ILogger in the controller, which can be removed if there is no use for it in the controller. Logging can be considered a cross-cutting concern in asp.net-core and handled external to the controller.






          share|improve this answer





















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186956%2fcontroller-with-templateservice%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You have abstracted away any concrete dependencies from the controller. I say that is well done for decoupling the controller and would allow better testability of the controller.



            ContextTemplate is assumed to be DbContext derived, which already implements Unit of Work and Repository pattern so there is really no use in re-implementing the pattern.



            IMO I see no immediate problems with the above code apart from the non use of the explicitly injected ILogger in the controller, which can be removed if there is no use for it in the controller. Logging can be considered a cross-cutting concern in asp.net-core and handled external to the controller.






            share|improve this answer

























              up vote
              0
              down vote



              accepted










              You have abstracted away any concrete dependencies from the controller. I say that is well done for decoupling the controller and would allow better testability of the controller.



              ContextTemplate is assumed to be DbContext derived, which already implements Unit of Work and Repository pattern so there is really no use in re-implementing the pattern.



              IMO I see no immediate problems with the above code apart from the non use of the explicitly injected ILogger in the controller, which can be removed if there is no use for it in the controller. Logging can be considered a cross-cutting concern in asp.net-core and handled external to the controller.






              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                You have abstracted away any concrete dependencies from the controller. I say that is well done for decoupling the controller and would allow better testability of the controller.



                ContextTemplate is assumed to be DbContext derived, which already implements Unit of Work and Repository pattern so there is really no use in re-implementing the pattern.



                IMO I see no immediate problems with the above code apart from the non use of the explicitly injected ILogger in the controller, which can be removed if there is no use for it in the controller. Logging can be considered a cross-cutting concern in asp.net-core and handled external to the controller.






                share|improve this answer













                You have abstracted away any concrete dependencies from the controller. I say that is well done for decoupling the controller and would allow better testability of the controller.



                ContextTemplate is assumed to be DbContext derived, which already implements Unit of Work and Repository pattern so there is really no use in re-implementing the pattern.



                IMO I see no immediate problems with the above code apart from the non use of the explicitly injected ILogger in the controller, which can be removed if there is no use for it in the controller. Logging can be considered a cross-cutting concern in asp.net-core and handled external to the controller.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Feb 7 at 7:35









                Nkosi

                1,870619




                1,870619






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186956%2fcontroller-with-templateservice%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation