question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make Result types constructors public

See original GitHub issue

Background and Motivation

Result types were changed to be public but was decided to keep ctors internal to have a better design discussion.

The original proposal is described in #40656

Proposed API

namespace Microsoft.AspNetCore.Http;

public class EmptyHttpResult : IResult
{
-    public static readonly EmptyHttpResult Instance { get; } = new();
+    public EmptyHttpResult() {} 
}

public sealed partial class StatusCodeHttpResult : IResult
{
+    public StatusCodeHttpResult(int statusCode) {}
}

public class NoContentHttpResult : IResult
{
+    public NoContentHttpResult() {}
}

public class UnauthorizedHttpResult : IResult
{
+    public UnauthorizedHttpResult() {} 
}

public sealed class AcceptedAtRouteHttpResult : IResult
{
+    public AcceptedAtRouteHttpResult(string? routeName = null, 
+                                     object? routeValues = null, 
+                                     object? value = null) {}
}

public sealed class CreatedAtRouteHttpResult : IResult
{
+    public CreatedAtRouteHttpResult(string? routeName = null, 
+                                     object? routeValues = null, 
+                                     object? value = null) {}
}

public sealed class AcceptedHttpResult : IResult
{
+    public AcceptedHttpResult(string? location, object? value = null) {}
+    public AcceptedHttpResult(Uri locationUri, object? value = null) {}
}

public sealed class CreatedHttpResult : IResult
{
+    public CreatedHttpResult(string? location = null, object? value = null) {}
+    public CreatedHttpResult(Uri locationUri = null, object? value = null) {}
}

public sealed class BadRequestObjectHttpResult : IResult
{
+    public BadRequestObjectHttpResult(object? error = null) {}
}

public sealed class ConflictObjectHttpResult : IResult
{
+    public ConflictObjectHttpResult(object? error = null) {}
}

public sealed class NotFoundObjectHttpResult : IResult
{
+    public NotFoundObjectHttpResult(object? value = null) {}
}

public sealed class OkObjectHttpResult : IResult
{
+    public OkObjectHttpResult(object? value = null) {}
}

public sealed class UnprocessableEntityObjectHttpResult : IResult
{
+    public UnprocessableEntityObjectHttpResult(object? value = null) {}
}

public sealed class ProblemHttpResult : IResult
{
+    public ProblemHttpResult(ProblemDetails problemDetails) {}
+    public ProblemHttpResult(
+        string? detail = null,
+        string? instance = null,
+        int? statusCode = null,
+        string? title = null,
+        string? type = null,
+        IDictionary<string, object?>? extensions = null) {}
}

public sealed class JsonHttpResult : IResult
{
+    public JsonHttpResult(object? value = null, JsonSerializerOptions? jsonSerializerOptions = null) {}

-    public string ContentType { get; }
+    public string ContentType { get; init; }

-    public int? StatusCode { get; }
+    public int? StatusCode { get; init; }
}

public sealed partial class ContentHttpResult : IResult
{
+    public ContentHttpResult(string? content = null, string? contentType = null) {}

-    public int? StatusCode { get; }
+    public int? StatusCode { get; init; }
}

public sealed partial class FileContentHttpResult : IResult
{
+    public FileContentHttpResult(ReadOnlyMemory<byte> fileContents, string? contentType = null) {}

-    public bool EnableRangeProcessing { get; }
+    public bool EnableRangeProcessing { get; init; }

-    public EntityTagHeaderValue? EntityTag { get; }
+    public EntityTagHeaderValue? EntityTag { get; init; }

-    public string? FileDownloadName { get; }
+    public string? FileDownloadName { get; init; }

-    public DateTimeOffset? LastModified { get; }
+    public DateTimeOffset? LastModified { get; init; }
}

public sealed class FileStreamHttpResult : IResult
{
+    public FileStreamHttpResult(Stream fileStream, string? contentType = null) {}

-    public bool EnableRangeProcessing { get; }
+    public bool EnableRangeProcessing { get; init; }

-    public EntityTagHeaderValue? EntityTag { get; }
+    public EntityTagHeaderValue? EntityTag { get; init; }

-    public string? FileDownloadName { get; }
+    public string? FileDownloadName { get; init; }

-    public DateTimeOffset? LastModified { get; }
+    public DateTimeOffset? LastModified { get; init; }
}

public sealed partial class PhysicalFileHttpResult : IResult
{
+    public PhysicalFileHttpResult(string fileName, string? contentType = null) {}

-    public bool EnableRangeProcessing { get; }
+    public bool EnableRangeProcessing { get; init; }

-    public EntityTagHeaderValue? EntityTag { get; }
+    public EntityTagHeaderValue? EntityTag { get; init; }

-    public string? FileDownloadName { get; }
+    public string? FileDownloadName { get; init; }

-    public DateTimeOffset? LastModified { get; }
+    public DateTimeOffset? LastModified { get; init; }
}

