質問

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.

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top