Skip to content

Commit

Permalink
feat: allow middlewares to be registered at the beginning
Browse files Browse the repository at this point in the history
  • Loading branch information
joelfoliveira authored and filipeesch committed Oct 15, 2020
1 parent 1fff317 commit 2a2de8a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,31 @@ public interface IConsumerMiddlewareConfigurationBuilder
IConsumerMiddlewareConfigurationBuilder Add<T>(Factory<T> factory)
where T : class, IMessageMiddleware;

/// <summary>
/// Registers a middleware at the beginning of the middleware list
/// The middleware will run before other middlewares that already have been registered
/// </summary>
/// <param name="factory">A factory to create the instance</param>
/// <typeparam name="T">A class that implements the <see cref="IMessageMiddleware"/></typeparam>
/// <returns></returns>
IConsumerMiddlewareConfigurationBuilder AddAtBeginning<T>(Factory<T> factory)
where T : class, IMessageMiddleware;

/// <summary>
/// Registers a middleware
/// </summary>
/// <typeparam name="T">A class that implements the <see cref="IMessageMiddleware"/></typeparam>
/// <returns></returns>
IConsumerMiddlewareConfigurationBuilder Add<T>()
where T : class, IMessageMiddleware;

/// <summary>
/// Registers a middleware at the beginning of the middleware list
/// The middleware will run before other middlewares that already have been registered
/// </summary>
/// <typeparam name="T">A class that implements the <see cref="IMessageMiddleware"/></typeparam>
/// <returns></returns>
IConsumerMiddlewareConfigurationBuilder AddAtBeginning<T>()
where T : class, IMessageMiddleware;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,31 @@ public IConsumerMiddlewareConfigurationBuilder Add<T>(Factory<T> factory) where
return this;
}

public IConsumerMiddlewareConfigurationBuilder AddAtBeginning<T>(Factory<T> factory) where T : class, IMessageMiddleware
{
this.middlewaresFactories.Insert(0, factory);
return this;
}

public IConsumerMiddlewareConfigurationBuilder Add<T>() where T : class, IMessageMiddleware
{
this.DependencyConfigurator.AddTransient<T>();
this.RegisterType<T>();
this.middlewaresFactories.Add(resolver => resolver.Resolve<T>());
return this;
}

public IConsumerMiddlewareConfigurationBuilder AddAtBeginning<T>() where T : class, IMessageMiddleware
{
this.RegisterType<T>();
this.middlewaresFactories.Insert(0, resolver => resolver.Resolve<T>());
return this;
}

public MiddlewareConfiguration Build() => new MiddlewareConfiguration(this.middlewaresFactories);

private void RegisterType<T>() where T : class, IMessageMiddleware
{
this.DependencyConfigurator.AddTransient<T>();
}
}
}

0 comments on commit 2a2de8a

Please sign in to comment.