35 votes

Returning status codes from business layer

Status codes have long gone the way of the Dodo --- made extinct by exceptions, actually. The problem with a status code is that it can be ignored. An exception cannot. Consider these two examples. ...
13 votes
Accepted

ViewModel creator design

I would say your design is fine. In general I advise injecting IHandleViewModel<T> implementations directly into consumers, because this makes it easier to ...
  • 381
12 votes

Error Handling - Controller's OnException and Application_Error

The OnException() methods does 2 things. It logs the exception It creates an return value So let us refactor these two points to two separate methods. <...
  • 50k
11 votes
Accepted

Somewhat esoteric if statement in a paginated feed

You're checking that "page does not contain any posts and page number is not one". Reversing the condition is often the easiest way to clarify it. The inverse condition would be "page contains at ...
11 votes

Unit testing with dependency injection and MOQ

There are a number of issues in your code. Naming. Methods should use verb phrases to denote the action they perform and try to be specific. So your DateList ...
  • 7,142
10 votes
Accepted

Repository and Controller tests

As Phil Sandler said, the code here is so thin that it's possibly questionable how much value unit tests actually add. However, I would say that their two main benefits are both still relevant: ...
  • 5,714
10 votes
Accepted

Naming for unit tests for controller to edit posts

Let me start out by saying that your test names are actually decent. You have already come to the conclusion that a good test name is one that can convey what is being tested without requiring someone ...
10 votes
Accepted

Checking dictionary for specified key

Not only is this difficult to read, it's also inefficient. Cache your duplicate LINQ lookup to save time and make it more readable. As for using the ternary operator, that's up to you, but I prefer ...
  • 5,167
10 votes
Accepted

MVC binding list of values to checkboxlist in an efficient way

In my opinion, your view is doing too much work. This is happening because your model is missing a property. If you add an IsSelected property to your model, I ...
  • 30.7k
10 votes
Accepted

Implementing IDisposable for an Excel class

Almost. From the MSDN docs on the Disposable Pattern: ✓ DO allow the Dispose(bool) method to be called more than once. The method might choose to do nothing ...
  • 30.7k
9 votes

Thin controller design

We have asked to keep the MVC controller as thin, so we decided to move all the service calling/business logic into the ViewModel class. The concept of the "thin" controller is a guideline to help ...
  • 361
8 votes
Accepted

Pagination implementation for a blog system

int num What does num represent exactly? Number of pages? Number of posts per page? Current page number? Properly naming the ...
7 votes

Unit of work + repository + service layer with dependency injection

public interface IUnitOfWork : IDisposable { IRepository<T> Repository<T>() where T : class; void Commit(); void Rollback(); } The goal of ...
7 votes
Accepted

Command-query segregation principle for an authenticator

Since it's been several days and this question has not been answered, I will give it a go. I'm saying this not because making Authenticate follow Command-Query ...
7 votes

ViewModel creator design

Why the marker interface? ...
7 votes
Accepted

Storing an integer counter in indexer

I suggest the following: ...
  • 43.9k
7 votes
Accepted

Writing a thin navigation controller

The responsability of a controller is to create ViewModels and return them. This usually imply some boilerplate code, creating new instances, setting parameters, ...
  • 12.3k
7 votes

Combating magic strings in a web application

First point, these extensions are really cool. I don't have much comments to give, your code looks pretty good! The ActionInfo class could be simplified a little :...
  • 12.3k
7 votes
Accepted

ASP controller for tracking equipment

Index It would appear you primarily do three things here. Sort, Filter, and Page. There's not much to say about your paging, but the sorting and filtering logic clutter the action. Try creating an ...
  • 1,960
7 votes

EventLogger for MVC application

Again enums should not have any prefix. Drop all those Enum_! You may like to take a look to .NET Naming Guidelines. Inside the class ...
6 votes

Returning status codes from business layer

something is wrong here. ...
  • 28.5k
6 votes
Accepted

Is it an anti-pattern to let ViewModel fill itself from a domain object?

It's not an anti pattern. At some point, one layer must know about the layer underneath. You also don't want to rewrite view-model mapping code in multiple controllers or actions, nor do you want to ...
6 votes

Monkeying around with Survey Monkey and Asp.Net Mvc

ASP.NET MVC will produce a controller instance for each request, so you need a bit of help from the framework to assist you with caching - because anything you cache at controller level will only live ...
6 votes
Accepted

Getting a user's property

...
  • 50k
6 votes

ASP.NET core proper way to work with the repository pattern

Better? Probably... All your repositories look almost identical, only the served type is different. Therefore, you may as well turn to generic repository. In its elementary form, adopting your ...
  • 1,990
6 votes
Accepted

User can add and delete customers

You don't need to enumerate twice here var temp = _context.Users.AsEnumerable().ToList(); You can just call .ToList(). <...
  • 8,438
6 votes

Find consecutive numbers and turn them into ranges

Letting a single method do everything is not such a good idea. You should rather separate the logic for finding consecutive numbers from creating a string from ranges and let them produce the final ...
  • 43.9k
5 votes
Accepted

Is it "legit" to use a Linq query in a razor view?

This is not an ideal way to do things. The model passed to your view should only have what you need for the view. There are a few different things going on here. One is searching for the information ...
5 votes

Returning status codes from business layer

I personally agree with Exceptions over Status Codes principle. However, once when having really deep and scattered business logic (with pluginable strategies etc.) we decided to mix them together. ...
  • 171
5 votes
Accepted

Repository implementation

Testability Your repository implements an interface which will allow it to get stubbed out easily, so that's a very testable thing. However if you want to also (unit-test) your repositories ...

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