What's the practical purpose of using constraint of class on the Repository Pattern

Cool Relax 21 Reputation points
2021-01-10T21:37:42.27+00:00

The primary issue I am facing with Repository Pattern as explained in the below tutorial article is the use of the constraint.
This seems to work perfect for small projects, however on larger projects this is causing a headache.

For example: Lets say I have the following three tables:

Customer, CustomerAddress and CustomerPhoneNumber, using entity Framework, I am having issues as to how I interact with these tables.
Getting data is easy, I can simply use the Include method to return data from all three of the tables based on their relationships.

However, I am not certain how I would insert a new Customer, CustomerAddress and CustomerPhoneNumber without doing these separately, meaning having separate repository classes for each.

Assuming you have a repository which derived from a generic repository of T where T has to be an entity.
How would you got about inserting records in a single transaction using a single repository, and without creating a separate repository for every related table.

Note:
I am not asking about how to do this in a single transaction, I am asking about how this would work in a single repository, even when the tables/entities are related.

implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,962 questions
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-01-11T07:01:03.653+00:00

    According to the current information, I think CustomerAddress and CustomerPhoneNumber should both have a foreign key to Customer, right?
    I defined two tables for testing: Person and Book
    55216-1.png
    In the code, their corresponding classes are like this:

        [Table("Book")]  
        public partial class Book  
        {  
            [DatabaseGenerated(DatabaseGeneratedOption.None)]  
            public int ID { get; set; }  
            [StringLength(10)]  
            public string Name { get; set; }  
            public decimal? Price { get; set; }  
            public int? OwnerID { get; set; }  
            public virtual Person Person { get; set; }  
        }  
        [Table("Person")]  
        public partial class Person  
        {  
            public Person()  
            {  
                 Books = new HashSet<Book>();  
            }  
            [DatabaseGenerated(DatabaseGeneratedOption.None)]  
            public int ID { get; set; }  
            [StringLength(10)]  
            public string Name { get; set; }  
            public virtual ICollection<Book> Books { get; set; }  
        }  
    

    When I insert data, I only need a Person repository to insert data into two tables.

                    Person person = new Person() { ID = 5, Name = "test2" };  
                    books.Add(new Book() { ID = 6, Name = "book6", OwnerID = 3,Price = 55, Person = person });  
                    person.Books = books;  
      
                    UnitOfWork unitOfWork = new UnitOfWork();  
                    unitOfWork.PersonRepository.Insert(person);  
                    unitOfWork.Save();  
    

    The UnitOfWork class and GenericRepository class come from the documentation you provided.
    The document I refer to: Relationships


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-01-12T06:33:20.03+00:00

    EF already uses the UoW and repository patterns. So repeating the pattern over EF is not a good approach.

    https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.dbcontext?view=entity-framework-6.2.0

    The repository pattern is a domain pattern. It is not a data persistence pattern.

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

    https://www.ben-morris.com/why-the-generic-repository-is-just-a-lazy-anti-pattern/

    You may want to look into using the DAO pattern in the future.

    https://javarevisited.blogspot.com/2013/01/data-access-object-dao-design-pattern-java-tutorial-example.html

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.