CInject - Code Injection with Runtime intelligence

2 minute read

CInject adds more value to your existing applications by injecting runtime intelligence.  You can use injectors provided by with CInject, or define your own injector.

This article will highlight some cases where you can directly use CInject

Existing application has no or very little logging

Your application is a 3-tier application and is designed using Dependency Injection and Inversion of Control patterns.  Application switches the flow control too frequently and has no logging.

Use LogInjector to inject logs in your application.  When you execute the application, LogInjector will create application logs of when a method was invoked and when it was completed.  You can also customize or build your own LogInjector by implementing ICInject interface.

OnInvoke method provides you a lot of information such as,

  • Executing method and its properties and signature - Complete reflection object is provided to OnInvoke method
  • Arguments passed to the injected method - You can use this to debug what was passed to the method.  If the argument is an object (not native type), you can use ObjectSearch to find out any value of any property
  • Executing assembly - Useful if you want to load assembly later or want to perform some additional tasks on the assembly

Application is performing slower than I expected

Your application is performing slower than you expected and would want to find out which method is taking the most time.  However, all third-party tools are paid and expensive.

You can use the PerformanceInjector provided with CInject.  Injecting PerformanceInjector provides you method execution time in milli-seconds.  What more, you can write you own PerformanceInjector or extend the current one to log the results in the database and build a beautiful ASP.NET or Silverlight application to view it.
A sample performance log file is:
[1] INFO  	 Sleep executed in 1201.8618 mSec
[1] INFO  	 Add executed in 1438.8754 mSec
[1] INFO  	 Subtract executed in 0 mSec
[1] INFO  	 GetName executed in 0 mSec
[1] INFO  	 GetTypeName executed in 0 mSec
[1] INFO  	 GetTypeName executed in 0 mSec
[1] INFO  	 Sleep executed in 1200.0686 mSec
[1] INFO  	 GetRefValue executed in 1200.0686 mSec
[1] INFO  	 GetOutValue executed in 0 mSec
[1] INFO  	 Create executed in 0 mSec
[1] INFO  	 GetArrayCount executed in 0 mSec
[1] INFO  	 AddOptional executed in 0 mSec
[1] INFO  	 AddOptional executed in 0 mSec
[1] INFO  	 DelegateCalled executed in 0 mSec
[1] INFO  	 CallDelegate executed in 1.0001 mSec
So yes you can extend existing injectors, or create new ones as per your need.

Don't know what gets passed as arguments to few methods

You have a Windows based Application (Winforms, WPF, etc.) and would want to know how your inputs are getting translated/transformed and passed as arguments to some of your methods.  For example, a method takes TextBox as an argument but you would want to know value of Name and Text property of the argument.
You can use ObjectValueInjector with ObjectSearch to get specific properties of arguments passed to a method. The output of ObjectValueInjector would appear like
Method btnChangeValue_Click Argument #0 :Text= Print the Value in label
Method btnChangeValue_Click Argument #0 :Name= btnChangeValue
Method ChangeValue Argument #0 :Text= a
Method ChangeValue Argument #0 :Name= txtInputValue