Home > C# > Magiq Behaviors: Implementing plugins for magiq

Magiq Behaviors: Implementing plugins for magiq

In a previous post I talk about the API for Magiq plugins and for the next two weeks, I was trying to figure out a way to actually implement it! But a lot of headaches later, I finally get to somewhere.

If you are new here, Magiq is a framework that provides mass deletes, mass inserts, mass updates and collection queries to several Linq providers.

The main idea was to implement it as listeners/interceptos/aspects, so you can choose to decorate the calls (for example, in order to implement a logger) or to override them (for example, the actual insert for a bulkinsert). The other idea is to have all this stuff strongly typed, so no generic aspect framework was allowed.

So… Let’s rock!

First, each behavior is represented by one interface and a default class implementing it. Plugins will be classes implementing those interfaces, acting as a chain of responsability.
Second, each class that interacts with behaviors should inherit form BehaviorHandler, where TBehavior is a specific type of behavior that this class is handling. The class is able to work with other behaviors, but for API simplicity, it provides a default.

public interface IBehavior1{
     void DoSomething();
     int GetSomethingElse();
}

public interface IBehavior2{
     void DoAnotherThing();
}

public class Class1 : BehaviorHandler<IBehavor1> {
     public int Bla(){
           Call( x=>x.DoSomething() );
           Behavior<IBehavior2>().Call( x=> x.DoAnotherThing() );
           return Call( x=> x.GetSomethingElse() );
     }
}

All the “Calls” are done using this method:

public static void Execute<TBehavior>(IMagiqBehavior behavior, Action<TBehavior> action)
{
	if (behavior == null)
		return;

	if (!(behavior is TBehavior))
	{
		Execute(behavior.NextBehavior, action);
		return;
	}

	action((TBehavior)behavior);
}

So, it follow the chain only for the behaviors that implement the specific interface.

Finally, a plugin behavior will look like this:

public class Plugin : IBehavior1
{
     public IMagiqBehavior NextBehavior{ get; set; }

     public void DoSomething(){
           //do something before
           Next<IBehavior1>().Call( x => x.DoSomething() );
           //do something after
     }

     public int GetSomething(){
           //do something before
           var result = Next<IBehavior1>().Call( x => x.GetSomethingElse() );
           //do something after
           return resut;
     }
}

In the following days I will be posting the entire bulk insert implementation following this schema.

Advertisements
Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: