2011-02-25 23:36:21 +00:00
|
|
|
using System.Reflection;
|
|
|
|
using Microsoft.Practices.Unity;
|
|
|
|
using Microsoft.Practices.Unity.InterceptionExtension;
|
|
|
|
using Portoa.Persistence;
|
2011-02-26 01:08:38 +00:00
|
|
|
using Portoa.Search;
|
2011-02-25 23:36:21 +00:00
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Configuration {
|
|
|
|
|
|
|
|
public class UpdateSearchIndex : UnityContainerExtension {
|
|
|
|
protected override void Initialize() {
|
|
|
|
Container
|
|
|
|
.Configure<Interception>()
|
|
|
|
.AddPolicy("UpdateSearchIndexPolicy")
|
|
|
|
.AddCallHandler<UpdateSearchIndexCallHandler>()
|
|
|
|
.AddMatchingRule<QuoteUpdatedMatchingRule>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class QuoteUpdatedMatchingRule : IMatchingRule {
|
|
|
|
private static readonly MethodBase saveMethod = typeof(IRepository<Quote, int>).GetMethod("Save", new[] { typeof(Quote) });
|
|
|
|
|
|
|
|
public bool Matches(MethodBase member) {
|
|
|
|
return member == saveMethod;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class UpdateSearchIndexCallHandler : ICallHandler {
|
|
|
|
private readonly IUnityContainer container;
|
|
|
|
|
|
|
|
/// <remarks>
|
|
|
|
/// Can't inject ISearchIndexBuilder because it causes an infinite loop
|
|
|
|
/// while trying to instantiate the call handler. So we do a later resolve
|
|
|
|
/// on the index builder so that this shit fucking works.
|
|
|
|
/// </remarks>
|
|
|
|
public UpdateSearchIndexCallHandler(IUnityContainer container) {
|
|
|
|
this.container = container;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) {
|
|
|
|
var returnValue = getNext()(input, getNext);
|
|
|
|
if (returnValue.Exception != null) {
|
|
|
|
//if the update failed then just return
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var quote = returnValue.ReturnValue as Quote;
|
2011-02-26 01:22:07 +00:00
|
|
|
if (quote == null || !quote.ShouldIndex) {
|
2011-02-25 23:36:21 +00:00
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
container.Resolve<ISearchIndexBuilder<Quote>>().UpdateIndex(quote);
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Order { get; set; }
|
|
|
|
}
|
|
|
|
}
|