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.