ProxyFoo is currently v0.x.x, which means that the API is subject to change.

Safe Proxies

Safe proxies are null-safe proxies of a subject interface. A safe proxy will pass-through the results of a valid object or safely return a default value (including another safe proxy) instead of returning null. If a wrapped object returns a null value, a safe proxy will be created in its place.

Usage

A safe proxy is created with the following static method call:

using ProxyFoo;

object obj = null;
var result = Safe.Wrap<ISample>(obj);

or when using object extensions:

using ProxyFoo.ExtensionsApi;

object obj = null;
var result = obj.Safe<ISample>();

A call to a method of result will always return a value even if obj==null. Return values will be the default value for the type (unless overridden by attribute on the subject interface).

A safe proxy will still return null for concrete class return types. The workaround for this case is to use an interface in place of the concrete class and apply a duck cast to the subject before applying a safe wrapper. (It may be possible to specify a default instance via attribute in the future.)