How do you transfer dependence to the facility, destroying the methods and properties of the facility?



  • There are the following types in the area:

    namespace Model {
       public interface IData {
          void Send(string str);
       }
       public class Test {
          public void Run() {
            // (1)
          }
       }
    }
    

    The Test.Run method should refer to the IData facility for its operation.
    There's the next IData interface and the test code.

    class Data : Model.IData {
        public void Send(string str) { }     
    }     
    

    var d = new Data();
    var t = new Test();

    // (2)

    What code should be instead (1) and (2)so that the reference to Data is in Test.Run?

    However, the definition of Class Test cannot change anything, the designer(s), methods, properties and fields cannot be added, and the Run method cannot be changed.

    You can't use Singleton.

    p.s.
    It's not for brain smashing, as the comment says. There's a lot of code. And there's just a dragon code analyser. There will be no addition/replacement in the analyzer. The Model Code cannot be changed and the Singapores are excluded, and Test.Run should add dependence. Asked, how? Answered, whatever you want to do.
    If there's any idea, write as well as put it down.



  • This is a working example with thread local storage:

    namespace Model
    {
        public interface IData
        {
            void Send(string str);
        }
        public class Test
        {
            public void Run()
            {
                LocalDataStoreSlot slot = Thread.GetNamedDataSlot("dict");
                var dict = (Dictionary<Model.Test, Model.IData>)Thread.GetData(slot);
                var d = dict[this];
            }
        }
    }
    

    namespace ThreadLocalTest
    {
    class Data : Model.IData
    {
    public void Send(string str) { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var d = new Data();
            var t = new Model.Test();
    
            LocalDataStoreSlot slot = Thread.AllocateNamedDataSlot("dict");
            var dict = new Dictionary&lt;Model.Test, Model.IData&gt;();
            Thread.SetData(slot, dict);
            dict[t] = d;
            t.Run();
            Thread.FreeNamedDataSlot("dict");
        }
    }
    

    }

    With regard to other possible options: http://blog.stephencleary.com/2013/04/implicit-async-context-asynclocal.html (this would be useful if you wanted to cross the border. await)


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2