public sealed class PushStreamHttpResult : IResult
{
+    public PushStreamHttpResult(Func<Stream, Task> streamWriterCallback, string? contentType = null) {}

-    public bool EnableRangeProcessing { get; }
+    public bool EnableRangeProcessing { get; init; }

-    public EntityTagHeaderValue? EntityTag { get; }
+    public EntityTagHeaderValue? EntityTag { get; init; }

-    public string? FileDownloadName { get; }
+    public string? FileDownloadName { get; init; }

-    public DateTimeOffset? LastModified { get; }
+    public DateTimeOffset? LastModified { get; init; }
}

public sealed partial class VirtualFileHttpResult : IResult
{
+    public VirtualFileHttpResult(string fileName, string? contentType = null) {}

-    public bool EnableRangeProcessing { get; }
+    public bool EnableRangeProcessing { get; init; }

-    public EntityTagHeaderValue? EntityTag { get; }
+    public EntityTagHeaderValue? EntityTag { get; init; }

-    public string? FileDownloadName { get; }
+    public string? FileDownloadName { get; init; }

-    public DateTimeOffset? LastModified { get; }
+    public DateTimeOffset? LastModified { get; init; }
}

public sealed partial class ChallengeHttpResult : IResult
{
+    public ChallengeHttpResult(AuthenticationProperties? properties = null) {}
+    public ChallengeHttpResult(AuthenticationProperties? properties, string authenticationScheme) {}
+    public ChallengeHttpResult(AuthenticationProperties? properties, IList<string> authenticationSchemes) {}
}

public sealed partial class ForbidHttpResult : IResult
{
+    public ForbidHttpResult (AuthenticationProperties? properties = null) {}
+    public ForbidHttpResult (AuthenticationProperties? properties, string authenticationScheme) {}
+    public ForbidHttpResult (AuthenticationProperties? properties, IList<string> authenticationSchemes) {}
}

public sealed partial class SignOutHttpResult : IResult
{
+    public SignOutHttpResult (AuthenticationProperties? properties = null) {}
+    public SignOutHttpResult (AuthenticationProperties? properties, string authenticationScheme) {}
+    public SignOutHttpResult (AuthenticationProperties? properties, IList<string> authenticationSchemes) {}
}

public sealed partial class SignInHttpResult : IResult
{
+    public SignInHttpResult(ClaimsPrincipal principal) {}

-    public string? AuthenticationScheme { get; }
+    public string? AuthenticationScheme { get; init; }

-    public AuthenticationProperties? Properties { get; }
+    public AuthenticationProperties? Properties { get; init; }
}

public sealed partial class RedirectHttpResult : IResult
{
+    public RedirectHttpResult(string url, bool acceptLocalUrlOnly = false) {}

-    public bool Permanent { get; }
+    public bool Permanent { get; init; }

-    public bool PreserveMethod { get; }
+    public bool PreserveMethod { get; init; }
}

public sealed partial class RedirectToRouteHttpResult : IResult
{
+    public RedirectToRouteHttpResult(string? routeName = null, object? routeValues = null) {}

-    public bool Permanent { get; }
+    public bool Permanent { get; init; }

-    public bool PreserveMethod { get; }
+    public bool PreserveMethod { get; init; }

-    public string Fragment { get; }
+    public string Fragment { get; init; }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
davidfowlcommented, Mar 29, 2022

My thinking is we should get rid of the public Instance properties altogether and just return the shared instance from Results. and Results.Typed. methods.

Also the non typed ones. I guess I wasn’t clear but I expected us to change the existing impl to cache.

1reaction
halter73commented, Mar 28, 2022

If we decide to add constructors to any of these types, I think every one of them should have at least one constructor even if there’s an equivalent Instance property that’s more efficient. Most people aren’t going to care about one extra allocation a request, so I don’t think that’s an excuse to make the API less consistent.

My thinking is we should get rid of the public Instance properties altogether and just return the shared instance from Results. and Results.Typed. methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Providing Constructors for Your Classes
A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except ...
Read more >
Constructor in Java
Constructor in java is used to create the instance of the class. Constructors are almost similar to methods except for two things -...
Read more >
java - Why am I being asked for a return type when creating ...
I'm trying to make a class and create two constructors in it. I've created it as I have done all of my previous...
Read more >
Java Constructors
Java Constructors. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object...
Read more >
5.2. Writing Constructors — AP CSAwesome
The constructors you write will almost always be marked public . Like methods, constructors also have a parameter list specified in parenthesis that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found