.NET Collections

Do not use collections from System.Collections unless you are maintaining legacy code. They don’t provide type safety and they have poor performance when used with value types.

The collections can be easily grouped in a few categories based on the interfaces they implement. These determine which operations are supported by a collection and consequently in which scenarios can they be used.

The common interface for collections is the ICollection interface. It inherits from the IEnumerable interface which provides the means for iterating through a collection of items. The ICollection interface adds the Count property and methods for modifying the collection:

The authors of the Base Class Library (BCL) believed that these suffice for implementing a simple collection. Three different interfaces extend this base interface in different ways to provide additional functionalities.

  1. Lists
    • The IList interface describes collections with items which can be accessed by their index
  2.  Sets
    • ISet interface describes a set, i.e. a collection of unique items which doesn’t guarantee to preserve their order. it will only add the item to the collection if it’s not already present in it. The return value will indicate if the item was added. The most basic implementation of ISet is the HashSet class. If you want the items in the set to be sorted, you can use SortedSet instead.
  3. Dictionaries
    • IDictionary<tkey, tvalue= » »>  stores key-value pairs instead of standalone values. The indexer allows getting and setting the values based on a key instead of an index:</tkey,></tkey,>.

 

Queue and Stack

The Queue class implements a FIFO (First in, First out) collection. Only a single item in it is directly accessible, i.e. the one that’s in it for the longest time.

The Stack class is similar to Queue, but it implements a LIFO (Last in, First out) collection. The single item that’s directly accessible in this collection is the one that was added the most recently.

Thread safety

The regular generic classes in the Base Class Library have one very important deficiency they are not entirely thread-safe. While most of them support several concurrent readers, the reading operations are still not thread-safe as no concurrent write access is allowed. As soon as the collection has to be modified, any access to it from multiple threads must be synchronized.The simplest approach to implementing such synchronization involves using the lock statement with a common synchronization object but the Base Class Library comes with the ReaderWriterLockSlim class which can be used to implement this specific functionality simpler.

Concurrent collections

The concurrent collections in the System.Collections.Concurrent namespace provide thread-safe implementations of collection interfaces.

Immutable collections

Immutable collections aren’t included in the Base Class Library. To use them, the System.Collections.Immutable NuGet package must be installed in the project. They take a different approach to making collections thread-safe. Instead of using synchronization locks as concurrent collections do, immutable collections can’t be changed after they are created. This automatically makes them safe to use in multi-threaded scenarios since there’s no way for another thread to modify them and make the state inconsistent.

When choosing a collection to use in your code, always start by thinking through which operations you will need to perform on that collection. Based on that, you can select the most appropriate collection interface. Unless you have any other special requirements, go with an implementation from the System. Collections.Generic namespace. If you’re writing a multithreaded application and will need to modify the collection from multiple threads, choose the concurrent implementation of the same interface instead. Consider immutable collections if their behavior and performance match your requirements best.

DevOps

Best definition of DevOps –  the union of people, process, and products to enable continuous delivery of value to our end users. What is DevOps – Donovan Brown

Core values of DevOps:

  • collaboration across a multidisciplinary team with a common goal, common metrics, common notion of improvement and they need to work together as one
  • process focus on more value and less waste (remove manual handoffs, delays waiting times) and doing more for the end users
  • tooling – provides automation, eliminates rework, eliminates mistakes and get feedback

observeorientdecideand act

  • focus on the velocity of this loop

To be stable and reliable you don’t need to move slower but you need to build resilience into your systems and you need to get used to do changes more frequently.

The role of an IT Manager is critical in a DevOps mindset. There is need for a middle layer that translates business needs on the execution ground.

One of the core values of DevOps is a shortened release cycle.

DevOps practices:

  1. Configuration management
    • what we are deploying, how we are deploying it, what is the configuration of what is going into production
  2. Release management
    • how to build a release pipeline that we can trust
  3. Continuous integration
    • testing the code, compile it at every single check-in
  4. Continuous deployment
    • getting it out in a testing environment at a very least,
  5. Application performance monitoring
    • production monitoring and getting performance error and usage information
  6. Test automation
    • automate all types of test (deployment, integration, user experience, UI)
  7. Infrastructure as code
    • when we deploy the code we have a related infrastructure and that infrastructure is checked in the version control as well

DevOps habits: – help drive the right culture

  1. Team autonomy and enterprise alignement
  2. Rigorous management of technical debt
    • take time in the schedule and reduce it
  3. Focus on flow of customer value
    • a mindshift for development and operations
    • dev+tester+it – gets first piece of feedback when the customer uses that feature
  4. Evidence gathered in production
    • when looking in production try to figure out new ways of doing things – it could lead to a hypothesis driven development
  5. Live site culture
    • there is no place like production
  6. Managing infrastracture as a flexible resource

 

Salary transparency

When negotiating, the less information your opponent has, the better. If you know how much everyone else in your role earns you will hold at that amount. If you have no idea, you might accept less. Making this information publicly could:

  • close the gender wage gap
  • help everyone to wheel, deal, demand and self-advocate

When a company hire another employee they have no idea how much value that person will add to the company. Salary transparency ensures that the employee knows what the company makes and the company knows what he makes which may help level salaries and eliminate discrimination.

 

Objects and data structures

