none
IDispoable, class and Interface RRS feed

  • General discussion

  • Hi,

    For this article: http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

    There is this code:

    namespace ContosoUniversity.DAL
    {
        public interface IStudentRepository : IDisposable
        {
            IEnumerable<Student> GetStudents();
            Student GetStudentByID(int studentId);
            void InsertStudent(Student student);
            void DeleteStudent(int studentID);
            void UpdateStudent(Student student);
            void Save();
        }
    }
    namespace ContosoUniversity.DAL
    {
        public class StudentRepository : IStudentRepository, IDisposable
        {
            private SchoolContext context;
    
            public StudentRepository(SchoolContext context)
            {
                this.context = context;
            }
    
            public IEnumerable<Student> GetStudents()
            {
                return context.Students.ToList();
            }
    
            public Student GetStudentByID(int id)
            {
                return context.Students.Find(id);
            }
    
            public void InsertStudent(Student student)
            {
                context.Students.Add(student);
            }
    
            public void DeleteStudent(int studentID)
            {
                Student student = context.Students.Find(studentID);
                context.Students.Remove(student);
            }
    
            public void UpdateStudent(Student student)
            {
                context.Entry(student).State = EntityState.Modified;
            }
    
            public void Save()
            {
                context.SaveChanges();
            }
    
            private bool disposed = false;
    
            protected virtual void Dispose(bool disposing)
            {
                if (!this.disposed)
                {
                    if (disposing)
                    {
                        context.Dispose();
                    }
                }
                this.disposed = true;
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
    }

    We can find that the IStudentRepository interface inherit IDispose interface, however StudentRepository class implement the IDispose interface! In my opinion, since IStudentRepository has already inherit IDispose interface, the StudentRepository class doesn't need to implement IDispose interface. Am I right?

    What's the reason of that?

    Thanks


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 26, 2014 2:50 AM

All replies

  • Yes you are right.
    For your example,  "StudentRepository : IStudentRepository, IDisposable " is same as  "StudentRepository : IStudentRepository".

    by the way, there is a interesting thing. If your source code is " "StudentRepository : IStudentRepository", then compile it to an exe or dll.
    Then decompile this exe (or dll) by RadGate .Net Reflection, you will find the code change to "StudentRepository : IStudentRepository, IDisposable " , and this situation only happens when using C#. (if the source code is vb.net , it will keep StudentRepository Implements IStudentRepository). I don't know the reason, but it happens.


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Wednesday, November 26, 2014 7:46 PM
  • Hi Bill,

    Thanks.

    Maybe this issue need to discuss, I will change this thread to discuss.

    Regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 27, 2014 1:29 AM
  • Hello,

    In my opinion, this should be a style for designing the code which could let the reader easily know that the StudentRepository class implements IStudentRepository interface and the IStudentRepository implements the IDisposable interface even if the reader only sees the StudentRepository class. Lots of interface in MSDN seems to use this style, for example, the List<T> type and etc.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 27, 2014 3:33 AM
    Moderator
  • IStudentRepository does not implement IDisposable when it inherits from it. There is no implemtation in interfaces, it just declares that IStudentRepository is IDisposable so who is implementing it must also implement IDisposable. So StudentRepository must implement IDisposable or one of its possible base classes must have been implemented it.

    What you don't need to do is explicitly say in class declaration that StudentRepository implements IDisposable. This is more of an style question how do you do it. For some it is helpful to see in class declaration what inferfaces it implements.

    But actually in this case you might also consider that saying that IStudentRepository must implement IDisposable is bad design, do they really need to implement it? No. In this case the creator of IStudentRepository has known that StudentRepository needs implementing IDisposable so he has put it in interface although I can think of several implementations from it that does not need IDisposable for example when testing. So the correct design in my mind would be to remove IDisposable from IStudentRepository and let it be in StudentRepository because that's the one that needs to implement it.

    Saying in some interface that it inherits from IDisposable needs good thinking are all implementations of that interface really disposables. Usually not and saying so then is wrong. So I don't prefer it. Those cases when I know that all classes like repositories that really access database are disposables, I have put it in repository base class not in repository interfaces. This makes testing easier since you don't need to mock IDisposable and also someone might in future create repository that is not IDisposable.


    • Edited by MasaSam Thursday, November 27, 2014 6:31 AM
    Thursday, November 27, 2014 6:19 AM
  • Hi Fred Bao,

    Thanks.

    Base on List<T> Class, it doesn’t inherit IDispose interface. So I don’t understand clearly about what you mean. Could you provide the simple code?

    Hi MasaSam,

    Thanks.

    >> IStudentRepository does not implement IDisposable when it inherits from it. There is no implemtation in interfaces, it just declares that IStudentRepository is IDisposable

    Yes, the interface not implement the inherited interface.

    >> So StudentRepository must implement IDisposable or one of its possible base classes must have been implemented it.

    I don’t know whether I get your point, I don’t think StudentRepository must implement IDisposable interface, because IStudentRespository interface has already inherit IDisposable interface, then the implement class (StudentRepository) should implement IDisposable interface regardless whether this class inherit IDisposable interface or not.

    For the design of IStudentRepository interface inherit IDisposable, I think it means that it forces the class that inherit this interface should implement IDisposable interface in order to let us to dispose the necessary object.

    Best Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, November 27, 2014 7:12 AM