Thursday, March 28, 2019

Inspecting your .NET classes with .NET Object Model Diagram Generator

OmgGenerator


While at the MVP summit in March 2019, I attended a short presentation by Morten Nielson, a fellow MVP. He talked about a utility he created that facilitates inspection & documentation of .NET classes. You can visit his GitHub project at https://github.com/dotMorten/DotNetOMDGenerator/blob/master/README.md.

There is much more detail about his utility at Morton's GitHub site. I will, however, give you a very stripped down version of what this utility is capable of doing in this article. To start with, download and globally install the OmgGenerator with:


dotnet tool install --global dotMorten.OmdGenerator


Once you have installed the OmgGenerator utility, you can inspect .NET classes in your application by running the following command in the root of your .NET project:

generateomd /source=. /output=diagram

I run the above commamd in one of my simple projects and this is what was created in a file named diagram.html:

 
Thev default output format is HTML. You can generate the output in markdown format with the /fotmat switch as shown below:

generateomd /source=. /output=diagram /format=md

The above would produce a file named diagram.md with the same diagram as before.
You can get the utility to exclude certain directories with the /exclude switch. The following would exclude folders bin, tests, Views and Areas:

generateomd /source=. /output=diagram /exclude="*/bin/*;*/tests/*;*/Views/*;*/Areas/*"

Another very powerful feature is the tool’s ability to identify the class differences between two branches in GitHub. In the following example I am comparing the master branch and the ver2 branch in a GitHub repo:

generateomd /output=difs /source=https://github.com/medhatelmasry/HealthAPI/archive/ver2.zip /compareSource=https://github.com/medhatelmasry/HealthAPI/archive/master.zip /exclude="*/ref/*;*/tests/*;*/perftests/*"

The diff.html file produced looks like this:

The above tells me the following:
  • the Symptoms property was added to the Ailment class
  • a new Hospital class was added
  • the Supplier property was added to the Medication class

Conclusion

I am impressed with Morten's tool and would recommend it to any .NET programmer out there.