Skip to content

Instantly share code, notes, and snippets.

View ChuckkNorris's full-sized avatar

Levi ChuckkNorris

  • Credera
  • Denver, CO
View GitHub Profile
namespace PredictableCoding.Controllers.Entities
{
public class Movie {
public string Title { get; set; }
public string Description { get; set; }
public Actor Actors { get; set; }
private string _Genre;
public string Genre {
get => _Genre;
@ChuckkNorris
ChuckkNorris / UsePropertiesToCleanData.cs
Last active September 6, 2017 03:54
Use properties to clean data
// https://github.com/ChuckkNorris/PredictableCoding/blob/master/Entities/Movie/Movie.cs
public class Movie {
public string Title { get; set; }
public string Description { get; set; }
public Actor Actors { get; set; }
private string _Genre;
public string Genre {
get => _Genre;
@ChuckkNorris
ChuckkNorris / NullCoalescingOperator.cs
Created September 26, 2017 05:07
Null Coalescing Operator
public class NullCoalescingOperator
{
// Write your code to be expectant of null values
public IEnumerable<Movie> SearchMovies(string searchTerm)
{
IEnumerable<Movie> toReturn;
string lowerSearchTerm = searchTerm.ToLower();
// Search Movies
toReturn = this.Movies.Where(movie =>
@ChuckkNorris
ChuckkNorris / CircularDependencies.cs
Created October 2, 2017 04:56
Avoid injecting services into other services
public class MovieService : IService
{
private UserService _userService;
public MovieService(UserService userService)
{
this._userService = userService;
}
}
@ChuckkNorris
ChuckkNorris / MovieSearch.cs
Created October 2, 2017 05:00
Plan for null values
public IEnumerable<MovieDto> SearchMovies(string searchTerm) {
IEnumerable<MovieDto> toReturn = new List<MovieDto>();
string lowerSearchTerm = searchTerm?.ToLower();
// Search Movies
if (!String.IsNullOrEmpty(lowerSearchTerm))
{
toReturn = this.Movies?.Where(movie =>
(movie?.Name?.ToLower().Contains(searchTerm) ?? false)
|| (movie?.Actors?.Any(actor =>
(actor?.FirstName?.ToLower().Contains(searchTerm) ?? false)
@ChuckkNorris
ChuckkNorris / GetUser.cs
Created October 2, 2017 05:06
Returning null is a good indication that an object could not be found
public UserDto GetUser(string email) {
if (string.IsNullOrEmpty(email)) throw new ArgumentException("message", nameof(email));
UserDto toReturn = null;
toReturn = Users?.FirstOrDefault(user => user?.Email == email);
return toReturn;
}
@ChuckkNorris
ChuckkNorris / Startup.cs
Last active October 4, 2017 04:30
Add services to the container using reflection to avoid the tedious task of manually adding each service
public void ConfigureServices(IServiceCollection services) {
services.AddMvc();
InjectServices(services);
}
private void InjectServices(IServiceCollection services) {
// This will become very tedious later
//services.AddTransient<UserService>();
//services.AddTransient<MovieService>();
@ChuckkNorris
ChuckkNorris / ArgumentExceptions.cs
Created October 3, 2017 02:10
Arguments should be validated
public UserDto GetUser(string email) {
if (!ValidationUtil.IsValidEmail(email))
throw new ArgumentException("Must be a properly formatted email", nameof(email));
UserDto toReturn = null;
toReturn = Users?.FirstOrDefault(user => user?.Email == email);
return toReturn;
}
public bool AreCredentialsValid(string email, string password) {
bool toReturn = false;
@ChuckkNorris
ChuckkNorris / ExceptionHandlineMiddleware.cs
Created October 3, 2017 02:27
Create a middleware to handle user friendly exceptions
// Middlware intercepts exceptions thrown during a request
private static Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = HttpStatusCode.InternalServerError; // 500 if unexpected
string userErrorMessage = "Sorry, something went wrong";
if (exception is UserFriendlyException) {
code = HttpStatusCode.BadRequest;
userErrorMessage = exception.Message;
}
//else if (exception is MyUnauthorizedException) code = HttpStatusCode.Unauthorized;
@ChuckkNorris
ChuckkNorris / Utilities.cs
Created October 4, 2017 03:38
But expect that it the arguments might be null
public class ValidationUtil {
public static bool IsValidEmail(string email) {
bool toReturn = false;
if (!String.IsNullOrEmpty(email)) {
string emailPattern = @"^[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*"
+ "@"
+ @"((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$";
Regex reg = new Regex(emailPattern);
toReturn = reg.IsMatch(email);
}