- The project I've been working on used to have SQL as its primary datastore. We're implementing another data store using MongoDB as it will replace the SQL one soon. During the migration process, we have 2 versions of the website running parallel. The production version is using SQL which is viewed by customers and the other one using MongoDB which is being tested by our staff. So once the Mongo version development is finished, it will be kept running in parallel with the SQL version until we're completely confident that all the features are working fine. During that time, any changes made by users to the SQL db have to be applied to MongoDB.
- There're quite a few solutions in my mind. However, we've had some sort of DataAccess dlls to access Sql database before and another implementation of same interfaces which talk to MongoDB have been implemented. So the options is narrowed down to something like an adapter pattern which takes 2 instances of the same interface, invoke method on the first one and then invoke the same method on the second instance. Being a good fan of Composition over Inheritance, I was thinking of something like:
public class DualService<T> : T { private readonly T _primary; private readonly T _secondary; public DualService(T primary, T secondary) { _primary = primary; _secondary = secondary; } public void MethodA() { _primary.MethodA(); _secondary.MethodA(); } // So on }
- Well, this approach is good only if the generic T interface has just a few methods. Ours has over 30 methods and it keeps counting ;), it's implemented by the "Outsource team" So I dropped this option, and thought about the Autofac Interceptor. The intercepter I'm gonna implement will take the secondary implementation and invoke the same method as the invocation if:
- The method is not decorated by IgnoreInterceptAttribute
- The method has a result type void. (Well, a command should not have return type here if you're curios about the reason)
- The method is decorated by DualInvocationAttribute
- Here is the implementation. This approach requires Castle.Core and AutofacContrib.DynamicProxy2. The 2 attributes are just simple attributes I created to enhance the filter:
[AttributeUsage(AttributeTargets.Method)] public class IgnoreInterceptAttribute : Attribute { } [AttributeUsage(AttributeTargets.Method)] public class DualInvocationAttribute : Attribute { } public class DualInvocation<T> : IInterceptor where T: class { private readonly T _secondaryImplementation; private readonly Action<Exception> _onSecondaryImplementationInvokeError; public DualInvocation(T secondaryImplementation, Action<Exception> onSecondaryImplementationInvokeError = null) { _secondaryImplementation = secondaryImplementation; _onSecondaryImplementationInvokeError = onSecondaryImplementationInvokeError ?? (ex => Trace.WriteLine(ex)); } public void Intercept(IInvocation invocation) { invocation.Proceed(); var returnType = invocation.Method.ReturnType; var att = invocation.Method.GetCustomAttributes(typeof(IgnoreInterceptAttribute), true); if (att.Any()) { return; } if (returnType != typeof(void)) { att = invocation.Method.GetCustomAttributes(typeof(DualInvocationAttribute), true); if (!att.Any()) { return; } } var methodInfo = invocation.Method; try { methodInfo.Invoke(_secondaryImplementation, invocation.Arguments); } catch (Exception ex) { _onSecondaryImplementationInvokeError(ex); } } }
- You might wonder about how to use this class, take a look at the unit tests. You should probably do something either in code or autofac xml config, similar to what I did in the SetUp:
- Here is a dummy interface to demonstrate the Autofac registrations
public interface ISomeService { void Update(); void Update(int userId); [IgnoreIntercept] void Update(int userId, bool multi); int GetUser(int userId); [DualInvocation] bool DeleteUser(int userId); }
- And here is the tests, the tests are using NSubstitute to mock the interface. (Pretty similar to Moq, etc)
[TestFixture] public class MethodIntercept { private ISomeService primary, secondary; private IContainer container; private Action<Exception> exceptionHandler; [SetUp] public void RegisterAutofacComponents() { primary = Substitute.For<ISomeService>(); secondary = Substitute.For<ISomeService>(); var builder = new ContainerBuilder(); builder.RegisterInstance(secondary).Named<ISomeService>("secondary").SingleInstance(); builder.RegisterInstance(primary) .Named<ISomeService>("primary").SingleInstance() .EnableInterfaceInterceptors() .InterceptedBy(typeof (DualInvocation<ISomeService>)); builder.RegisterType<DualInvocation<ISomeService>>() .WithParameters(new Parameter[] { new ResolvedParameter((p, c) => p.Name == "secondaryImplementation", (p, c) => c.ResolveNamed("secondary", typeof (ISomeService))), new NamedParameter("exceptionHandler", exceptionHandler) }) .AsSelf() .InstancePerLifetimeScope(); container = builder.Build(); } [Test] public void Should_invoke_secondary_method_if_it_is_not_ignored() { // Arrange var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.Update(); // Assert secondaryService.Received(1).Update(); } [Test] public void Should_invoke_secondary_overload_method_if_it_is_not_ignored() { // Arrange var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.Update(1); // Assert secondaryService.Received(1).Update(1); } [Test] public void Should_invoke_secondary_overload_method_if_it_is_decorated_by_DualInvocationAttribute() { // Arrange var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.DeleteUser(1); // Assert secondaryService.Received(1).DeleteUser(1); } [Test] public void Should_not_invoke_secondary_method_if_it_is_not_void() { // Arrange var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.GetUser(1); // Assert secondaryService.DidNotReceive().GetUser(Arg.Any<int>()); } [Test] public void Should_not_invoke_secondary_method_if_it_is_ignored() { // Arrange var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.Update(1, true); // Assert secondaryService.DidNotReceive().Update(Arg.Any<int>(), Arg.Any<bool>()); } [Test] public void Should_not_throw_exception_if_cannot_invoke_secondary_method() { // Arrange exceptionHandler = ex => Trace.WriteLine(ex); secondary.When(x => x.Update()).Do(callInfo => { throw new Exception(); }); var primaryService = container.ResolveNamed<ISomeService>("primary"); var secondaryService = container.ResolveNamed<ISomeService>("secondary"); // Action primaryService.Update(); // Assert secondaryService.Received(1).Update(); } }
- I reckon this approach is clean, simple and It works on my machine . Cheers.
1 comments:
If the only thing against "DualService : T" is the number of methods in T, then the adapter could easily be "implemented" with T4 autogenerating all of it's members.
However, your solution also looks nice :)
Post a Comment