locked
Entity Framework and adding DataAnnotations RRS feed

  • Question

  • Im going through a tutorial on EF 6 (6.3 if i recall).

    I have the below structure for a Solution i have separated.

    DAL (Class library)
    I add EF component and select DB first.
    Create a Partial class 

    Would this be the correct place to add DataAnnotations?

    The idea is when i add [Required] or whatever the surrounding attribute would be, how do i get my client to ensure the rule has not been broken i.e. If i have [Required] how would my client (could be a web app) know this field is required? 

    Thursday, June 11, 2020 3:41 PM

All replies

  • Would this be the correct place to add DataAnnotations?

    Dataannotation in the DAL is not the optimal approach IMO.

    The idea is when i add [Required] or whatever the surrounding attribute would be, how do i get my client to ensure the rule has not been broken i.e. If i have [Required] how would my client (could be a web app) know this field is required? 

    Dataannotations can be applied to an object that works in the presentation layer, like a viewmodel in ASP.NET MVC.

    https://www.dotnettricks.com/learn/mvc/understanding-viewmodel-in-aspnet-mvc

    Business rule logic can  applied against an object passing through the business logic layer, which would be like a DTO passing through the BLL on its way to the DAL to be persisted to the database. And when I say business rules logic, I am not talking about dataannothation. 

    https://en.wikipedia.org/wiki/Separation_of_concerns

    A DTO travels passing through the layers,  and the EF entity is left at the DAL

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    Example of DAL using EF with DAO and DTO patterns.

    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DAL.Models;
    using Entities;
    using Microsoft.EntityFrameworkCore;
    namespace DAL
    {
        public class DaoArticle :IDaoArticle
        {
            private PublishingCompanyContext pc;
            private IDaoAuthor _daoAuthor;
            public DaoArticle(PublishingCompanyContext dbcontext, IDaoAuthor daoAuthor)
            {
                pc = dbcontext;
                _daoAuthor = daoAuthor;
            }
            public async Task<List<DtoArticle>> GetAll()
            {
                var dtos = new List<DtoArticle>();
                var articles = await pc.Article.ToListAsync();
                dtos.AddRange(articles.Select(article => new DtoArticle()
                {
                    ArticleId = article.ArticleId,
                    AuthorId = article.AuthorId,
                    Title = article.Title,
                    Body = article.Body
                }).ToList());
                return dtos;
            }
            public async Task<List<DtoArticle>> GetArticlesByAuthorId(int id)
            {
                var dtos = new List<DtoArticle>();
                var articles = await pc.Article.Where(a => a.AuthorId.ToString().Contains(id.ToString())).ToListAsync();
              
                foreach (var article in articles)
                {
                    var intid = (int)article.AuthorId;
                    var dtoauthor = await _daoAuthor.Find(intid);
                    var dto = new DtoArticle
                    {
                        ArticleId = article.ArticleId,
                        AuthorId = article.AuthorId,
                        AuthorName = dtoauthor.LastName +", " + dtoauthor.FirstName,
                        Title = article.Title,
                        Body = article.Body
                    };
                    dtos.Add(dto);
                }
                
                return dtos;
            }
            public async Task<DtoArticle> Find(int id)
            {
                var dto = new DtoArticle();
                var article = await pc.Article.FindAsync(id);
               
                if (article != null)
                {
                    dto.ArticleId = article.ArticleId;
                    dto.AuthorId = article.AuthorId;
                    dto.Title = article.Title;
                    dto.Body = article.Body;
                }
                else
                {
                    throw new Exception($"Article with ID = {id} was not found.");
                }
                return dto;
            }
            public async Task Add(DtoArticle dto)
            {
                var article = new Models.Article
                {
                    AuthorId = dto.AuthorId,
                    Title = dto.Title,
                    Body = dto.Body
                };
                pc.Article.Add(article);
                await pc.SaveChangesAsync();
            }
            public async Task Update(DtoArticle dto)
            {
                var article = new Article
                {
                    ArticleId = dto.ArticleId,
                    AuthorId = dto.AuthorId,
                    Title = dto.Title,
                    Body = dto.Body
                };
                pc.Entry(article).State = EntityState.Modified;
                await pc.SaveChangesAsync();
            }
            public async Task Delete(int id)
            {
                var article = pc.Article.Find(id);
                if (article != null)
                {
                    pc.Article.Remove(article);
                    await pc.SaveChangesAsync();
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DAL.Models;
    using Entities;
    using Microsoft.EntityFrameworkCore;
    
    namespace DAL
    {
        public class DaoArticle :IDaoArticle
        {
            private PublishingCompanyContext pc;
            private IDaoAuthor _daoAuthor;
    
            public DaoArticle(PublishingCompanyContext dbcontext, IDaoAuthor daoAuthor)
            { 
                pc = dbcontext;
                _daoAuthor = daoAuthor;
            }
            public async Task<List<DtoArticle>> GetAll()
            {
                var dtos = new List<DtoArticle>();
    
                var articles = await pc.Article.ToListAsync();
    
                dtos.AddRange(articles.Select(article => new DtoArticle()
                {
                    ArticleId = article.ArticleId,
                    AuthorId = article.AuthorId,
                    Title = article.Title,
                    Body = article.Body
                }).ToList());
    
                return dtos;
            }
    
            public async Task<List<DtoArticle>> GetArticlesByAuthorId(int id)
            {
                var dtos = new List<DtoArticle>();
    
                var articles = await pc.Article.Where(a => a.AuthorId.ToString().Contains(id.ToString())).ToListAsync();
               
                foreach (var article in articles)
                {
                    var intid = (int)article.AuthorId;
    
                    var dtoauthor = await _daoAuthor.Find(intid);
    
                    var dto = new DtoArticle
                    {
                        ArticleId = article.ArticleId,
                        AuthorId = article.AuthorId,
                        AuthorName = dtoauthor.LastName +", " + dtoauthor.FirstName,
                        Title = article.Title,
                        Body = article.Body
                    };
    
                    dtos.Add(dto);
                }
                 
                return dtos;
            }
            public async Task<DtoArticle> Find(int id)
            {
                var dto = new DtoArticle();
    
                var article = await pc.Article.FindAsync(id);
                
                if (article != null)
                {
                    dto.ArticleId = article.ArticleId;
                    dto.AuthorId = article.AuthorId;
                    dto.Title = article.Title;
                    dto.Body = article.Body;
                }
                else
                {
                    throw new Exception($"Article with ID = {id} was not found.");
                }
    
                return dto;
    
            }
    
            public async Task Add(DtoArticle dto)
            {
                var article = new Models.Article
                {
                    AuthorId = dto.AuthorId,
                    Title = dto.Title,
                    Body = dto.Body
                };
    
                pc.Article.Add(article);
                await pc.SaveChangesAsync();
    
            }
    
            public async Task Update(DtoArticle dto)
            {
                var article = new Article
                {
                    ArticleId = dto.ArticleId,
                    AuthorId = dto.AuthorId,
                    Title = dto.Title,
                    Body = dto.Body
                };
    
                pc.Entry(article).State = EntityState.Modified;
                await pc.SaveChangesAsync();
    
            }
    
            public async Task Delete(int id)
            {
                var article = pc.Article.Find(id);
    
                if (article != null)
                {
                    pc.Article.Remove(article);
                    await pc.SaveChangesAsync();
                }
            }
    
        }
    }
    
    

    Friday, June 12, 2020 5:41 AM