none
How to perform crud operations using design patterns? RRS feed

  • Question

  • Hi All,

        I want to perform crud operations with out using entity frame work related patterns(like repository,...).  Please guide me with an example(s). Waiting for valuable replies.

    • Moved by CoolDadTx Wednesday, November 20, 2019 7:27 PM EF related
    Wednesday, November 20, 2019 3:28 PM

All replies

  • Hi All,

        I want to perform crud operations with out using entity frame work related patterns(like repository,...).  Please guide me with an example(s). Waiting for valuable replies.

    The repository pattern is not an EF related pattern, in general. However, EF does use the repository pattern itself. But in application development and using design patterns, the repository pattern is a domain pattern and not a data persistence pattern.

    https://martinfowler.com/eaaCatalog/repository.html

    On the other hand, I use the DAO pattern in the data access layer pattern, along with using the DTO pattern.

    https://www.geeksforgeeks.org/data-access-object-pattern/

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

    https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm

    https://www.red-gate.com/simple-talk/dotnet/net-framework/net-application-architecture-the-data-access-layer/

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

    I even user these patterns in using EF in a layered architectural design. You also can use the patterns without using EF.

    Example..

    using System.Collections.Generic;
    using System.Threading.Tasks;
    using Entities;
    
    namespace DAL
    {
        public interface IDaoArticle
        {
            Task<List<DtoArticle>> GetAll();
            Task<List<DtoArticle>> GetArticlesByAuthorId(int id);
            Task<DtoArticle> Find(int id);
            Task Add(DtoArticle dto);
            Task Update(DtoArticle dto);
            Task Delete(int id);
        }
    }
    
    ==========================================================
    
    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 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();
                }
            }
    
        }
    }
    
    ===========================================================
    
    
    namespace Entities
    {
        public class DtoArticle
        {
            public int ArticleId { get; set; }
            public string Title { get; set; }
            public string Body { get; set; }
            public int AuthorId { get; set; }
            public string AuthorName { get; set; }
        }
    }
    

    Wednesday, November 20, 2019 4:50 PM