简体   繁体   中英

Changing a Parameter - who's responsible?

I've got a table which gets filled by another application. This table contains an attribute called IsMailSent .

EF builds my objects of Type Request based on the database data.

The object looks something like this:

public class Request {
    int SomeInt;
    bool IsMailSent;
    SomeObject SomeObject;
}

Now I want to create a Service which will load all the entries with IsMailSent == false and send this mail to their recipients.

My current code works as follows: A class called MailMessageService got a Start() and a Stop() method. The Start method looks like this:

public void Start(int delay) {
        tokenSource = new CancellationTokenSource();
        T = new Task(() => {
            MailService ms = new MailService(Res.ServerAddress, int.Parse(Res.ServerPort));
            while (true) {
                var messages = GetMailMessages(_context.Requests.Where(o => !o.IsMailSent));
                ms.Send(messages);
                Thread.Sleep(delay);
            }
        }, tokenSource.Token);
    }

The method GetMailMessages receives a Collection of Request and builds a Collection of MailMessages . Currently I created a class that inherits from MailMessage and contains a reference to the corresponding Request-object. The idea behind is that the MailService (which is responsible for sending the Mails) should set the IsMailSent property to true .

So the Send() Method should set IsMailSent = true

But is this the best way to do it? As I understand the SOLID Principles, the MailService should not be responsible for setting this property (as it is responsible for sending the mails) - or am I wrong?

You could add a method for setting the IsMailSent to the Request class. So the Request class would finally decide to set or not set the IsMailSent to true . This way the set code is still in the Request class and it would still be possible to influence the set.

Eg

public class Request {
    // Property
    public bool IsMailSent { get; private set; }

    public void MailSent() {
       // TODO check some conditions
       if (...) {
           ...
       }

       // If everything is correct set the property
       IsMailSent = true;
    }
}

And in the MailService.Send(...) you call MailSent method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM