none
How to assign any number of parameter keys on function Getbyid Repository pattern ? RRS feed

  • Question

  • Problem

    How to assign any number of parameter keys on function Getbyid Repository pattern ?

    I work on web app using repository pattern generics 

    in  repository interface getbyid function as following

    public T GetById(int Id)
            {
                return dbSet.Find(Id);
            }

    and in interface Igenerics

    T GetById(int Id)

    suppose i dont know how many parameters passing so that what i do and how to call it please 

    getbyid return keys for model may be keys for model 1key or 2 or 3 or 4 etc ..

    so that can you help me using dynamic datatype or  params object[] keyValues

    public T GetById(what i write here)
            {
                return dbSet.Find(Id);
            }
    and in interface Igenerics
    
    T GetById(what i write here)
    when call getbyid how to call it
    i need to use dynamic and params object[] keyValues
    


    Saturday, February 9, 2019 2:56 AM

Answers

  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by engahmedbarbary Wednesday, February 20, 2019 12:55 PM
    Tuesday, February 19, 2019 8:10 AM
    Moderator

All replies

  • can any one help me please
    Saturday, February 9, 2019 10:45 AM
  • can any one help me please

    I avoid the generic repository pattern, becuase it's too generic meaning you can't change the functionality to do something else. And the repository pattern is not a persistence pattern.

    https://blog.sapiensworks.com/post/2012/03/05/The-Generic-Repository-Is-An-Anti-Pattern.aspx

    https://www.infoworld.com/article/3117713/application-development/design-patterns-that-i-often-avoid-repository-pattern.html

    https://programmingwithmosh.com/entity-framework/common-mistakes-with-the-repository-pattern/

    I have nothing against the repository pattern when it is non generic, and it is used in its proper usage as a domain pattern using a data mapper

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

    http://www.bradoncode.com/blog/2013/08/repository-vs-domain-model-vs-data.html

    <from you>

    suppose i dont know how many parameters passing so that what i do and how to call it please 

    getbyid return keys for model may be keys for model 1key or 2 or 3 or 4 etc ..

    so that can you help me using dynamic datatype or  params object[] keyValues

    <end>

    Now,  if using the non generic repositroy and the data mapper patterns you can probably come up with whatever you're trying to do above there in functionality. 

    Your other alternative to the repository pattern is to use the DAO pattern.

    https://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

    You can use the DAO pattern with the repository pattern with the DAO pattern being used in the data mapper.

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

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

    Or you can just eliminate the repository and just go with the data access layer using the DAO pattern in the DAL.

    With the data mapper,  you can use the DTO pattern with passing the DTO or DTO(s) through the layers.

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

    Example code where I am just using the DAO and DTO pattern in the DAL with the Entity Framework. There is no repositroy being used in the solution.

    BTW, Web applications using ASP.NET can be discussed at the ASP.NET forums. 

    https://forums.asp.net/

    HTH

    using System;
    using System.Collections.Generic;
    using Entities;
    
    namespace DAL.DAO
    {
        public interface IDAOStudent
        {
            DTOStudent GetStudentById(Int32 id);
            List<DTOStudent> GetStudents();
            void CreateStudent(DTOStudent dto);
            void UpdateStudent(DTOStudent dto);
            void DeleteStudent(Int32 id);
        }
    }
    
    =============================================
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Data.Entity;
    using System.Data.Entity.Core.EntityClient;
    using System.Data.Entity.Core.Objects;
    using System.Data.Entity.Infrastructure;
    using System.Data.SqlClient;
    using Entities;
    using DAL.Model;
    
    namespace DAL.DAO
    {
        public class DAOStudent : IDAOStudent
        {
            public DTOStudent GetStudentById(Int32 id)
            {
                var dto = new DTOStudent();
                using (var context = new CUDataEntities())
                {
                    var student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
    
                    if (student != null)
                    {
                        dto.StudentID = student.StudentID;
                        dto.FirstName = student.FirstName;
                        dto.LastName = student.LastName;
                        dto.EnrollmentDate = student.EnrollmentDate;
    
                        var enrolllments =  new DAOEnrollment().GetEntrollmentsByStudentId(id).ToList();
                        var courses = new DAOCourse().GetCoursesByStudentCourseId(student.StudentID).ToList();
    
                        dto.EnrollsandCourses = (from a in enrolllments
                                      join b in courses on a.CourseID equals b.CourseID
                        select new  DTOEnrollandCourse()
                         { Title = b.Title, Credits = b.Credits, Grade = a.Grade }).ToList();
                    }
                }
    
                return dto;
            }
            public void CreateStudent(DTOStudent dto)
            {
                using (var context = new CUDataEntities())
                {
                    var student = new Student
                    {
                        FirstName = dto.FirstName,
                        LastName = dto.LastName,
                        EnrollmentDate = dto.EnrollmentDate
                    };
    
                    context.Students.Add(student);
                    context.SaveChanges();
                }
            }
    
            public void DeleteStudent(int id)
            {
                Student student;
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == id)).SingleOrDefault();
                }
    
                using (var newContext = new CUDataEntities())
                {
                    newContext.Entry(student).State = System.Data.Entity.EntityState.Deleted;
                    newContext.SaveChanges();
                }
            }
    
            public List<DTOStudent> GetStudents()
            {
               
                var dtos = new List<DTOStudent>();
    
                using (var context = new CUDataEntities())
                {
                    //var adapter = (IObjectContextAdapter)context;
                    //var objectContext = adapter.ObjectContext;
                    
                    //var entityConn = objectContext.Connection as EntityConnection;
                    //var dbConn = entityConn.StoreConnection as SqlConnection;
    
                    //dbConn.Open();
    
                    var students = context.Students.ToList();
    
                    foreach(var stud in students)
                    {
                        var dto = new DTOStudent
                        {
                            StudentID = stud.StudentID,
                            FirstName = stud.FirstName,
                            LastName = stud.LastName,
                            EnrollmentDate = stud.EnrollmentDate
                        };
    
                        dtos.Add(dto);
                    }
                }
    
                return dtos;
            }
    
            public void UpdateStudent(DTOStudent dto)
            {
                var student = new Student();
    
                using (var context = new CUDataEntities())
                {
                    student = (context.Students.Where(a => a.StudentID == dto.StudentID)).SingleOrDefault();
                }
    
                if (student != null)
                {
                    student.FirstName = dto.FirstName;
                    student.LastName = dto.LastName;
                    student.EnrollmentDate = dto.EnrollmentDate;
                } 
    
                using (var dbcontext = new CUDataEntities())
                {
                    if (student != null)
                    {
                        dbcontext.Entry(student).State = EntityState.Modified;
                        dbcontext.SaveChanges();
                    }
                }
            }
        }
    }
    sing System;
    using System.Collections.Generic;
    
    namespace Entities
    {
        public class DTOStudent
        {
           public Int32 StudentID { get; set; }
           public string LastName { get; set; }
           public string FirstName { get; set; }
           public DateTime? EnrollmentDate { get; set; }
    
            public virtual ICollection<DTOEnrollandCourse> EnrollsandCourses { get; set; }
    
        }
    }



    • Edited by DA924x Saturday, February 9, 2019 12:14 PM
    Saturday, February 9, 2019 12:11 PM
  • Hi engahmedbarbary,

    Thank you for posting here.

    Based on your description, you want to  assign any number of parameter keys on function Getbyid Repository pattern.

    You could try the following code.

    public T GetById(params object[] test)
            {
                for (int i = 0; i < test.Length; i++)
                {
                    return dbSet.Find(test[i]);
                }
               
            }
    

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, February 13, 2019 3:16 AM
    Moderator
  • Hi engahmedbarbary,

    Thank you for posting here.

    Based on your description, you want to  assign any number of parameter keys on function Getbyid Repository pattern.

    You could try the following code.

    public T GetById(params object[] test)
            {
                for (int i = 0; i < test.Length; i++)
                {
                    return dbSet.Find(test[i]);
                }
               
            }

    Hope my solution could be helpful.

    Best regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.c Dynamic om.

    Well your solution was explained to the OP in the ASP.NET forums. The OP wants to use Dynamic as he or she explained it in the ASP.NET forums. Dynamic doesn't work with a generic repository. However, Dynamic should work with a non generic Repository pattern or the DAO pattern.


    • Edited by DA924x Wednesday, February 13, 2019 4:43 AM
    Wednesday, February 13, 2019 4:41 AM
  • Problem of generic repository (or generic DAO) as you want to use is that you expose DB entity to another layer. Repository pattern should be use for business models which are modeled by anemic or rich domain model. There are powerful as full support business layer. It makes sense because you want to create/update some business entity in business layer and you don't want to save them immediately into database. So you save them into repository and when business flow ends you save all repositories. 

    There is next pattern which encapsulate all used repositories in business flow. It is called unit of work. Princip of unit of work pattern is that repository doesn't have Save method and it is used as repository in right way. It means repository class doesn't know how to save data. It has reference to DbContext injected by unit of work class. Unit of work class has method CommitChanges which saves all entities into database. 

    When you create generic repository then all repositories will have GetById method. It could be nice but there can be case when you don't want to have GetById method. As I mentioned repository pattern is for support business layer to work with business entity.

    Wednesday, February 13, 2019 6:12 AM
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by engahmedbarbary Wednesday, February 20, 2019 12:55 PM
    Tuesday, February 19, 2019 8:10 AM
    Moderator
  • yes my problem solved
    Wednesday, February 20, 2019 12:56 PM
  • yes my problem solved

    You marked the wrong post as 'answered", becuase the post you marked as 'answered' was an inquiry about was there any reply post to the post you made in this forum 'answered' your question.  
    Wednesday, February 20, 2019 3:10 PM