Does dependency injection increase my risk of doing something stupid?

I am trying to cover a widespread injection of addiction / IoC. As I read more and more about the benefits that I can certainly appreciate them, however, I am concerned that in some cases using the dependency injection pattern may lead to flexibility by limiting the risk by encapsulating controls what the system is capable of doing and what mistakes I or another project programmer can make. I suspect that I have something missing from the template that concerns my problems, and I hope someone can point it out.

Here is a simplified example of what concerns me. Suppose I have a NotifyAdmins method in the Notification class, and I use this method to distribute very sensitive information to users who have been identified as administrators in the application. Information may be distributed by fax, email, chat, etc. Based on user preferences. This method should get a list of administrators. Historically, I would encapsulate the creation of a set of administrators in a method by calling the AdminSet class or by calling the UserSet class, which requests a set of user objects that are administrators, or even by directly calling the database (s). Then I can call the Notification.NotifyAdmins method without fear of accidentally sending sensitive information to non-administrators.

I believe that dependency injection requires me to take the list of administrators as a parameter (in one form or another). This makes testing easier, however, what prevents me from making a stupid mistake when calling the code and passing to the NonAdmins set? If I don’t enter the kit, I can accidentally send the wrong people the wrong mistakes in one or two fixed places. If I do a set injection, I do not experience this error everywhere, do I call a method and enter a set of administrators? Am I doing something wrong? Are there IoC tools that allow you to specify these restrictions, but still use dependency injection?

Thank.

+3
source share
3 answers

You need to change your mindset.

/, , , , , .

, , :

AdminProvider provider = new AdminProvider();
Notification notify = new Notification(provider);
notify.Execute();

, :

String[] admins = new String[] { "joenormal@hotmail.com" };
Notification notify = new Notification(admins);
notify.Execute();

, .

Execute :

List<String> admins = _AdminProvider.GetAdmins();
...

- :

List<String> admins = _AdminProvider.GetAllUserEmails();

, .

+4

, , admin . , . , AdminSet, Notification admin. , Notification.

: ( ), injecion ( ) - (, , IOC , . , .

0

, :

( Open/Closed Principle) - . - Specification.

, AdminSpecification, .

, Notification API, :

public class Notification
{
    private readonly string message;

    public Notification(string message)
    {
        this.message = message;

        this.AdminSpecification = new AdminSpecification();
    }

    public ISpecification AdminSpecification { get; set; }

    public void SendTo(IEnumerable users)
    {
        foreach(var u in users.Where(this.AdminSpecification.IsSatisfiedBy))
        {
            this.Notify(u);
        }
    }

    // more members
}

You can still override the filtering behavior for testing purposes by assigning the differet specification, but the default value is safe, so you are not likely to make a mistake in this API.

For even better protection, you can wrap this entire implementation behind the Facade interface .

0
source

Source: https://habr.com/ru/post/1727112/


All Articles