A class should not push its variables out through getters and setters. It should expose abstract interfaces that allows its users to manipulate the essence of the data without having to know its implementation. We want to express data in abstract terms.

Procedural code (code using data structures) makes it easy to add new functions without changing the existing data structures. Object oriented code makes it easy to add new classes without changing existing functions.

Not everything is an object.

The Law of Demeter – a module should not know about the innards of the objects it manipulates. A method  of a class C should call only the methods of these:

  • C
  • an object created by f
  • an object passed as an argument to f
  • an object held in an instance variable of C

Talk to friends, not to strangers.

Data Transfer Objects – a very useful structure with public variables and no functions used especially when communicating with database which often become the first in series of translatio stages that convert raw data in a database into objects in the application code.

Active Records – a special form of DTO with navigational methods like save or find and are direct translations from database tables.

Objects expose behavior and hide data making it easy to add new kinds of object without changing existing behaviors. It also hard to add new behaviors to existing objects.

Data structures exposes data and have no significant behavior. That makes it easy to add new behaviors to existing data structures but makes it hard to add new data structures to existing functions.

Choose the right approach:

  • objects – the flexibility to add new data types
  • data structure – the flexibility to add new behaviors

 

 

Functions

A function (method) is a the first line of organisation in any program.

Rules for making the functions communicate their intent:

  • not small but very small
  • do one thing – can you extract another method from it with a name that is not a restatement?
  •  use descriptive names
  • number of arguments: niladic, monadic,  dyadic, triadic (to avoid)
  • no side effects, no hidden things, do only what you promised in the name
  • do something (change the state of an object )or answer to something (return some information about the object), not both
  • use exceptions instead of error codes
  • Don’t Repeat Yourself – duplication, the root of all evil.

Azure Functions C#

An Azure Function is a way to create a function that can run in the cloud and can handle/respond to different events that can happen in Azure. For example,  something was changed in a BLOB.

The developper doesn’t need to care about Virtual Machines or any infrastructure. You create the function, you deploy and you run it.

Azure Functions allows the developer to create serverless applications.

Azure Functions support triggers which is a way to start the execution of the code.

What can you do with Azure Functions?

  • building APIs and microservices
  • working with internet of things
  • task to run on a schedule (image/order processing, file maintenance)

Azure multi-region deployments

The customers demand the highest performance in availability and their expectation is that the application will run even if even a data center may be down.

We can deploy publish the application into a second region that can be even on another continent.

Azure Traffic Manager will route clients to the one of the region where we deployed. It can be configured:

  • to route the clients to the lowest latency region for that specific customer
  • use geographic routing, for example, a customer from Europe will use a data center from Europe.
  • add a standby region –  route clients to the standby region where it is something wrong with the primary region.

Geo-replication for our data – Azure SQL and Document DB replicate data around the world. For example, you can have a primary database in the first region and a secondary database in the second region. All the operations committed to the primary database will be replicated (asynchronously) to the second database.

Web application + App Service

When an Web application or an API needs to be published to Azure => App Service – Platform as a Service – no need to take care of the virtual machine or the OS. The deployments will be nearly instantaneous because all the infrastructure is already in the place so we can publish directly from the source code.

An App Service plan can be used to scale vertically to add/remove cores or to scale horizontally to add/remove instances. Or use auto-scale.

Use diagnostic tools, metrics tools, debugging tools (even a remote debug session and step through code running inside an App Service) instead of connecting remotely and inspect the environment.

Deployment slots associated with an app service (Staging, QA, Production).

Storage account (BLOB Storage) – store log and diagnostic files, user uploads, application static content (images, videos, script and CSS files) – this way the content will be pushed into Azure Content Delivery Network

Database – Azure SQL (relational model) or Document DB (storing documents) or both. The details (patches, logs)  are handled by Azure (database as a service).

To talk about – Azure Resource Group – encapsulates all these resources (App Service, App Service Plan, Storage Account, Azure SQL) – a logical container for resources – grouping all the application resources into a single group will make it easier to manage the Azure services.

 

Object Oriented Programming fundamentals – notes

Object != Class
A class provides the definition of the object type. It defines properties and actions.
An object is an instance of a class
The object variable is the variable that references the object result of the instantiation. It holds the state of the object. It retains the values of its properties.
Objects only exits at runtime.

A class – a cookie cutter
An object = a cookie made by the cookie cutter

Business object, entity

A business object is a class specifically defined for solving a business problem
An entity is anything from the real world that is significant enough to be represented as a class in the application

OOP
Object Oriented Programming is  an approach to build applications by focusing on objects that interact cleanly with one another in a way that is flexible, natural, well-crafted and testable.

In order to build to solve a real problem

Identify classes
Separate responsibilities
Establish relationships
Leverage reuse

The 4 pilons of OOP

Abstraction is a concept, a way to think: define appropriate classes by focusing on what is important for a purpose. Appropriate abstractions depends on the requirements of the application.

Cohesion
Collaboration – « uses a » – one class uses another class. Example, orderRepository uses the Order class to display the order or to serialise

Things any programmer should know #2

  • Don’t ignore an error!It will raise problems in the future.
  • Write comments only when the code doesn’t or can’t say something.
  • Technical exceptions will be handled by the application framework while the business domain exceptions by the client code.
  • Deploy often.
  • Challenge yourself by doing what you are not good at (deliberate practice).