Component-Based Object Extender
Download: CBO Extender
What Is CBO Extender
Component-Based Object Extender (CBO Extender) is an object extensibility framework. As indicated by the name, it is component-based, which means it works with interfaces, and it extends objects directly instead of extending their types. CBO Extender can be used to extend any interface methods including interfaces from third party, from .NET Framework or interfaces defined by you. It is a general-purpose tool for adding functionality to objects by attaching behaviors to interface methods of the objects.
Extending Objects vs. Extending Types
Extending types involves design time efforts and is an intermediate step. The purpose is for objects of the extended types to have the additional functionality. What about extending objects directly without involving the intermediate step? That is the basic idea behind the CBO Extender.
Presuming it is simple enough and powerful enough (simple means it is easy to get it right, powerful means it can be used for most serious programming tasks), extending objects has following benefits:
- Components are stable;
- Extension is done on the client side of components as needed;
- leaner application;
- little maintenance.
What CBO Extender Does and Doesn’t
CBO Extender does
- work directly on objects;
- extend only interface methods and any interface methods;
- decorate existing methods;
- use methods as behaviors.
CBO Extender doesn’t
- create, change or derive from components;
- extend non-interface methods;
- inject existing methods;
- use classes as behaviors.
What CBO Extender Consists of
CBO Extender consists of a Dynamic Decorator and an AOP Container.
Dynamic Decorator evolved from the Decorator Pattern. It solves the same problem as the Decorator Pattern does: add additional functionality to an existing object dynamically. However, Dynamic Decorator takes a complete different route. Instead of creating decoration classes, it uses a customized .NET proxy technology to intercept method calls and applies additional functionality along the way, either before or after the execution of the target methods. One advantage of the Dynamic Decorator is that it doesn’t need to design/maintain decoration classes. And it is easy to use and can be applied to any object of any components. The article Dynamic Decorator Pattern explains Dynamic Decorator in details. The article Add Aspects to Objects Using Dynamic Decorator discusses the important features of the Dynamic Decorator.
AOP Container integrates the Dynamic Decorator with IoC (Inversion of Control) Containers. It enables any IoC container to have AOP capabilities by configuration or using code. The article AOP Container discusses adding AOP capabilities to IoC Containers by configuration. The article AOP Container 2 discusses adding AOP capabilities to IoC Containers using code.
Aspects in CBO Extender
In most AOP tools, aspects represent cross-cutting concerns and usually take form of some special classes. In CBO Extender, aspects are methods with specific signature. Although the methods may address cross-cutting concerns like logging, security checking, auditing, etc., they can also address concerns like sorting and grouping, which are specific to some types and are not cross-cutting. In the context of CBO Extender, aspects are methods that can implement any logic including cross-cutting concerns.
Use CBO Extender
You can use the Dynamic Decorator directly in your code by calling its
CreateProxy<T> method. There are a number of articles describing its use, for example, Dynamic Decorator, Unity and Castle DynamicProxy Comparison.
If you use some IoC Container, you may use AOP Container to integrate CBO Extender with your IoC Container. There are two ways to use AOP Container with an IoC Container: by configuation or using code. The article AOP Container discusses how to use AOP Container by configuration with MEF, Unity and Windsor. The article AOP Container 2 discusses how to use AOP Container using code with Unity and Windsor.
CBO Extender and Application Development
CBO Extender complements component-based development by making it easy to attach behaviors to objects of a component at runtime. Here is the guideline for application development using CBO Extender:
- Design components to meet business requirements in a general way
- Design cross-cutting concerns as general aspects methods in shared modules
- Design other concerns as specific aspect methods in form of local or anonymous methods
- Extend objects as needed
The following articles discuss how the above guideline are applied to various application types.
- Components, Aspects and Dynamic Decorator
- Components, Aspects and Dynamic Decorator for ASP.NET Application
- Components, Aspects and Dynamic Decorator for ASP.NET MVC Application
- Components, Aspects and Dynamic Decorator for Silverlight / WCF Service Application
- Components, Aspects and Dynamic Decorator for MVC/AJAX/REST Application
- Configurable Aspects for ASP.NET MVC/AJAX/REST Application Using AOP Container
- Configurable Aspects for WPF Application Using AOP Unity Container
CBO Extender vs. Other Proxy Technologies
CBO Extender uses a customized proxy that is based on .NET proxy technology. There are other customized proxies like Castle DynamicProxy and Unity Interceptor.
CBO Extender differentiates itself from other proxy technologies by its aspect. In CBO Extender, an aspect (behavior) is a method with a specific signature (a.k.a. aspect method). In other proxy technologies (Windsor or Unity), an aspect (behavior) is a class implementing a specific interface (a.k.a. behavior class).
Aspect method is conceptually simpler than behavior class. After all, why you need a class if a method can solve the problem? Aspect method also has capabilities to access target and call context. The article Dynamic Decorator, Unity and Castle DynamicProxy Comparison gives more details about their differences in terms of performance and features.
CBO Extender vs. PostSharp
PostSharp injects code into a method while CBO Extender decorates a method. PostSharp adds behaviors to classes at design time while CBO Extender adds behaviors to objects at runtime.
Though PostSharp can do almost anything within a class, it can hardly do things right. Most of time, it is simply too early to decide whether an aspect is needed for a class when you design the class. The article Aspects to Object vs. Aspects to Class discusses some issues related to design aspects for classes.
I did find a case that PostSharp can be used to implement
INotifyPropertyChanged interface for a WPF application (see the article
Configurable Aspects for WPF Application Using AOP Unity Container). But in general, addressing aspects at class level is not a best practice.
Why CBO Extender
- CBO Extender embraces component-based development;
- Components are separated from aspects at design time and are kept separated at runtime;
- Aspects are wired to component’s objects at runtime as needed;
- Aspects can be powerful by chaining them and by accessing target and call context;
- CBO Extender is simple and lightweight.