mercredi 29 avril 2020

Pass data objects with constructor service objects in dependency injection

I'm a bit familiar with dependency injection. Today a case came. I need to make an object from a data.So I completed code in the structure

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

Like this I have different sources like Excel etc...

And say I have teacher's alerts also have the different combinations

Now a new requirement comes. I have already IDateTransformationService in my system which runs on the customer customization. Its asked to do this transformation also with this. Means the above example should change to DateTime.Now() to transformationService.Transform(DateTime.Now());

There is lot of places create the object already in my project. I'm using unitycontainer now. Is there a way to auto resolve IDateTransformationService in place so that I needn't to each place and change creation of object after manual resolve? Eg: some code like below. But the object initialization shouldn't expect the first paramter and it should autoresolve

public StudentGeneratorTypeFromXML(IDateTransformationService transformationService,Student student)
{
    this.transformationService=transformationService
    this.student= student;
}

Aucun commentaire:

Enregistrer un commentaire