使用构造函数获取形式参数错误

时间:2018-09-09 08:38:44

标签: c# constructor

我有1个班级,负责向给定月费的父母发送付款提醒。

下面是课程:

 public class FeesPaymentNotification
  {
        public void Notify() //send email notification to parents
        {
          var model = new FeesModel(1,100, 1000);
        }
  }

 public class FeesModel
    {
        public FeesModel() { }
        public FeesModel(int id, int courseId, int fees)
        {
            this.Id = id;
            this.CourseId = courseId;
            this.Fees = fees;
        }
        public int Id{ get; set; }
        public int CourseId { get; set; }
        public int Fees { get; set; }
        public int TeacherId { get; set; }
        public string Title { get; set; }
        public string CourseName { get; set; }
        public DateTime CreatedAt { get; set; }
        // other properties
   }

在我的情况下,我只需要设置3个属性,因此我创建了具有3个属性的构造函数。 FeesModel可在许多地方使用,我不想创建另一个模型,因为这是我们用于收费的常用模型。

在使用收费模式的其他地方,我遇到了以下错误:

  

没有给出与“ FeesModel.FeesModel(string,int,int)”的所需形式参数“ id”相对应的参数。

代码:

var model = new FeesModel // this is where and so many other places I am getting above error
            {
                TeacherId = teacherId,
                Title = title,
                CourseName =  courseName,
                CreatedAt = DateTime.UtcNow
            };

为解决上述错误我创建了用于填充FeesModel和此类中其他模型的私有方法,但这似乎违反了SRP原则,由于其他原因,我也没有其他私有方法来填充其他模型类有很多这样的私有方法。

1)这种私有方法是否确实违反了SRP

2)是否有其他方法可以不使用AutoMapper 来解决此问题?

0 个答案:

没有答案