Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting code: single type per file #3644

Merged
merged 3 commits into from
Mar 15, 2017
Merged

Extracting code: single type per file #3644

merged 3 commits into from
Mar 15, 2017

Conversation

xavierdecoster
Copy link
Member

The below types will be affected by semver2-related work (addition of semVerLevel optional parameter).
This separate PR is just another minor refactoring in preparation of semver2-related PRs (avoid pollution + easier diffs).

@skofman1
Copy link
Contributor

🌷 One type per file... what a beauty

Copy link
Contributor

@scottbommarito scottbommarito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the potential for a lot of shared code here. I think if we're going to fix this code we might as well improve it all around!

{
public interface IPackageIdsQuery
{
Task<IEnumerable<string>> Execute(
Copy link
Contributor

@scottbommarito scottbommarito Mar 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPackageIdsQuery and IPackageVersionsQuery are nearly identical, the only difference being the name of the parameter. Personally I would merge them into a single interface, IPackageQuery. It will also allow you to share code between the autocomplete queries and the SQL queries more easily.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

queryString += "&prerelease=" + includePrerelease.Value;
}

var endpoints = await _serviceDiscoveryClient.GetEndpointsForResourceType(_autocompleteServiceResourceType);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the code here is shared between AutocompleteServicePackageIdsQuery and AutocompleteServicePackageVersionsQuery--the construction of the query string is the only part that seems to differ at all. I would suggest making a single base class that has all of the logic that calls the search service and parses its response. You can then call into this shared functionality with the query string you've constructed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although the code looks similar, the functionality is quite different. In addition, the interfaces are markers for their specific use cases and configured separately in the DI container. See https://github.com/NuGet/NuGetGallery/blob/master/src/NuGetGallery/App_Start/DefaultDependenciesModule.cs#L324.

Would consider such change out-of-scope for this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can agree that consolidating the two interfaces into IPackageQuery is outside the scope of this PR, but consolidating the code between the two AutocompleteService query classes is minor and I think fits within it well.

public class AutoCompletePackageQuery {
    private readonly ServiceDiscoveryClient _serviceDiscoveryClient;
    private readonly string _autocompleteServiceResourceType;
    private readonly RetryingHttpClientWrapper _httpClient;

    public AutoCompletePackageQuery(IAppConfiguration configuration)
    {
        _serviceDiscoveryClient = new ServiceDiscoveryClient(configuration.ServiceDiscoveryUri);
        _autocompleteServiceResourceType = configuration.AutocompleteServiceResourceType;
        _httpClient = new RetryingHttpClientWrapper(new HttpClient());
    }

    public async Task<IEnumerable<string>> RunQuery(string queryString, bool? includePrerelease) {
    {
        queryString += $"&prerelease={includePrerelease.HasValue ? includePrerelease.Value : false}";
        var endpoints = await _serviceDiscoveryClient.GetEndpointsForResourceType(_autocompleteServiceResourceType);
        endpoints = endpoints.Select(e => new Uri(e + "?" + queryString)).AsEnumerable();
        
        var result = await _httpClient.GetStringAsync(endpoints);
        var resultObject = JObject.Parse(result);

        return resultObject["data"].Select(entry => entry.ToString());
    }
}

so that you can do

public class AutocompleteServicePackageIdsQuery : AutoCompletePackageQuery, IPackageIdsQuery
{
    public async Task<IEnumerable<string>> Execute(string partialId, bool? includePrerelease)
    {
        partialId = partialId ?? string.Empty;
        return RunQuery("take=30&q=" + Uri.EscapeUriString(partialId), includePrerelease);
    }
}

and

public class AutocompleteServicePackageVersionsQuery : AutoCompletePackageQuery, IPackageIdsQuery
{
    public async Task<IEnumerable<string>> Execute(string id, bool? includePrerelease)
    {
        if (string.IsNullOrWhiteSpace(id))
        {
            throw new ArgumentNullException(nameof(id));
        }
        return RunQuery("id=" + Uri.EscapeUriString(id), includePrerelease);
    }
}

Copy link
Member

@joelverhagen joelverhagen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seems to be some value in consolidating the code per @scottbommarito's suggestion. Not sure if there is test coverage so please limit refactoring to tested code.

@xavierdecoster
Copy link
Member Author

@scottbommarito implemented your suggestion, can you please have another look at it?

Copy link
Contributor

@scottbommarito scottbommarito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks awesome!


public AutoCompleteDatabaseQuery(IEntitiesContext entities)
{
_dbContext = (DbContext)entities;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null check


public AutoCompleteServiceQuery(IAppConfiguration configuration)
{
_serviceDiscoveryClient = new ServiceDiscoveryClient(configuration.ServiceDiscoveryUri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@xavierdecoster xavierdecoster merged commit 222e6f9 into dev Mar 15, 2017
@xavierdecoster xavierdecoster deleted the dev-refactoring branch March 15, 2017 07:58
@shishirx34 shishirx34 mentioned this pull request Mar 20, 2017
15 tasks
jozefizso added a commit to goit/NuGetGallery that referenced this pull request Apr 20, 2017
…7.03.27

* tag 'v2017.03.27': (205 commits)
  Revert UpdateIsLatest optimistic concurrency changes (NuGet#3707)
  UpdateIsLatest concurrent unlist fix (NuGet#3695)
  Change telemetry time to use correct format (NuGet#3690)
  Fix typo of "publically" (NuGet#3636)
  Fix regression (NuGet#3667)
  Add credential to Register and RequestPasswordReset audits (NuGet#3666)
  Functional test for temp keys (NuGet#3664)
  Telemetry for temp keys (NuGet#3662)
  Temp keys implementation (NuGet#3563) (NuGet#3646)
  Extracting code: single type per file (NuGet#3644)
  Telemetry for package push (NuGet#3649)
  Upgrade to NuGet.* v4.0.0 dependencies (NuGet#3643)
  Fix concurrent push test by disabling search hijacking on feed (NuGet#3641)
  Fixing Package Description truncation (NuGet#3638)
  Fix Microsoft Account removal (NuGet#3639)
  Send e-mail when a new API key is created (NuGet#3634)
  IsLatest Fix: wrong connection string passed to retry context (NuGet#3632)
  Update WindowsAzure.Storage to 7.0.0 (NuGet#3633)
  Depend on signed version of Elmah (NuGet#3609)
  Move AzureEntityList and TableErrorLog to NuGetGallery.Core (NuGet#3607)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants