Wednesday, October 14, 2009

Opinia Cb Radia President Lincoln

New Project: zInject, or "How to Code Injection anybody else"

have long had the idea of \u200b\u200ba program to inject code and compiled assemblies on hanging around the head. Finally, today was the day to create version 0.1 I present

zInject !

What is the idea behind it? Simple ...
Suppose I have a full-fledged project , with a class whose methods are M1, M2 and M3.

 

{public class M
public void M1 () {

...


} public void M2 () {

...


} public void M3 () {

...
}}


One day the boss comes and says: "From now on, we need to verify how much delay the implementation of the following methods: M1, M2 and M3.
Then you think:" Well would have to make new methods that run before and after M1, M2 and M3 and that they figure out how long each implementation.

 
public class
M
{public void M1 () {
Started
("M1");
...
Term ("M1");


} public void M2 () {
Started
("M2");
...
Term ("M2");

} public void M3 () {
Started
("M3");
...
Term ("M3");
}}

This brings a number of problems. Add this dirty code M1, M2 and M3 for the calls to new methods at the beginning and final. Worse, if I have to add new "behavior" at the beginning or end of the method, the code ends up being incomprehensible! Not to mention if each behavior (eg, security or audit) involves adding more than one line of code ... To fix comes

zInject .
To use, define an attribute that inherits from InjectionBaseAttribute


 
TiempoAttribute public class: public
InjectionBaseAttribute TiempoAttribute
{()}


{public override void
 BeforeExecute (string method, object target) {

Started (method);

} public override void AfterExecute (string method, object target) {

Term (method);
}}


Then simply add the attribute to the methods you want: public class

 

M
{[Time]
public void M1 () {

...


} [Time]
public void M2 () {

...


} [Time]
public void M3 ()
{
...
}}


now say ... All very nice, but ... How do I run the code attribute?

To do this you must run the injector itself. This is done either by running from the command line when compiling the assembly, such as using a Post Build Action in Visual Studio. This utility will add in the IL of the assembly the necessary calls to methods and ExecuteAfter ExecuteBefore

To Do:

  • avoid completely the use of Reflection. Currently, the injected code uses reflection to implement the actions of the attributes, just for simplicity of development. In the future, will be injected directly llamdo the particular attribute. Power
  • customize which attributes to apply and which not
  • UI for Windows / OS X / Linux
  • Remove from external libraries dependencies

I leave the management of the project on CodePlex to download the compiled binaries and full source download their tests to download and see how it is used.

http://zInject.codeplex.com

Any suggestion is welcome!

Greetings! Zaiden


0 comments:

Post a Comment