As of May 31, 2023, we have updated our Code of Conduct.
14 votes
Accepted

ASP.NET REST controller with try-catch error handling

throw ex; This is a fatal mistake. It'll create a new stack-trace and you won't be able to tell where the actual exception occured. If you want to rethrow it then ...
t3chb0t's user avatar
  • 44k
13 votes

C# Am I using proper SOLID principles?

I suppose the simplest thing is to go through the principles: SRP - Cannot see any 'too big' things in your example OCP - The example is small enough and you are not using class inheritance that I ...
Jamie Stevenson's user avatar
6 votes
Accepted

WebApi Get with paging

Let's start with the BUG. if (entity.Count() == 0 || entity == null) If entity is null, ...
RubberDuck's user avatar
  • 30.8k
6 votes

Connect to MySQL and fetch a list of customers asynchronously using C#

In Get() you do not need to wrap the call to GetAllCustomers() into a Task because it's ...
Adriano Repetti's user avatar
6 votes

C# Am I using proper SOLID principles?

IMO returning IEnumerable<T> from an API is a violation of "L", as running GetEnumerator() a second time (or ...
David Browne - Microsoft's user avatar
5 votes
Accepted

Method GetById and SingleResult

You can wrap the single entity into an IQueryable. Something like this should work: SingleResult.Create(new[]{entity}.AsQueryable()); You could write a helper ...
Pierre Menard's user avatar
5 votes
Accepted

N-tiered web app to save a role

Naming conventions. In C#, types are named using PascalCase. Rename: OM_Role to OmRole ...
dcastro's user avatar
  • 1,167
5 votes

Generic WebRequest Method for .PCL

Thing that makes me more perplex is error-handling. First of all you're catching all exceptions, it may hide subtle bugs unrelated to web request itself (for example wrong parameters you supply in ...
Adriano Repetti's user avatar
5 votes

ASP.NET REST controller with try-catch error handling

Typically in an ASP.NET app (and in fact in any app), you should only catch and handle the base System.Exception in a single place as close to the application entry ...
Matt Cole's user avatar
  • 948
5 votes

Custom HttpClient implementation for third part usage with sync/async calls

internal class Client : IDisposable { private static HttpClient _client; private static Uri _baseAddress; Neither of those fields should be ...
Peter Taylor's user avatar
4 votes

.NET REST API JSON Filtering

I know this is an old post but I tried doing something similar recently so thought I should share it here. The code looks great but it still returns unwanted properties with 'Null' values. What I have ...
shwetaOnStack's user avatar
4 votes

Method GetById and SingleResult

[EnableQuery] and SingleResult does in fact return 404 OOTB, that is one of the key reasons for ...
Chris Schaller's user avatar
4 votes

C# Async Oauth API Wrapper Class

Do not use a public field; instead, make it a property: public bool isInitialized = false; Your code is barely above 100 lines, you don't need ...
BCdotWEB's user avatar
  • 11.1k
4 votes

Generic WebRequest Method for .PCL

I would define it as an extension to the Uri type, so it could be used this way: ...
Dmitry Nogin's user avatar
  • 6,113
4 votes
Accepted

Claiming coupons

Short answer is maybe. If this is the only code that updates it then you will be fine but you are making a gateway that only one person can update at a time even if they have different codes. If your ...
CharlesNRice's user avatar
  • 4,253
4 votes

Web API for reading file system

It's a very bad idea to call the controller a FileSystem and inform the user that he's going to be working with well, a file system. He should not know how you are ...
t3chb0t's user avatar
  • 44k
4 votes
Accepted

Web API for reading file system

I think one of the most common errors in today times, is for example handling potential errors, as you try to do here: ...
Icepickle's user avatar
  • 835
4 votes
Accepted

Reading MultiPart From Data with descriptors

The naming and variable conventions in your method look like it was written by at least three different persons: ...
t3chb0t's user avatar
  • 44k
4 votes
Accepted

IsDatabaseUp returns true or throws exception

The naming convention Is… implies that the function is a predicate, returning either true or false. If you wanted to throw an exception when the database is down, ...
200_success's user avatar
4 votes
Accepted

Setting up pagination links inside a WebAPI Controller

The intention When I have all matching objects - I pass them to a "Paging" method within the service (I think I have to do this in the service rather than the repository because a generic repo ...
Flater's user avatar
  • 5,532
4 votes
Accepted

Trying to map JSON to different classes in web API

The first thing to do is to remove duplicated code. If you do not need further processing DeserializeObject<T>() returns T ...
Adriano Repetti's user avatar
4 votes

ASP.NET REST controller with try-catch error handling

The linked post adds a neat summary: So, to sum up: Don’t catch fatal exceptions; nothing you can do about them anyway, and trying to generally makes it worse. Fix your code so that it ...
Flater's user avatar
  • 5,532
4 votes

Subscribe to Event form one service to another

in your case I would try to avoid subscribing to events as this couples the projects and references and prevents the garbage collector to clean up the unused service. if you are creating new services ...
Mightee's user avatar
  • 141
3 votes
Accepted

API controller with database queries

I see two improvements, one of which Gert Arnold pointed on in a comment. If an API method returns a list, collection or IEnumerable of anything, worse case scenario you should return an empty ...
Greg Burghardt's user avatar
3 votes
Accepted

Multiple connection string in Web API 2

First of all I would highlight a security problem: you're directly exposing your implementation to service users. They just need to try different Client-IDs to ...
Adriano Repetti's user avatar
3 votes
Accepted

Building a custom response body based on API consumer's need

Possible problem Assume one would pass a request like api/characters/2?fields=id,displayname,mainImageUrl,displayname then the Assemble<TEntity, TDto>() ...
Heslacher's user avatar
  • 50.1k
3 votes
Accepted

Implementation Async/Await

Your implementation is technically correct, but... There is no benefit in using Task.Run in an web application. All you're doing is deferring work from one thread ...
dcastro's user avatar
  • 1,167
3 votes
Accepted

WebApi action that updates entity using stored procedure

You don't have a people, you have a person. Saying if (person.IsMain == true) is exactly ...
RubberDuck's user avatar
  • 30.8k
3 votes
Accepted

Choose the right type for method GetById

If you're only going to make the minimal required change Accommodate the IQueryable requirement in the layer where it is needed, not in your repository layer. That will probably be some object that ...
moarboilerplate's user avatar
3 votes

MVC-Web API 2 integration

Avoid duplicated magic strings, like "http://www.ortund.com/". Assign to a constant instead. This snippet appears repeated multiple times: ...
janos's user avatar
  • 110k

Only top scored, non community-wiki answers of a minimum length are eligible