Skip to content

Commit

Permalink
Merge pull request #33 from ZEXSM/bugfix/use-builder-singleton
Browse files Browse the repository at this point in the history
#ZEXSM#bugfix use builder singleton
  • Loading branch information
ZEXSM authored Sep 23, 2020
2 parents 85044f1 + 9a33a0e commit bc41663
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
7 changes: 2 additions & 5 deletions src/OData.QueryBuilder/Builders/ODataQueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@
using OData.QueryBuilder.Options;
using OData.QueryBuilder.Resources;
using System;
using System.Text;

namespace OData.QueryBuilder.Builders
{
public class ODataQueryBuilder<TResource> : ODataQueryResource<TResource>
{
public ODataQueryBuilder(Uri baseUrl, ODataQueryBuilderOptions odataQueryBuilderOptions = default)
: base(
new StringBuilder($"{baseUrl.OriginalString.TrimEnd(QuerySeparators.SlashChar)}{QuerySeparators.SlashString}"),
: base($"{baseUrl.OriginalString.TrimEnd(QuerySeparators.SlashChar)}{QuerySeparators.SlashString}",
odataQueryBuilderOptions ?? new ODataQueryBuilderOptions())
{
}

public ODataQueryBuilder(string baseUrl, ODataQueryBuilderOptions odataQueryBuilderOptions = default)
: base(
new StringBuilder($"{baseUrl.TrimEnd(QuerySeparators.SlashChar)}{QuerySeparators.SlashString}"),
: base($"{baseUrl.TrimEnd(QuerySeparators.SlashChar)}{QuerySeparators.SlashString}",
odataQueryBuilderOptions ?? new ODataQueryBuilderOptions())
{
}
Expand Down
10 changes: 4 additions & 6 deletions src/OData.QueryBuilder/Resources/ODataQueryResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,20 @@ public class ODataQueryResource<TResource> : IODataQueryResource<TResource>
{
private readonly VisitorExpression _visitorExpression;
private readonly ODataQueryBuilderOptions _odataQueryBuilderOptions;
private readonly StringBuilder _stringBuilder;
private readonly string _resourse;

public ODataQueryResource(StringBuilder stringBuilder, ODataQueryBuilderOptions odataQueryBuilderOptions)
public ODataQueryResource(string resourse, ODataQueryBuilderOptions odataQueryBuilderOptions)
{
_visitorExpression = new VisitorExpression(odataQueryBuilderOptions);
_odataQueryBuilderOptions = odataQueryBuilderOptions;
_stringBuilder = stringBuilder;
_resourse = resourse;
}

public IODataOption<TEntity> For<TEntity>(Expression<Func<TResource, object>> entityResource)
{
var query = _visitorExpression.ToString(entityResource.Body);

_stringBuilder.Append(query);

return new ODataOption<TEntity>(_stringBuilder, _odataQueryBuilderOptions);
return new ODataOption<TEntity>(new StringBuilder($"{_resourse}{query}"), _odataQueryBuilderOptions);
}
}
}
15 changes: 15 additions & 0 deletions test/OData.QueryBuilder.Test/ODataQueryOptionListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ public void ODataQueryBuilderList_ExpandNested_Success()
})
.ToUri();

uri = _odataQueryBuilderDefault
.For<ODataTypeEntity>(s => s.ODataType)
.ByList()
.Expand(f =>
{
f.For<ODataKindEntity>(s => s.ODataKind)
.Expand(ff => ff.For<ODataCodeEntity>(s => s.ODataCode).Select(s => s.IdCode))
.Select(s => s.IdKind);
f.For<ODataKindEntity>(s => s.ODataKindNew)
.Select(s => s.IdKind);
f.For<ODataKindEntity>(s => s.ODataKindNew)
.Select(s => s.IdKind);
})
.ToUri();

uri.OriginalString.Should().Be("http://mock/odata/ODataType?$expand=ODataKind($expand=ODataCode($select=IdCode);$select=IdKind),ODataKindNew($select=IdKind),ODataKindNew($select=IdKind)");
}

Expand Down

0 comments on commit bc41663

Please sign in to comment.