5

I being following the Unity Interception link, to implement Unity in my project.

By, following a link I have made a class as shown below:

[AttributeUsage(AttributeTargets.Method)]
public class MyInterceptionAttribute : Attribute
{

}

public class MyLoggingCallHandler : ICallHandler
{
    IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {
        IMethodReturn result = getNext()(input, getNext);
        return result;
    }
    int ICallHandler.Order { get; set; }
}

public class AssemblyQualifiedTypeNameConverter : ConfigurationConverterBase
{
    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value != null)
        {
            Type typeValue = value as Type;
            if (typeValue == null)
            {
                throw new ArgumentException("Cannot convert type", typeof(Type).Name);
            }
            if (typeValue != null) return (typeValue).AssemblyQualifiedName;
        }
        return null;
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        string stringValue = (string)value;
        if (!string.IsNullOrEmpty(stringValue))
        {
            Type result = Type.GetType(stringValue, false);
            if (result == null)
            {
                throw new ArgumentException("Invalid type", "value");
            }
            return result;
        }
        return null;
    }
}

Till now I have done nothing special, just followed the example as explained in the upper link. But, when I have to implement the Unity Interception class, I came up with lots of confusion.

Suppose, I have to implement on one of the methods in my class like:

[MyInterception]
public Model GetModelByID(Int32 ModelID)
{
    return _business.GetModelByID(ModelID);
}

This is the main thing where I have being stuck, I dnt know how I have to use the Intercept class over the GetModelByID() method and how to get the unity.

Please help me, and please also explain the concept of Unity Interception.

Community
  • 1
  • 1
  • What do you want to intercept or what functionality do you want to add. Basically does your `ICallHandler` implementation nothing. It calls your GetModelById method and returns the value. – Jehof Jan 27 '14 at 10:24
  • @Jehof I just want that, if my **_business** is null then no call to **GetModelById()** should be made, how can I achieve that? –  Jan 27 '14 at 10:26
  • Throw an exception in your `GetModelById` method if `_business` is null. Interception is not the right tool to support this. – Jehof Jan 27 '14 at 10:28
  • @Jehof this is what I can't do, I have been asked a task, and I have to do the task through Interception. I know `If` is pretty simple but I have to use interception. –  Jan 27 '14 at 10:31
  • and what do you want to return if `_business` is null? Your ICallHandler has to check the object that is being intercepted, if _business is set. – Jehof Jan 27 '14 at 10:33
  • I want that if `_business is null` then I wont be able to make call to `GetModelByID` function. –  Jan 27 '14 at 10:35
  • what you mean by won´t be able? when i write code `_yourObject.GetModelByID(5)` i´m calling your method – Jehof Jan 27 '14 at 10:36
  • I mean that if `_business is null` then `_business.GetModelByID` would return null in that condition, does it make sense to you know, I think I am not able to explain things to you... when you write _myobject.GetModelByID(5) then it would return null in that case.. –  Jan 27 '14 at 10:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46146/discussion-between-harsh-sharma-and-jehof) –  Jan 27 '14 at 10:40
  • @Jehof are you able to get me, or you want further clarifications from my side... –  Jan 27 '14 at 10:55

1 Answers1

0

Unity interception explained

Interception is a concept where you can isolate your "core" code from other concerns. In your method:

public Model GetModelByID(Int32 ModelID)
{
   return _business.GetModelByID(ModelID);
}

You don't want to "pollute" it with other code like logging, profiling, caching etc, that isn't part of the core concept of the method and unity interception will help you with this.

With interception you can add functionality to existing code without having to touch the actual code!

Your specific problem

if my _business is null then no call to GetModelById() should be made, how can I achieve that?

You can actually achieve what you're trying to do by using interception and reflection. I dont have access to a dev enviorment right now but something like this should work,

IMethodReturn ICallHandler.Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
{
    IMethodReturn result = input.CreateMethodReturn(null, new object[0]);

    var fieldInfos = input.Target.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
    var businessField = fieldInfos.FirstOrDefault(f => f.Name == "_business");

    if (businessField != null && businessField.GetValue(input.Target) != null)
        result = getNext()(input, getNext);

    return result;
}

You can access the target (the object) that the method (the one you're intercepting) belongs to, and then read the value of that objects private fields through reflection.

jonnep
  • 285
  • 3
  • 17
  • Basically, if i have to define `Unity Interception` how can i define, i checked on google, but didn't find any particular and confined answer can you please explain. – HarshSharma Jan 31 '14 at 08:11
  • Are you looking for a definition/summary of what unity interception really is? Not sure that i understand your question – jonnep Jan 31 '14 at 08:36
  • Man, you are doing a great job, just one more question, the method you defined `ICallHandler.Invoke`, how i have to make a call to this function, or is this function is executed automatically when my program runs. – HarshSharma Jan 31 '14 at 09:22
  • You will not be needing to call the function by your own, Unity will take care of this automatically for you. Good luck with your implementation! – jonnep Jan 31 '14 at 09:27