locked
inheritance and repository RRS feed

  • Question

  • User-309523270 posted

    Hi,

    I have the following scenario:

    Model
    =======
    public class PersonEntry
    {
     public string LastName { get; set; }
     public string FirstName { get; set; }
     public string MiddleName { get; set; }
    }


    public class FacultyEntry : PersonEntry
    {

    }

    public class StudentEntry : PersonEntry
    {
     public string Major { get; set; }
     public string GPA { get; set; }
    }

    =====================================================================
        public class PersonRepository 
        {
            private PersonAPIContext _personAPIContext;
            private readonly IMapper _mapper;

            public PersonRepository(PersonAPIContext context, IMapper mapper) 
            {
                this._PersonAPIContext = context;
                this._mapper = mapper;
            }

     
            public async Task<PersonDTO> Create(PersonDTO oPerson)
            {
                 this._PersonAPIContext.Persons.Add(oPerson);
            }
        }


    =====================================================================
        public class TeacherRepository : PersonRepository
        {
            private PersonAPIContext _PersonAPIContext;
            private readonly IMapper _mapper;

            public TeacherRepository(PersonAPIContext context, IMapper mapper) : base(context, mapper)
            {
                this._PersonAPIContext = context;
                this._mapper = mapper;
            }

            public async Task<TeacherEntry> Create(TeacherEntry p)
            {
                Task<PersonDTO> Person = base.Create("Teacher");

                if (Person != null)
                {
                        //create teacher code; need PersonId to insert into Teacher Table
                }


                //await _PersonAPIContext.SaveChangesAsync();

                return p;
            }

        }


    ========================================================================

        public class StudentRepository : PersonRepository
        {
            private PersonAPIContext _PersonAPIContext;
            private readonly IMapper _mapper;

            public StudentRepository(PersonAPIContext context, IMapper mapper)
            {
                this._PersonAPIContext = context;
                this._mapper = mapper;
            }

            public async Task<StudentEntry> Create(StudentEntry p)
            {
                Task<PersonDTO> Person = base.Create("Student");

                if (Person != null)
                {
                     //create student code; need PersonId to insert into Student Table
                }

                return p;
            }

        }

    ===========================================================================

     public UnitOfWork
     {
             private PartyAPIContext _partyAPIContext;
             private PersonRepository personRepository;  // planning for this to be an instance of TeacherRepository or studentRepository

             public UWPerson(PartyAPIContext partyAPIContext, PersonType c)
             {
                 _partyAPIContext = partyAPIContext;

                  if(c == "Student")
                 {
                       personRepository = new StudentRepository(_partyAPIContext);
                  }
                  if(c == "Teacher")
                 {
                       personRepository = new FacultyRepository(_partyAPIContext);
                  }
             }

             public PartyEntry Create(PartyEntry p)
             {
                   _partyAPIContext = partyAPIContext;

                   personRepository.Create(p);  /* I want this to call StudentRepository.Create or FacultyRepository.Create but it is calling PersonRepository.Create.  How can I make it so? */
                   _partyAPIContext.SaveChanges()
             }


     }

    I want this to call StudentRepository.Create() or FacultyRepository.Create() but it is calling PersonRepository.Create().  How can I make it so?

    Thanks,

    tinac99

    Monday, July 16, 2018 6:00 PM

Answers

  • User475983607 posted

    tinac99

    How do I do the transactions then?  in the APIController?

    Transactions are covered in the Entity Framework support documentation.

    https://msdn.microsoft.com/en-us/library/dn456843(v=vs.113).aspx

    The design is up to you.  I generally build services as a HTTP interface into business logic.  I only have client interaction logic in endpoints like returning bad request.  Transactions are handled in business logic specific to the process at hand. 

    tinac99

    I have several tables I need to add into so I'll just add a _personContext.SaveChanges() on the main one?

    Yes, EF is already a unit of work.  Repos are fine but in my experience repos are only a small faction of the logic required to build an application.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 11:44 AM

All replies