O.O
O.O

Reputation: 11287

Private Static Dependency vs Private Dependency

  public interface ISomeDependency
  {
    void Calculate( Person person );
  }
  public class SomeDependency : ISomeDependency
  {
    void ISomeDependency.Calculate( Person person )
    {          
      person.Age = 30;
    }
  }

  public class Person
  {
    private static ISomeDependency _someDependency;
    public DateTime BirthDate { get; set; }
    public int Age { get; set; }
    public Person( ISomeDependency someDependency )
    {
      _someDependency = someDependency;
    }
    public void CalculateAge()
    {
      _someDependency.Calculate( this );
    }
  }
  public class Client
  {
    public Client()
    {
      Person p = new Person( new SomeDependency() );
      p.BirthDate = DateTime.Now.AddYears( -30 );
      p.CalculateAge();
    }
  }

Why would it matter if the dependency is static or not? In general, does it matter?

Upvotes: 3

Views: 76

Answers (2)

slash shogdhe
slash shogdhe

Reputation: 4187

if _someDependency is static ,it will get stored in high frequency heap else it will goto low frequency heap.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152491

Why would it matter if the dependency is static or not? In general, does it matter?

Yes - in your case you have an instance constructor that's resetting the static member, so the value of the static member will be the value that the last constructor was given.

Since there's no reason to make the dependency static that I can see (you have no static methods/properties that use it) it should be fine an an instance (non-static) field.

Upvotes: 1

Related Questions