WMI Implementation With ORMi

I have written a new .NET library named as ORMi to communicate to WMI (Windows Management Instrumentation). In this article, let's get an overview of ORMi.

ORMi is a new .NET library I wrote to communicate to WMI (Windows Management Instrumentation). I´ve made several dozens of WMI compatible apps over these years and I always came to the conclusion that .NET standard libraries are very poor and make developers write too much code to get simple values. That´s where ORMi stands - by making every WMI work much more easily and letting developers write more readable, easy to understand, and maintainable code.

I've added several improvements since the first versions of ORMi, such as - async methods, WMI watcher, and WMI method support.

Anyone who has worked with WMI and .NET can tell you that WMI work can be a headache. WMI class names are not the simpler ones and definitely not more intuitive. They have tricky and non conventional names. I bet you would never want to give that name to a class on your project. That's one thing that makes ORMi simpler and more intuitive.

Let me show you how it works.

To get the full experience with ORMi, it is important to have our model defined and mapped to a WMI Class. Let's suppose that we want to get a list of devices connected to the PC. That is quite simple.

  1. [WMIClass("Win32_PnPEntity")]    
  2. public class Device    
  3. {    
  4.     public string Name { getset; }    
  5.   
  6.     public string Description { getset; }    
  7.   
  8.     [WMIProperty("Status")]    
  9.     public string StatusName { getset; }    
  10. }  

The above class is enhanced with the use of some attributes. WMIClass tells ORMi the name of the WMIClass that Device represents. WMIProperty attribute tells which is the WMI property that will be mapped to that property. ORMi works like this: If there is no attribute, then the member name is used to do the mapping. If an attribute has been set, then the attribute name is used.

Then, with our model properly set, we can query WMI to get the instances we want.

  1. static void Main(string[] args)    
  2. {    
  3.     WMIHelper helper = new WMIHelper("root\\CimV2");  
  4.     List<Device> devices = helper.Query<Device>().ToList();    
  5. }  

This way, for example, if you only want to search for a mouse device, you can query that only.

  1. List<Device> devices = helper.Query<Device>().Where(p => p.Description.Contains("Mouse")).ToList();  

ORMi also supports all the CRUD operations. You can add, modify, or delete the instances in a quite simple way.

  1. Person person = new Person  
  2.     {  
  3.         FirstName = "John",  
  4.         Lastname = "Doe",  
  5.         DocumentNumber = "9995",  
  6.     };  
  7. helper.AddInstance(person);  

The class defined in this example is defined this way.

  1. [WMIClass("Lnl_CardHolder")]  
  2. public class Person  
  3. {  
  4.     public string Lastname { getset; }  
  5.     public string FirstName { getset; }  
  6.   
  7.     [WMIProperty( Name = "SSNO", SearchKey = true)]  
  8.     public string DocumentNumber { getset; }  
  9.   
  10.     [WMIIgnore]  
  11.     public int Age { getset; }  
  12. }  

All these operations are also supported in an async fashion. You are invited to suggest improvements, fixes, and contribute to the project!.

Project Repository and docs can be found at https://github.com/nicoriff/ORMi

You can download it via NuGet too from here: https://www.nuget.org/packages/ORMi/