-1

What is the purpose of having an interface for a model class when there is no methods for this class and its not inheriting anywhere other than Event class? Please read the question properly. It's about model class and interface not about interface inheritance for a normal class.

 public interface IEvent
 {
     int EventID{get;set;}
     string EventName{get;set;}
 }

 public class Event:IEvent
 {
     public Event(int eventID, string eventName)
     {
        this.EventID = eventID; 
        this.EventName = eventName;
     }

     public int EventID { get; set; }
     public string EventName { get; set; }
 }
Keppy
  • 471
  • 1
  • 7
  • 23
  • 12
    None, basically. It seems like a completely unnecessary abstraction without more context. Why not ask whoever added it? – Marc Gravell Jun 10 '19 at 07:00
  • @MarcGravell, I was about to ask them, but want to confirm with someone who knows better than me before asking them. – Keppy Jun 10 '19 at 07:04
  • Interface must required Implementation. So its use to fix your business logic which must require implementation in your project – Hardik Leuwa Jun 10 '19 at 07:05
  • 1
    Totally agree with @MarcGravell – Ali Bahrami Jun 10 '19 at 07:05
  • Possible duplicate of [Why would I want to use Interfaces?](https://stackoverflow.com/questions/240152/why-would-i-want-to-use-interfaces) – Liam Jun 10 '19 at 08:35
  • If you have a whole collection of model classes, using an interface and requiring all model classes to implement that interface can ensure that they all contain a certain basic, common set of properties with the identical names and datatypes – marc_s Jun 10 '19 at 08:41
  • @Liam, its not a duplicate. read the question and the specific point(Model class) – Keppy Jun 10 '19 at 09:09
  • @marc_s, Yes I know, see my question. There is no inheritance other than the specified model class. – Keppy Jun 10 '19 at 09:11
  • How is this not a *"normal class"*?! – Liam Jun 10 '19 at 09:35
  • 1
    Also: a class **implements** an interface - it doesn't "inherit" (from) it - there's **no inheritance** at all involved in this example.... – marc_s Jun 10 '19 at 09:37
  • I think what you maybe need to understand is that a MVC "model" is just a normal class. There is literally nothing special about a model. It is just a class that gets passed into the MVC framework. It obeys all the normal rules that all classes do. Including interface implementation and polymorphism – Liam Jun 10 '19 at 09:48
  • @Liam, I mean by "normal" class is like other classes which is having properties and methods, just to differentiate. Does a model class(which is having only properties) require an interface when this interface is implemented only by Event class? – Keppy Jun 10 '19 at 09:52
  • Ok, all I can do is repeat what everyone else has said, it depends. You have not given us anywhere near enough information for anyone to answer this question. I'd even go as far as to say the only person who can answer this is someone who has full access to your source code. Ask whoever wrote this why they added this interface. BTW this class has properties and can have methods... – Liam Jun 10 '19 at 09:57

2 Answers2

2

Interfaces are useful for establishing a contract - implementors MUST match the contact.

Interfaces are also useful for Mocking in unit tests, or DI/IoC.

Its possible whoever created your interface was using it for one of these two reasons...

Ian Robertson
  • 2,652
  • 3
  • 28
  • 36
  • Please read the question properly. Question is about Model class and there is no inheritance other than which I have mentioned. – Keppy Jun 10 '19 at 09:11
  • 1
    Going around telling everyone they're wrong isn't going to help. Clarify your question if this doesn't cover it @Keppy – Liam Jun 10 '19 at 09:35
0

One of the advantages is Multiple inheritances ex.

 public interface IEvent
    {
    int event Id {get;set}
    string EventName{get;set}
    } 

    public interface IEventDES
    {
    string event Description{get;set}
    }

    public class Event:IEvent, IEventDES
     {
        public Event(int eventID,string eventName, string Description)
        {
           this.EventID=eventID; 
           this.EventName=eventName;
this.Description= Description;
        }
     }
  • Please read the question properly. Question is about Model class and there is no inheritance other than which I have mentioned. – Keppy Jun 10 '19 at 09:06
  • 1
    Yes, your model is too small for now, in future if your model is not being included with other properties and model is fixed there is no point of having the Interface, in case this model is part of MVC(model binding). If the project is big and in the future there will be a necessity of adding, to avoid the error on editing the existing model. you can maintain the Interface and inherit them. if any answers other than this good to here – samba shiva Jun 11 '19 at 10:26