Ralf
Ralf

Reputation: 548

Drawing a data structure

I am creating an implementation of a data structure (B-Tree) for learning purposes.

I want to put the classes required into it's own assembly and only reference and use the bare minimum of .NET-Namespaces there. Of course I also hide as much data and code through private or internal as possible so the class looks clean.

While developing, debugging and so on I do draw a graphical representation of the tree onto a bitmap and dump those onto disk, but to do that I need System.Drawing, ...2D, ...Imaging, ...Text as namespaces. Those should NOT be part of the pure data structure code. This drawing code should be "somewhere else"

I am done with the development, everything works and draws perfect. I can see my tree grow and shrink as list of pictures on my disk, but I am not happy with the solution layout. I think I am messing through the concerns of each assembly too much.

At the moment the drawing code is part of the windows forms code, and I put quite some public properties into the data structure class to allow the drawing code to get values needed when drawing.

Now how would I create a project, class and assembly that can draw the tree to a bitmap without compromising the pure data structure class?

The tree would raise events (or call delegates?) whenever it determines it changed it structure and should be re-displayed. The assembly responsible for drawing needs these calls, but also needs some access into the private details of the tree to display it.

Reflection is something I am somewhat familiar with but it sounds a bit over the top.

I could do DEBUG and RELEASE builds, and encapsulate the sections for drawing with conditional compiling (##IFDEBUG ?) but that still means I need to include a lot of code (and compile hints) into the data structure code.

Partial classes in the same assemble? Still needs the reference to System.Drawing.

Any other suggestions?

Thanks Ralf

Upvotes: 1

Views: 227

Answers (1)

usr
usr

Reputation: 171218

This is a hard problem in general but one thing you could do is expose only internal members to your drawing code so that other code does not need to know of their existence. You can use InternalsVisibleToAttribute to create a "friend" assembly that is able to access internal members of another. Then, you can put the drawing code into a second assembly and keep the primary one pristine.

Upvotes: 1

Related Questions