Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fluent Configuration is Index Strategy is specified as Event will aut…

…o-register NHibernate.Search listeners.

Also it's possible to specify to not do that.
  • Loading branch information...
commit 8693c76fca1d2ed614f6921ceab0c843164352b1 1 parent 57507af
@vcaraulean authored
View
14 src/NHibernate.Search.Fluent.Tests/FluentSearchTestBase.cs
@@ -16,7 +16,7 @@ public abstract class FluentSearchTestBase
protected IFullTextSession SearchSession;
private ISessionFactory sessionFactory;
-
+
[SetUp]
public void SetUp()
{
@@ -41,17 +41,19 @@ public void SetUp()
ConfigureSearch(configuration);
sessionFactory = configuration.BuildSessionFactory();
-
+
Session = sessionFactory.OpenSession();
SearchSession = Search.CreateFullTextSession(Session);
-
+
new SchemaExport(configuration)
.Execute(false, true, false, Session.Connection, null);
AfterSetup();
}
- protected virtual void AfterSetup() { }
+ protected virtual void AfterSetup()
+ {
+ }
protected virtual void ConfigureSearch(Configuration cfg) { }
@@ -63,6 +65,8 @@ public void TearDown()
sessionFactory.Dispose();
}
- protected virtual void Cleanup() { }
+ protected virtual void Cleanup()
+ {
+ }
}
}
View
9 src/NHibernate.Search.Fluent.Tests/Integration/Bridge/CustomFieldBridgeTests.cs
@@ -1,6 +1,4 @@
using NHibernate.Cfg;
-using NHibernate.Event;
-using NHibernate.Search.Event;
using NHibernate.Search.Fluent.Cfg;
using NUnit.Framework;
@@ -17,13 +15,6 @@ protected override void ConfigureSearch(Configuration cfg)
.IndexingStrategy().Event()
.IndexBase("LuceneIndex")
.DirectoryProvider().FSDirectory();
-
- cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
}
protected override void AfterSetup()
View
13 src/NHibernate.Search.Fluent.Tests/Integration/Inheritance/SearchInheritanceTest.cs
@@ -1,8 +1,6 @@
using Lucene.Net.Analysis.Standard;
using Lucene.Net.QueryParsers;
using NHibernate.Cfg;
-using NHibernate.Event;
-using NHibernate.Search.Event;
using NHibernate.Search.Fluent.Cfg;
using NUnit.Framework;
using Version = Lucene.Net.Util.Version;
@@ -20,13 +18,6 @@ protected override void ConfigureSearch(Configuration cfg)
.IndexingStrategy().Event()
.IndexBase("LuceneIndex")
.DirectoryProvider().FSDirectory();
-
- cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
}
protected override void AfterSetup()
@@ -38,7 +29,7 @@ protected override void AfterSetup()
References = new []
{
new Reference{Description = "code red"},
- new Reference{Description = "code blue"},
+ new Reference{Description = "code blue"}
}
});
@@ -48,7 +39,7 @@ protected override void AfterSetup()
InvoiceId = "yyy",
References = new []
{
- new Reference{Description = "code blue"},
+ new Reference{Description = "code blue"}
}
});
Session.Flush();
View
9 src/NHibernate.Search.Fluent.Tests/Integration/SearchEmbeddedCollectionsTest.cs
@@ -1,6 +1,4 @@
using NHibernate.Cfg;
-using NHibernate.Event;
-using NHibernate.Search.Event;
using NHibernate.Search.Fluent.Cfg;
using NUnit.Framework;
@@ -17,13 +15,6 @@ protected override void ConfigureSearch(Configuration cfg)
.IndexingStrategy().Event()
.IndexBase("LuceneIndex")
.DirectoryProvider().FSDirectory();
-
- cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
}
protected override void AfterSetup()
View
9 src/NHibernate.Search.Fluent.Tests/Integration/SearchEmbeddedEntitiesTest.cs
@@ -1,6 +1,4 @@
using NHibernate.Cfg;
-using NHibernate.Event;
-using NHibernate.Search.Event;
using NHibernate.Search.Fluent.Cfg;
using NUnit.Framework;
@@ -17,13 +15,6 @@ protected override void ConfigureSearch(Configuration cfg)
.IndexingStrategy().Event()
.IndexBase("LuceneIndex")
.DirectoryProvider().FSDirectory();
-
- cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
}
protected override void AfterSetup()
View
9 src/NHibernate.Search.Fluent.Tests/Integration/SimpleMappingsSearchTest.cs
@@ -2,8 +2,6 @@
using Lucene.Net.QueryParsers;
using Lucene.Net.Util;
using NHibernate.Cfg;
-using NHibernate.Event;
-using NHibernate.Search.Event;
using NHibernate.Search.Fluent.Cfg;
using NUnit.Framework;
@@ -19,13 +17,6 @@ protected override void ConfigureSearch(Configuration cfg)
.MappingClass<DomainSearchMapping>()
.IndexingStrategy().Event()
.DirectoryProvider().RAMDirectory();
-
- cfg.SetListener(ListenerType.PostUpdate, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostInsert, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostDelete, new FullTextIndexEventListener());
- cfg.SetListener(ListenerType.PostCollectionRecreate, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionRemove, new FullTextIndexCollectionEventListener());
- cfg.SetListener(ListenerType.PostCollectionUpdate, new FullTextIndexCollectionEventListener());
}
protected override void AfterSetup()
View
22 src/NHibernate.Search.Fluent/Cfg/FluentSearchIndexingStrategyConfiguration.cs
@@ -1,3 +1,5 @@
+using NHibernate.Search.Event;
+
namespace NHibernate.Search.Fluent.Cfg
{
public class FluentSearchIndexingStrategyConfiguration
@@ -27,9 +29,27 @@ public FluentSearchConfiguration Manual()
/// <summary>
/// Sets the IndexingStrategy property to "event"
/// </summary>
+ /// <param name="registerListeners">
+ /// Register NHibernate listeners.
+ /// Listeners are added to existent listeners collection.
+ /// Default value - true.
+ /// </param>
/// <returns></returns>
- public FluentSearchConfiguration Event()
+ public FluentSearchConfiguration Event(bool registerListeners = true)
{
+ if (registerListeners)
+ {
+ var configuration = (cfg as IFluentSearchConfiguration).Configuration;
+
+ configuration.AddListener(el => el.PostInsertEventListeners, new FullTextIndexEventListener());
+ configuration.AddListener(el => el.PostUpdateEventListeners, new FullTextIndexEventListener());
+ configuration.AddListener(el => el.PostDeleteEventListeners, new FullTextIndexEventListener());
+
+ configuration.AddListener(el => el.PostCollectionRecreateEventListeners, new FullTextIndexCollectionEventListener());
+ configuration.AddListener(el => el.PostCollectionRemoveEventListeners, new FullTextIndexCollectionEventListener());
+ configuration.AddListener(el => el.PostCollectionUpdateEventListeners, new FullTextIndexCollectionEventListener());
+ }
+
return setStrategy("event");
}
}
View
1  src/NHibernate.Search.Fluent/NHibernate.Search.Fluent.csproj
@@ -81,6 +81,7 @@
<Compile Include="Mapping\Parts\IHasFieldBridge.cs" />
<Compile Include="Mapping\Parts\IndexPart.cs" />
<Compile Include="Mapping\Parts\StorePart.cs" />
+ <Compile Include="NHibernateConfigurationExtensions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionExtensions.cs" />
</ItemGroup>
View
36 src/NHibernate.Search.Fluent/NHibernateConfigurationExtensions.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq.Expressions;
+using NHibernate.Cfg;
+using NHibernate.Event;
+
+namespace NHibernate.Search.Fluent
+{
+ public static class NHibernateConfigurationExtensions
+ {
+ public static void AddListener<TListener>(
+ this Configuration configuration,
+ Expression<Func<EventListeners, TListener[]>> expression,
+ TListener listenerImpl)
+ {
+ var propertyInfo = ReflectionExtensions.GetProperty(expression);
+
+ var existentListeners = (TListener[])propertyInfo.GetValue(configuration.EventListeners, null);
+
+ var newListeners = new List<TListener>(existentListeners) { listenerImpl }.ToArray();
+
+ propertyInfo.SetValue(configuration.EventListeners, newListeners, null);
+ }
+
+ public static void AddListeners<TListener>(
+ this Configuration configuration,
+ Expression<Func<EventListeners, TListener[]>> expression,
+ IEnumerable<TListener> listeners)
+ {
+ foreach (var listener in listeners)
+ {
+ configuration.AddListener(expression, listener);
+ }
+ }
+ }
+}
View
23 src/NHibernate.Search.Fluent/ReflectionExtensions.cs
@@ -48,5 +48,28 @@ public static System.Type GetMemberType(this MemberInfo member)
var info = member as PropertyInfo;
return info != null ? info.PropertyType : ((FieldInfo)member).FieldType;
}
+
+ public static PropertyInfo GetProperty<MODEL, T>(Expression<Func<MODEL, T>> expression)
+ {
+ MemberExpression memberExpression = getMemberExpression(expression);
+ return (PropertyInfo)memberExpression.Member;
+ }
+
+ private static MemberExpression getMemberExpression<MODEL, T>(Expression<Func<MODEL, T>> expression)
+ {
+ MemberExpression memberExpression = null;
+ if (expression.Body.NodeType == ExpressionType.Convert)
+ {
+ var body = (UnaryExpression)expression.Body;
+ memberExpression = body.Operand as MemberExpression;
+ }
+ else if (expression.Body.NodeType == ExpressionType.MemberAccess)
+ {
+ memberExpression = expression.Body as MemberExpression;
+ }
+
+ if (memberExpression == null) throw new ArgumentException("Not a member access", "expression");
+ return memberExpression;
+ }
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.