3

Below Class2 has a property that needs to be set before GetSomething is called, however because I access Class2 at the top of Class1 the property is always null when it gets to Something class. I can't seem to figure out how to change my code to set the property before it's used. Anyone?

EDIT I want to pass the dependency from form1's constructor, not hardcode it further up the chain.

public partial class form1
{
    private static readonly ISomeConstructedClass someConstructedClass = Class1.SomeConstructedClass;
    public form1()
    {
        someConstructedClass.SomeDependency = new SomeDependency();
        someConstructedClass.Whatever();
    }
}
public static class Class1
{
    public static readonly ISomething something = (ISomething)Class2.GetSomething("something");
    public static ISomeConstructedClass SomeConstructedClass
    {
     get
     {
       return something.SomeConstructedClass;
     }
    }

} .... }

public class Class2
{   
    public static ISomeDependency SomeDependency
    {
       get;
       set;
    }
    public static GetSomething(string something)
    {
         switch(something)
         {
            case "something":
               return new Something( SomeDependency );
         }         
    }
}

  public class Something : ISomething
  {
      public ISomeDependency SomeDependency
      {
           get;
           set;
      }
      public Something(ISomeDependency someDependency)
      {
           SomeDependency = someDependency;
      }
  }
4

2 回答 2

2

[Re]Edit:

I was confused about what you were trying to do before, you just need to create the dependency first.

public partial class form1
{
    private static /*readonly*/ ISomeConstructedClass someConstructedClass;
    public form1()
    {
        Class2.SomeDependency = new SomeDependency();
        someConstructedClass = Class1.SomeConstructedClass;
        someConstructedClass.Whatever();
    }
}

I would also move the creation of something into the property just to make sure it is not initialized too soon (before the form1 constructor is called).

public static class Class1
{
    public static ISomething something;
    public static ISomeConstructedClass SomeConstructedClass
    {
        get
        {
            if (something == null) {
                something = (ISomething)Class2.GetSomething("something");
            }
            return something.SomeConstructedClass;
        }
    }
}

You can use a static constructor. This is called before any static (or instance for that matter) fields or methods are called/accessed.

Something like:

static Class2() {
    SomeDependency = SomeDependencyYouNeed;
}
于 2013-07-12T20:49:25.230 回答
0

Why are you using static methods? It looks like you're attempting a sort of Dependency Injection. Either create an instance of Class2 and pass the dependency in the constructor (and don't use static methods), or pass the dependency as a parameter of the GetSomething() method.

public static GetSomething(string something, ISomeDependency dependency).
于 2013-07-12T20:50:43.517 回答