Home

Awesome

.Net Documentation

SOLID, DI, N-Tier, Logs, etc with dotnet

C# (C-Sharp) language

Code Tips

The postfix x++ and the prefix ++x increment operators

int quantity = 0;
// Compare and later execute the next instruction to increment the counter
while (quantity++ < 3){ Console.WriteLine("Quantity: " + quantity.toString()) }
Console.WriteLine(++quantity); // output: 4

Optional parameters

static void MyMethod(string param1 = "a", string param2 = "b", string param3 = "c") {}
MyMethod(param3: "other value");

Output parameters

static void MyMethod(out int param1, out int param2, string param3 = "undefined") {
  //all output parameters must be assigned
  if (param3 == "undefined") { param1 = 1; param2 = 1; }
  else { param1 = 0; param2 = 0; }
}
int param1, param2;
MyMethod(out param1, out param2); // param1: 1, param2: 1

Objects

object.GetHashCode() // Unique identifier of each object

Predicates

myList.RemoveAll(myPredicate);
static bool myPredicate(CustomClass myObject) {
  return myObject.myProperty == "some value";
}

Delegates

Specify input and ouput parameters for the functions, useful for methods with the same logic

Predicate<CustomClass> myPredicate = otherPredicate;
// Define a delegate directly
myList.RemoveAll(delegate (CustomClass myObject) { 
  return myObject.myProperty == "some value";
});

Lambda expressions

Useful to write local functions that can be passed as arguments or returned as the value of function calls

myList.RemoveAll((myObject) => myObject.myProperty == "some value");

// Interfaces (Useful to create the definition of common methods in different classes)
// Find objects with the same interfaces
var listByIExample = from obj in myList
                     where obs i IExample
                     select (IExample)obj;

Tuple and ValueTuple

Useful to return multiple values with a single object

var unnamed = ("one", "two");
var named = (first: "one", second: "two");
(string frst, string sec)? nullableTuple = named;
(named == nullableTuple); // true. Member names don't participate.
static (int, string) MyValueTupleMethod()
{
  var data = (years: 5, name: "dog");
  data.years++;
  return data;
}
// And "deconstructing" feature:
(int years, string name) = MyValueTupleMethod();
var (years, name) = MyMethod();

Read-only lists

A generic list type such as IEnumerable is recommended, otherwise if it's for reading mode we have:

IReadOnlyList<CustomClass> GetMyReadOnlyList() { 
  return myList.AsReadOnly();
}

Commands

Tools

Accessing data

- ADO.NET

Support large loads and to excel at security, scalability, flexibility, and dependability. It has a bias toward a disconnected model (open a connection to the database, execute the command, and then close the connection as quickly as possible). It have the concept of connection pooling (manage the number of active connections for you). ADO.NET is its cross-platform compatibility:

.NET Framework data providers

ADO.NET

Components designed for fast data manipulation, for example:

- Entity Framework

An ORM created by Microsoft. It enables developers to manipulate data as domain-specific objects without regard to the underlying structure of the data store. You can find:

Layered Architectural Pattern (separation of concerns)

Domain Layer

The logic of the business, is a model of business. The rules and needs of the business with the workflow. Domain Layer is all about the business problem - so keep ti "pure" (Only C# classes, methods, properties, interfaces, enums), free of specific technologies, API's, libraries, frameworks, etc. The Domain Layer is the main unifying concept of the application... bridging technical concepts and business concepts. Starting with the Domain Layer help us utilize Test Driven Development. Not every application needs the complexity introduced by a domain layer.

Presentation Layer

UI Components and Process Components. It should do basic/simple formatting and validation. Also responsible for displaying exceptions, asking for user input on what to do next

Persistence Layer

The communication with Domain Layer is through C# interfaces/Contracts using Inversion of Control and Dependency Inversion. The Domain Layer only cares that the Persistence Layer abides by the terms of the contract. Besides CRUD operations, the Persistence Layer should also enforce data integrity constraints, validations, transactions, connection pooling, security, etc. Object Relation Mappers are used to mapping relational data to object instances. The Persistence Layer is merely an implementation detail (The security is in the Domain).

To transfer data the following types are used:

Application Services Layer

Application Services layer orchestrates lower-level, chattier methods to solve higher-level domain-specific problems. Focus on have Cohesive methods and classes adhere to SRP and DRY. The Application Services Layer has methods that serve the Presentation Layer by rolling-up many lower-level methods into fewer higher-level methods, orchestrating those lower-lever methods to coordinate/collaborate a solution.

Web Services Layer (WCF or ASP.NET Web API)

Serialize types for transmit over the network. The role of a Web Services layer is to expose web-callable methods to a client application. Web Service methods expose functionality of the Domain Layer of Application Services Layer.

Data Transfer Objects

Consider consolidating data structures that are passed between layers and tiers. Reduce round-trips between layers and tiers. The Persistence Layer use DBSet entities, The Domain Layer use Domain Classes and the Presentation Layer use View Model. With AutoMapper we mapping object instances in a layer to Data Transfer Objects, it assumes convention of property names being called the same, but there´s a way to "hand-roll the mapping", too.

Using DTOs we can:

Dependency Injection Pattern (Unity, Castle Windsor, Ninject, Autofac, StructureMap, etc)

We can identify 3 rules:

Exist the following principles:

We need to configure the DI Container in the "Application Root"... simply, an area of the application that executes very early on.

SOLID

The SOLID Principles are a set of guidelines to help Agile developers build more maintainable and extensible object-oriented applications.

Design Patterns with GoF (Gang of Four - Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides)

Describe the common patterns to solve common problems. A pattern is a general reusable solution to a commonly occurring problem within a given context in software design.

They are categorized in three groups:

Concepts

Collaborators 🥇

<img alt="jdnichollsc" src="https://avatars3.githubusercontent.com/u/2154886?v=3&s=117" width="117">
Juan Nicholls

Supporting 🍻

I believe in Unicorns 🦄 Support me, if you do too.

Donate Ethereum, ADA, BNB, SHIBA, USDT/USDC, DOGE, etc:

Wallet address: jdnichollsc.eth

Please let us know your contributions! 🙏

Happy coding 💯

Made with ❤️

<img width="150px" src="https://avatars0.githubusercontent.com/u/28855608?s=200&v=4" align="right">

To whom it may concern.