How can I reduce redundancy in my controller?

2157 views asp.net-core
6

I use ASP.NET Core for the backend of my Azure App Service app. For each table in the database, I create a controller which acts as an API endpoint for that table. There are no problems with the code but for every Controller, I an repeating the same logic except for the Include(m => m.<Property>). Is there a way I can move all these logic into the parent TableController class and have the Include() method stuff in the model class.

These are some sample files:

Table Controller (parent class for all API controllers):

using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc;

namespace Backend.API
{
    public abstract class TableController<T> : Controller
    {
        // Public Methods
        [HttpGet]
        [Route("")]
        public abstract Task<IActionResult> GetAllAsync();

        [HttpPost]
        [Route("")]
        public abstract Task<IActionResult> CreateAsync([FromBody] T created);

        [HttpGet]
        [Route("{id}")]
        public abstract Task<IActionResult> GetAsync(string id);

        [HttpPatch]
        [Route("{id}")]
        public abstract Task<IActionResult> UpdateAsync(string id, [FromBody] T updated);

        [HttpDelete]
        [Route("{id}")]
        public abstract Task<IActionResult> DeleteAsync(string id);
    }
}

A sample Model:

using System;
using System.ComponentModel.DataAnnotations;

namespace Backend.Models.DB
{
    public class BlogPost
    {
        // Public Properties
        public DateTime DatePublished { get; set; }

        public Guid Id { get; set; }

        [Required]
        public string Body { get; set; }

        [Required]
        public string Title { get; set; }

        public string DatePublishedString =>
            string.Format("Posted on {0}.", DatePublished.ToString().ToLower());

        [Required]
        public User Publisher { get; set; }

        // Constructors
        public BlogPost() : this(null, null, null, new DateTime()) { }

        public BlogPost(BlogPost post) :
            this(post.Title, post.Body, post.Publisher, post.DatePublished) { }

        public BlogPost(string title, string body, User publisher, DateTime datePublished)
        {
            Title = title;

            if (datePublished == new DateTime())
                DatePublished = DateTime.Now;
            else
                DatePublished = datePublished;

            Body = body;
            Publisher = publisher;
        }

        // Public Methods
        public void Update(BlogPost updated)
        {
            Body = updated.Body;
            Title = updated.Title;
        }
    }
}

A sample Controller:

using System;
using System.Threading.Tasks;
using Backend.Models.DB;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Backend.API
{
    [Route("tables/BlogPost")]
    public class BlogPostController : TableController<BlogPost>
    {
        // Private Properties
        private readonly BadmintonClubDBDataContext _db;

        // Constructors
        public BlogPostController(BadmintonClubDBDataContext db) => _db = db;

        // Overridden Methods
        public override async Task<IActionResult> GetAllAsync()
        {
            var posts = await _db.BlogPosts
                .Include(bp => bp.Publisher)
                .ToArrayAsync();

            return Json(posts);
        }

        public override async Task<IActionResult> CreateAsync([FromBody] BlogPost created)
        {
            if (!ModelState.IsValid)
                return BadRequest();

            BlogPost post = (await _db
                .AddAsync(new BlogPost(created))).Entity;
            await _db.SaveChangesAsync();

            return Json(post);
        }

        public override async Task<IActionResult> GetAsync(string id)
        {
            BlogPost post = await _db.BlogPosts
                .Include(bp => bp.Publisher)
                .SingleOrDefaultAsync(bp => bp.Id == new Guid(id));

            if (post == null)
                return NotFound();

            return Json(post);
        }

        public override async Task<IActionResult> UpdateAsync(string id, [FromBody] BlogPost updated)
        {
            BlogPost post = await _db.BlogPosts
                .Include(bp => bp.Publisher)
                .SingleOrDefaultAsync(bp => bp.Id == new Guid(id));

            if (post == null)
                return NotFound();

            if (post.Id != updated.Id || !ModelState.IsValid)
                return BadRequest();

            post.Update(updated);

            await _db.SaveChangesAsync();

            return Json(post);
        }

        public override async Task<IActionResult> DeleteAsync(string id)
        {
            BlogPost post = await _db.BlogPosts
        

answered question

by mean include if want to load navigation property then you can use Expression<Func<T>> as a parameter of method, then you can pass whatever property you want to eager load as a lambda expressions. x=>x.propertyName. please check this stackoverflow.com/questions/35888878/…

1 Answer

11

public ICollection<TData> FindAll<TInclude>(Expr<Func<TData, TInclude>> include) {
    using (var ctx = new TContext()) {
        return ctx.T.Include(include).ToList();
    }
} 

posted this

Have an answer?

JD

Please login first before posting an answer.