mercredi 29 avril 2020

How can I mandate one class will hold One type of inner object

I have a requirement like from different type of objects I need to create AlertModelWrapper object. For this I can pass student only to StudentAlertGenerator, and teacher only to TeacherAlertGenerator. What should be the interface method here? If i take object i can't mandate student can have only Student type. Where my design lags?

public class StudentAlertGenerator : IAlertGeneratorType
{
  private readonly IDateTransformationService transformationService
  public StudentAlertGenerator(IDateTransformationService transformationService)
  {
     this.trnasformationservice =transformationservice
  }
  public AlertModelWrapper CreateAlertTypeModel(Student student)
  {
    return new AlertModelWrapper ()
    {
        ID= "std_"+student.Id,
        AlertModel = new AlertModel () { alertDetails=student.Name + "has an alert on " +transformationService.Transform(DateTime.Now())}
    };
  }
}

and teacher alertGeneratory as follows

public class TeacherAlertGenerator : IAlertGeneratorType
{
  private readonly IDateTransformationService transformationService
  public StudentAlertGenerator(IDateTransformationService transformationService)
  {
     this.trnasformationservice =transformationservice
  }
  public AlertModelWrapper CreateAlertTypeModel(Teacher teacher)
  {
    return new AlertModelWrapper ()
    {
        ID= "tea_"+teacher.Id,
        AlertModel = new AlertModel () { alertDetails=teacher.Name + "has an alert on " +transformationService.Transform(DateTime.Now())}
    };
  }
}

Aucun commentaire:

Enregistrer un commentaire