Skip to content

.net dynamic proxy generation library usable for custom abstraction layers.

License

Notifications You must be signed in to change notification settings

alien-mcl/RollerCaster

Repository files navigation

RollerCaster

RollerCaster is a simple yet effective tiny library that allows you to create proxies of interfaces and DTO classes.

Usage

Simple use cases for data structure is, well, simple. Just create new instance of the MulticastObject class and use an extension method of _ActLike<T>() to wrap it with any interface or DTO class:

var product = new MulticastObject().ActLike<IProduct>();
product.SomeValue = "some text";
var resource = product.ActLike<IResource>();
resource.Description = "some description";
foreach (var property in resource.Unwrap().Properties)
{
    Console.Write(property.Value);
}

Method implementation

Since version 1.3, RollerCaster allows you to inject custom method implementations enabling you to create proxies not only of pure data structure interfaces and DTO classes, but also types with some logic methods.

In order to inject those implementations, you can use a fluent-like builder exposed by the MulticastObject type named ImplementationOf<T>(), i.e.:

MulticastObject
    .ImplementationOf<ICart>()
        .ForFunction(instance => instance.Summarize()).ImplementedBy(cart => Implementations.Summarize(cart))
        .ForAction(instance => instance.Validate()).ImplementedBy(cart => Implementations.Validate(cart));

There are a few requirements of methods provided as an implementation:

  • method MUST be static (ownin class doesn't have to be static)
  • method MUST be public - this is due to possible access rights issues for other visibilities
  • method MUST accept first argument of the type being mapped - this works similar way as extension method does
  • method MUST match parameters and returned values as the method being implemented (please bare in mind previous requirement!)

In the implementation provided you can access the instance on which the method is invoked, thus you can use only it's public members.

Still, there is a possibility of using Unwrap() or TryUnwrap() method to access an underlying MulticastObject proxy and use GetProperty method in conjuction with i.e. HiddenPropertyInfo also available in this library to gain access to properties not exposed by the interfaces the proxy was casted to.

Locking instances

Since version 1.4 it is possible to lock instance from writing to properties. This might be achieved by calling i.e.:

var multicastObject = new MulticastObject();
if (!multicastObject.GetLockedState())
{
	multicastObject.LockInstance();
}
else
{
	multicastObject.UnlockInstance();
}

Locking an instance will cause property setters on proxy instances (after using ActLike) to throw InvalidOperationException. Collections still can be modified as it affects only property setters. It is still possible to modify properties without proxy through the MulticastObject API.

About

.net dynamic proxy generation library usable for custom abstraction layers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published