Skip to content

Commit

Permalink
#11 Changed the visitors list in QbservableServiceOptions to a list o…
Browse files Browse the repository at this point in the history
…f visitor factories.
  • Loading branch information
RxDave committed Aug 23, 2017
1 parent 139ed4f commit 84aa7c0
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 12 deletions.
15 changes: 7 additions & 8 deletions Source/Qactive/QbservableServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;

Expand All @@ -27,7 +26,7 @@ public sealed class QbservableServiceOptions
evaluationContext = new ServiceEvaluationContext()
};

private readonly List<ExpressionVisitor> visitors = new List<ExpressionVisitor>();
private readonly List<Func<ExpressionVisitor>> visitorFactories = new List<Func<ExpressionVisitor>>();
private bool sendServerErrorsToClients;
private bool enableDuplex;
private bool allowExpressionsUnrestricted;
Expand All @@ -38,7 +37,7 @@ public sealed class QbservableServiceOptions
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Required for code contracts.")]
private void ObjectInvariant()
{
Contract.Invariant(visitors != null);
Contract.Invariant(visitorFactories != null);
}

public bool IsFrozen { get; private set; }
Expand Down Expand Up @@ -86,9 +85,9 @@ public bool AllowExpressionsUnrestricted
}

#if READONLYCOLLECTIONS
public IReadOnlyList<ExpressionVisitor> Visitors => visitors.AsReadOnly();
public IReadOnlyList<Func<ExpressionVisitor>> VisitorFactories => visitorFactories.AsReadOnly();
#else
public ReadOnlyCollection<ExpressionVisitor> Visitors => visitors.AsReadOnly();
public ReadOnlyCollection<Func<ExpressionVisitor>> VisitorFactories => visitors.AsReadOnly();
#endif

public ExpressionOptions ExpressionOptions
Expand Down Expand Up @@ -143,13 +142,13 @@ public QbservableServiceOptions(QbservableServiceOptions clone)
evaluationContext = clone.evaluationContext;
}

public QbservableServiceOptions Add(ExpressionVisitor visitor)
public QbservableServiceOptions Add(Func<ExpressionVisitor> visitorFactory)
{
Contract.Requires(visitor != null);
Contract.Requires(visitorFactory != null);

var options = IsFrozen ? Clone() : this;

options.visitors.Add(visitor);
options.visitorFactories.Add(visitorFactory);

return options;
}
Expand Down
4 changes: 3 additions & 1 deletion Source/Qactive/ServerQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ private Expression PrepareExpression(out IQbservableProvider realProvider)

preparedExpression = visitor.Visit(preparedExpression);

foreach (var customVisitor in Provider.Options.Visitors)
foreach (var customVisitorFactory in Provider.Options.VisitorFactories)
{
var customVisitor = customVisitorFactory();

preparedExpression = customVisitor.Visit(preparedExpression);
}

Expand Down
2 changes: 1 addition & 1 deletion Testing/Qactive.Tests/ServiceOptions/VisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class VisitorTests : TestBase
public async Task SimpleVisitor()
{
var visitor = new TestExpressionVisitor();
var service = TestService.Create(QbservableServiceOptions.Default.Add(visitor), Observable.Range(1, 5));
var service = TestService.Create(QbservableServiceOptions.Default.Add(() => visitor), Observable.Range(1, 5));

var results = await service.QueryAsync(source => from value in source
where value % 2 == 0
Expand Down
2 changes: 1 addition & 1 deletion Testing/Qactive.Tests/Tcp/ServiceOptions/VisitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class VisitorTests : TestBase
public async Task SimpleVisitor()
{
var visitor = new TestExpressionVisitor();
var service = TcpTestService.Create(QbservableServiceOptions.Default.Add(visitor), Observable.Range(1, 5));
var service = TcpTestService.Create(QbservableServiceOptions.Default.Add(() => visitor), Observable.Range(1, 5));

var results = await service.QueryAsync(source => from value in source
where value % 2 == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class VisitorTests : TestBase
public async Task SimpleVisitor()
{
var visitor = new TestExpressionVisitor();
var service = WebSocketTestService.Create(QbservableServiceOptions.Default.Add(visitor), Observable.Range(1, 5));
var service = WebSocketTestService.Create(QbservableServiceOptions.Default.Add(() => visitor), Observable.Range(1, 5));

var results = await service.QueryAsync(source => from value in source
where value % 2 == 0
Expand Down

0 comments on commit 84aa7c0

Please sign in to comment.