-1

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
                .FindAsync(id);

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

            _db.BlogPosts.Remove(post);
            await _db.SaveChangesAsync();

            return Ok();
        }
    }
}

Looking at the sample controller, I have moved most of the logic into the model through methods like Update() and the constructors. How do I also move the logic for the Include() methods to my model as well?

TylerH
  • 20,799
  • 66
  • 75
  • 101
  • by mean include if want to load navigation property then you can use Expression> 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 https://stackoverflow.com/questions/35888878/pass-a-lambda-parameter-to-an-include-statement – Uttam Ughareja Sep 16 '18 at 02:43

1 Answers1

0

Declaring method with parameter of expression of func of entity it will allow you to pass lambda expression while calling method

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

And then method can be called as follow

var entityWithNavigationProp = FindAll(entity=>entity.propertyName);

For more about detail have a look at Pass a lambda parameter to an include statement

And for expression check this

Uttam Ughareja
  • 842
  • 2
  • 12
  • 21
  • While this may answer the question, it would be a lot more useful if you included some explanation as to how it solves the problem. – Nick Sep 16 '18 at 05:01