简体   繁体   中英

.NET dll dependencies not working as expected

I was reading Uncle Bob's blog about SOLID principles , especially his book's excerpt about SRP - Single Responsability Principle, and trying to apply it using a real Console app.

在此处输入图片说明

To prove his point, Uncle Bob says : "In .NET the GUI assembly would have to be built and deployed with the computational geometry application." So we have to break Rectangle into two different entities, etc..

But just to be sure, I wanted to test that statement and put each of these objects into their respective assembly.

在此处输入图片说明

To me, Uncle Bob means that if ComputationalGeometry is a console app, it should not run unless being put in the same directory as Rectangle.dll and GUI.dll, right ?

In fact though, my Computational Geometry Application is running perfectly fine without GUI.dll in the executable's directory. It crashes without Rectangle.dll, which is obvious. So .NET is quite clever and knows that the draw() function is not called by the ComputationalGeometry application.

Uncle Bob would be wrong ? Can someone explain me the point I am missing here ?

Yes, based on draw() method signature the statement that GUI.dll is required is wrong if method actually not called.

If all assembly references are inside body of method(s) than that assembly is not required as long as methods are not called (or to be more precise JITed).

This is what happens in your case - you have draw() method which signature does not depend on GUI.DLL and method is not called. Since your code does not call the method there is no request to JIT it, and hence types used by body of the method don't need to be loaded. So GUI.DLL does not need to be present in Bin folder.

  // method signature does not refer to Gui.
  void draw()
  {
      // Method body refers to Gui.
      Gui.Canvas.Default.DrawRectangle(1,2,3,4);
  } 

In more real case draw would have argument of some sort of GUI context (like draw(Gui.Canvas canvas) and hence Rectangle type itself will depend on GUI.DLL. As result using Rectangle class requires GUI.DLL to be present.

  // method signature refers to Gui.
  void draw(Gui.Canvas canvas)
  {
      // Method body refers to Gui.
      canvas.DrawRectangle(1,2,3,4);
  } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM