在.NET中为“模型”类提供接口有什么好处?

时间:2019-06-10 06:58:35

标签: c# asp.net-mvc asp.net-web-api

当模型类没有方法并且除Event类之外没有继承任何其他方法时,为模型类提供接口是什么目的?请正确阅读问题。它与模型类和接口有关,而与普通类的接口继承无关。

 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; }
 }

2 个答案:

答案 0 :(得分:2)

接口对于建立合同很有用-实现者必须匹配联系人。

接口也可用于单元测试或DI / IoC中的模拟。

创建界面的人可能出于以下两个原因之一使用它...

答案 1 :(得分:0)

优点之一是多重继承。

 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;
        }
     }