In C # .Net using IoC, is it okay to directly access a method without using a constructor?

I am new to IoC and I am struggling to figure out is it ok to pass a dependency directly to a method? I am using Unity IoC.

So, let's say I have a class that looks like this:

public class FtpCommon : IFtpCommon
{
    private readonly IConfigSettings _config;

    public FtpCommon(IConfigSettings config)
    {
        _config = config;
    }

    public string CombinePaths(string uri1, string uri2)
    {
        ....
    }        

    public string GetLocalFilteredFilePathsFull()
    {
        ....
    }        
    ....
}

      

Now, in one of the methods, I need to use a dependency on another class, and at the moment I do it like this:

public List<FtpListItem> GetFtpFileListRecursive(IFtpClient ftp)
{
    ...
}

      

The reason I am passing the ftp instance to the direct method is because the ftp instance with that point is configured the way I need it.

However, I read in various articles saying that all dependencies should be passed in the constructor, but I don't understand if this is the kind of dependency that should be transferred into a class variable and passed to the constructor, or are these dependencies OK? And how would I do it?

Can anyone have experienced please comment on this?

+3


source to share


1 answer


There is no rule for IoC as a generic pattern that says that injection should only be done through constructors.

Further, https://unity.codeplex.com/ ,



"The Unity Application Block (Unity) is a lightweight, expandable injection container with constructor, property, and invocation method injection support."

So go on like you ...

+2


source







All Articles