Skip to main content
added 8 characters in body
Source Link
Fabio
  • 3.2k
  • 1
  • 20
  • 26

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

YouIn constructor you can always inject default strategy conditions which remain unchanged during transformation lifetime

public class Transformation
{
    private readonly IConditionStrategy _defaultCondition;

    public Transformation(IConditionStrategy defaultCondition) 
    { 
        _defaultCondition = defaultCondition;
    }

    public ISource Transform(ISource input)
    {
        return Transform(_defaultCondition, input);
    }
   
    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

You can always inject default strategy conditions which remain unchanged during transformation lifetime

public class Transformation
{
    private readonly IConditionStrategy _defaultCondition;

    public Transformation(IConditionStrategy defaultCondition) 
    { 
        _defaultCondition = defaultCondition;
    }

    public ISource Transform(ISource input)
    {
        return Transform(_defaultCondition, input);
    }
   
    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

In constructor you can inject default strategy conditions which remain unchanged during transformation lifetime

public class Transformation
{
    private readonly IConditionStrategy _defaultCondition;

    public Transformation(IConditionStrategy defaultCondition) 
    { 
        _defaultCondition = defaultCondition;
    }

    public ISource Transform(ISource input)
    {
        return Transform(_defaultCondition, input);
    }
   
    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}
added 863 characters in body
Source Link
Fabio
  • 3.2k
  • 1
  • 20
  • 26

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

You can always inject default strategy conditions which remain unchanged during transformation lifetime

public class Transformation
{
    private readonly IConditionStrategy _defaultCondition;

    public Transformation(IConditionStrategy defaultCondition) 
    { 
        _defaultCondition = defaultCondition;
    }

    public ISource Transform(ISource input)
    {
        return Transform(_defaultCondition, input);
    }
   
    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input){
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}

You can always inject default strategy conditions which remain unchanged during transformation lifetime

public class Transformation
{
    private readonly IConditionStrategy _defaultCondition;

    public Transformation(IConditionStrategy defaultCondition) 
    { 
        _defaultCondition = defaultCondition;
    }

    public ISource Transform(ISource input)
    {
        return Transform(_defaultCondition, input);
    }
   
    public ISource Transform(IConditionStrategy condition, ISource input)
    {
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}
Source Link
Fabio
  • 3.2k
  • 1
  • 20
  • 26

As @Mark noticed there no such rule that all arguments should be instantiated in the constructor.

Given various applications, my estimate is that it has to be way lower than 80%

I think it is not right to make design decision based on the percentage values. You can use the way to inject dependencies which fits requirements of current context.

In your particular example IConditionStrategy is dependency which could change during runtime - so why you not introduce it as "external" dependency and pass it to the method as argument?

public class Transformation
{
    public Transformation() { }

    public ISource Transform(IConditionStrategy condition, ISource input){
        ISource source = new Source();

        foreach(var i in input){
             if (condition.Check(i))
             {
                 source.Add(i);
             }
        }

        return source;
    }
}