WHAT'S NEW?
Loading...

Day 4 Programming in C# 70-483

Index

  • Introduction
  • Handling exceptions
  • Throwing exceptions
  • Custom exceptions
  • References

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.


Handling exceptions

Exceptions are key point for your coding projects, by implementing a well designed exception handling system you will find out where the errors are in your system more easily and you'll get more efficient.

Exceptions are actually an object which contains a lot of useful information about what went wrong. They inherit from System.Exception and their structure is given by the reserved word: try and catch (optional). Your are allowed to define one or more catch blocks, so you can handle different types of exceptions. Keep in mind you have to declare those catch blocks from most-specific to less-specific, ie:


See here above how we are trying to parse a piece of text and effectively, we are getting a wrong format exception. On the other side, if the format were properly provided but another error raises like an overflow exception for instance, then the second catch block would be executed instead.

Since C#1, developers were allowed to create empty catch blocks to get exceptions from other languages like C++.

Sometimes, you might want to run a code always either an exception is raised or not. Typically this is done to release resources. Then you can define a finally block after the last catch statement.

In some scenarios you might want to prevent a finally block to be executed. You can achieve this by performing an Environment.FailFast and accessing one of its two implementations (one takes only an string, the other takes the string an the exception object). When this method is triggered the call is logged within the Windows application event log system and then the application is closed. Ie:


In this example, if the user introduces "10" as input, we'll be getting an exception and our app will be closed. When this happens, you can check some properties within the exception object like: 
  • StackTrace: contains a list with all the methods triggered to get to the current point.
  • InnerException: this happens when a new exception is thrown because of another exception.
  • Message: a human-readable message describing the exception.
  • ...
Finally, bear in mind that your finally blocks can't raise any exception. If not, the finally block might not be completely executed and the control will be passed to the next try/catch block, if there's any.

Only catch exceptions when you can resolve the issue or log the error. Try to avoid general catch blocks at the lower layers of your apps. The logging should also done in the higher layers, that way you will avoid duplicating logging messages.

Throwing exceptions

Use the reserved word throw to trigger a particular exception, then the CLR will start looking for a try/catch block which will handle the error. You can throw an exception in three different ways:
  1. Using throw without any identifier: this won't modify the call stack
  2. Using throw with the original exception: this will reset the call stack to the current location, so you lose from where the original exception came and will be harder to debug.
  3. Using throw with a new exception: this happens when you want to send a different exception to the caller. Imagine a calculator application which makes the arithmetic calls to a different engine. If something goes wrong in the math engine we'll end up with some kind of customized exception, ie: SumException. You don't want to send that back to the user, then you can instantiate a new exception object which contains relevant information for your users, but also links with the original exception.

There is a functionality added in C#5 which allows you to trigger an exception outside of the try/catch block with all the information related the exception. You need to call the ExceptionDispatchInfo.Throw() method and is generally used when you want to catch an exception in one thread and throw it on another thread. Is defined in the System.Runtime.ExceptionServices namespaces and it preserves the stack trace. See here below an example of the usage:


Notice how we capture the exception within the catch block, so then we can use it after the try/catch block. The code only fails if we don't introduce a number.

One thing you should be aware of is not to throw exceptions when dealing with expected situations. If the user introduces wrong data in a form that won't be the cause to throw an exception. You should have proper validation, besides exception affects slightly performance. Some exceptions are thrown only at runtime (ArithmeticException, DivideByZeroException,...) and you don't want to catch them. On the other side, you can catch more specific exceptions like:
  • Exception
  • ArgumentException
  • ArgumentNullException
  • ArgumentOutOfRAngeException
  • FormatException
  • InvalidOperationException
  • NotImplementedException
  • NotSupportedException
  • ObjectDisposedException

Custom exceptions

A customized exception should inherit from System.Exception class and it needs at least a parameter less constructor. As a best practice you can add few other constructors:
  • one that takes one string
  • one that takes both a string and a exception
  • one for serialization

By convention you have to add the "Exception" suffix when naming your exception class. The serialization attribute ([Serializable]) is important to work correctly across application domains.

Day 3 Programming in C# 70-483

Index

  • Introduction
  • Delegates
  • Lambda expressions
  • Events and callbacks
  • References

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.

Delegates

Basically, delegates are pointers to a function, they define the signature of a method and helps you to abstract the call to a function from a class. This avoid tight coupling between classes. Imagine, you are using a math library by calling its functions from your user interface. If the library changes its implementation you will end changing you interface too but this shouldn't affect you at all. Then is when delegates are useful by defining an abstract place where you can make your math calls and not being affected by any change in the library (loose coupling).

If you know how pointers work in C++ (no type safe), delegates are similar for .Net and also are type safe. See an example of how to DECLARE a new delegate, CREATE an instance of it, POINT the delegate to a particular function and INVOKE that function using the delegate:


Now I'd like to explain you the main features for delegates:

  • Instantiation
  • Multicasting
  • Delegate family (MulticastDelegate)
  • Covariance
  • Contravariance


See how the "delegate" reserved word is used here to tell the compiler the type of what we're creating. Since C# 2.0 you can instantiate delegates and pass them around, even send it to a particular method.

Other feature of delegates is multicasting: by using "+" and "+=" you can add other methods to the invocation list of an existing delegate instance. It'd look something like this:


Delegate d = Method1; 
d += Method2;

You can also end removing method from the invocation list using "-" or "-=". All this is possible because delegates inherit from System.MulticastDelegate which in turn, inherits from System.Delegate. For example, you might want to see how many methods a multicast delegate is going to call by using: 


int invocationCount = del.GetInvocationList().GetLength(0);

Finally, when you assign a method to a delegate, the method does not have to match the delegate exactly. This is called covariance and contravariance; both enable implicit reference conversion for arrays, delegates and generic types.

Covariance allows a method to return (Func) a type more derived than its hooked delegate. Contravariance permits a method that has a parameter (Action) types less derived than those in the delegate type.

See this covariance example where both StreamWriter and StringWriter inherit from TextWriter so the delegate will understand the returning type of the methods we hook in the example:


In the next example, you can see how contravariance works in a kind of similar way. This time the more derived class is in the delegate signature and because the method can work with TextWriter, should also know how to work with a StreamWriter:


Lambda expressions

This expressions, also called anonymous functions, are really handy when you need to create small functionality because you don't need to specify the entire method structure. You don't need to specify the return type or even the parameters type, see:



Sometimes, declaring the delegate as we did in our example can look cumbersome. We might want to replace it using Func<T, Result> types which represents delegates that returns a value. It's provided by the System namespace and take from 0 to 16 params. We declare a Func<int, int, int> where the first two integers represent the input parameters and the third one represents the result type:



On the other hand, if you want a delegate which doesn't return anything, you might want to use the System.Action type which also takes from 0 to 16 params:



All of this get more complicated when, for instance, lambda expressions refers to variables outside of the lambda expression. Normally, when the control leaves the scope of a variable, this is no longer valid. But if a delegate refers to a local variable and is returned to the calling method, the delegate has a longer life that the variable. In this cases, .Net makes those variables alive as the longest living delegate. It's called closure.

Practically, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created. It can use local variables of the method where was created, even if the method is finished. It's implemented by anonymous methods (lambda exp) like here:



See how the counter is created outside the delegate and then we return that delegate with the counter inside. The compiler needs to give a longer life to the variable, that's why this is called closure.

Events and callbacks

Events are a popular design pattern you can subscribe to, then you are notified when the publisher raises a new event (loose coupling). Delegates are the base of events. See here below how to instantiate a class and subscribe to its OnChange property which is raised only (check the if statement) when someone has subscribed to it through the Action delegate:



There is a weakness in the previous algorithm and it's because every user of the class can raise the event to all subscribers. To overcome this weakness we have in C# the event keyword. By using the event type we'll be solving:

  1. No longer a public property but a public field (compare with the previous example). This looks like a step back but the compiler will protect the event from unwanted access.
  2. The event cannot be assigned the same way as a delegate ( = / +=) which prevents anyone removing previous subscriptions.
  3. No outside users can raise the event. Only code that's part of the class defined the event.
  4. null check can be removed because the event is initialized.
  5. To match the .Net convention we need to replace the Action type and use an EventHandler or EventHandler<T> type 
Bear in mind the EventHandler takes a sender object (who raised the event. Null if a static method raised it) and some events arguments. This is how the code would look like after all these modifications:


See how we've customized the EventArgs so subscribers are required to pass an instance of the class. We can customize addition and removal of subscribers (aka custom event accessor). It looks like a property with a get and set accessor, but instead of "get / set" you use "add / remove". Important the "lock" keyword to make the access to the event thread safe, see how:


Think about events like a wrapper around delegates. Delegates are executed in a sequential order, generally, in order they were added. This take us to the next step: handling exceptions.

For example, if you subscribe three methods and the second one raises an Exception, you will probably get the result of the first method, while the third one won't be executed. If this is not the behaviour expected you can customize by using GetInvocationList() method and retrieving the subscribers and enumerating them manually, ie:


In the previous example we've customized how exceptions are managed so we always run any raised event but we've also logged if an exception happened.

I know, I know, today was a long post for a long day but if you understand how lambda expressions, delegates and events work together you will find yourself in a really good position to start making good and loose coupled applications ;) See ya!

Uncle Bob conference

Clean Code

Are you serving your own convenience? 


And this is all about for Robert C. Martin, writing clean code is all about thinking on other developers and thinking on you. Try to give the best every time you write/read code so you or other people won't waste time decoding what you tried to do.

Some suggested books during the conference and what I think are a MUST in your personal library:

  • Michael Feathers: Working effectively with legacy code
  • Design patterns
  • Refactoring Martin Fowler


To quote some interesting points during the conference I enclose here below some bullets I hope you find useful and remember comments are always welcome:
  1. Do not use comments unless is absolutly necessary. Comments are used to explain bad code.
  2. Name functions properly. Don't use different levels of abstraction in the same function. 
  3. Number of lines in a function? Don't pass bool into a function. Don't use switch: breaks IoC. Functions like "is..." within if statements are polite
  4. Use meaningful variable names which they'll help you/others to read your code without scrolling back & forward
  5. Hungarian notation. Don't use I for interfaces maybe use the suffix "Imp" but for the implementation.
  6. Good production systems have test systems (pieces of code only used for testing)
  7. Learn languages every 6 months / year and build your own projects: chess games
  8. Kata / Hackathons / Codetrips are always really useful to boost your coding skills.
  9. "Contest" for Java (Concurrency)
That's all for today, I hope you find it interesting, reagards.


Day 2 Programming in C# 70-483

Index


  • Introduction
  • Using the Parallel class
  • Using async and await
  • Using the Parallel Language Integrated Query (PLINQ)
  • Using concurrent collections

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.

Using the Parallel class

System.Threading.Tasks contains also another class we have not checked yet in previous posts. The Parallel class has a couple of static methods (For, ForEach and Invoke). This doesn't mean you should replace all your loops with parallel loops.

Use the parallel class only when your code doesn't have to be executed sequentially. When you have a lot of work to be done that can be executed in parallel. For smaller work sets or for work that has to synchronize access to resources, using the Parallel class can hurt performance.

In the following example you can see how to use the method For and ForEach when you want to run parallel operations without sharing resources. The third block of code is using the ParallelLoopResult to show you how to break a running operation when the loop completes the first half of iterations. At that point, the IsCompleted value equals to false and the LowestBreakIteration is 500.


Using async and await

When accessing I/O components on the primary thread, Windows typically, pauses your thread in order to release the CPU. Asynchronous solves this limitation but it has some complexity when writing code. For this reason, Microsoft introduced async and await operators when C#5 was released improving responsiveness and scalability.

A method marked with async starts on the current thread and it contains calls marked with await, which are our parallel tasks. Using the await keyword, the compiler can be aware if an operation has finished (continuing synchronously) or not (hooking up a continuation method that should run when the Task completes.

In the following example you get the taste of how this great tool works. Note: you probably need to import the System.Net.Http dll into your project if your IDE can't resolve the HttpClient class dependency



See how a separated method is decorated with the "async" keyword because the entry point for an app can't be marked as async.

Keep in mind, you are not making your app performing any better just by using tasks and awaiting them. You're improving responsiveness.

Normally, when an exception happens you expect an AggregateException to be triggered.

Another relevant point is the SynchronizationContext which connects its application model to its threading model. It abstracts how different applications work and helps you end up on the right thread working with a WPF app or ASP... This means, for example in an ASP app, tasks will find the right user related information (current user, culture,...) when get finished.

In some cases you don't need this data and could run an asynchronous task without the context. Calling the method ConfigureAwait(false) you disable the context saving process and you get better performance. See how in the following example we use a couple of awaits without saving the context. We are getting some text from internet and placing those lines in a text file.



With async/await you should keep in mind:

  1. Never return void within an async method. Return void is only valid for asynchronous events.
  2. Never marked a method as async without any await call decorated.

Using the Parallel Language Integrated Query (PLINQ)

As you are probably aware, LINQ (Language –Integrated Query) is a popular syntax used to perform queries over all kind of data. PLINQ is the parallel version and lets you run extended methods like for example: Where, Selet, SelectMany, GroupBy, Join, OrderBy, Skip and Take.

It's the runtime who decided weather to run in parallel or not. You can force to run in parallel by calling: WithExecutionMode() method. Behind the scenes the CLR will generate different Tasks objects to perform the asynchronous process.

By default, PLINQ uses all the available processor in the CPU (up to 64) but you can always limit this by calling: WithDegreeOfParallelism() method, and passing an integer which represents the number of processors to be used. Note, this parallelism does not guarantee any particular order, see following example:


There is also a way to get this results ordered by calling AsOrdered() function, which still perform a parallel action but buffering the results from the different tasks.

In some cases you might want to perform a sequential operation after a parallel one. By calling AsSequential(), PLINQ will return the answer as non parallel. In the following example we are using Take() and it might mess up the result unless we call AsSequential() first:


On the other hand, if you don't mind get not ordered results you might want to perform a ForAll() function call: parallelResult.ForAll(e => Console.WriteLine(e)); It does not need all the results before starting, but, it will remove any sort order specified. In some cases, you can end up with exceptions in your queries which will be handled using AggregateException. This will show a list of all exceptions triggered during the parallel execution.

Using concurrent collections

Asynchronous access to data always need to be concurrent safe, it means, more than one thread can modify the same data at the same time. There are some collections in .Net developers use to ensure concurrency is safe used like:

  • BlockingCollection<T>
Thread-safe entity handles access to its information. In reality it's a wrapper around a collection, by default, the ConcurrentQueue. For this example, notice how there is one task which listens for new items being added to the collection blocking if no items are available, while other task adds items to it.


You could call the CompleteAdding() method to signal that no more items will be added so other threads waiting for items won't be blocked any more. We can improve the algorithm by using GetConsumingEnumerable() method which returns an Enumerable object that blocks until it finds a new item. With this you can use a foreach with your BlockingCollection to enumerate it.

  • ConcurrentBag<T>
It is, effectively, just a bag which allow you to perform Add, TryTake and TryPeek with duplicates an no particular order. Sometimes I found TryPeek not very useful because you can end up with another tread deleting the item before you actually access it. ConcurrentBag implements IEnumerable<T> so you can iterate over an snapshot of the collection. If new items are added after you start iterate they won't be visible until you start a new iteration.
  • ConcurrentDictionary<TKey, T>
It stores key and value in a thread-safe manner, you can add, remove and update (if exist) items as atomic operations (a single operation will be performed without other threads interfering). TryUpdate() will check first if the current value is different from the new one. AddOrUpdate() will push a new item to the Dictionary if it doesn't exists. GetOrAdd() gets the current value of an item if it's available, if not, it will add the new value.
  • ConcurrentQueue<T>
  • ConcurrentStack<T>
Stack is a LIFO collection and a Queue is a FIFO collection. ConcurrentStack implements Push() (save item) and TryPop() (get item) methods, unfortunately you can never be sure whether are items within the queue due to several threads running in parallel. You can also add or remove bulk items with: PushRange() and TryPopRange().

ConcurrentQueue provides Enqueue (push) and TryDequeue (get) to add and remove items from the queue. It also contains a TryPeek() and it implements the IEnumerable class by making a snapshot of the data.


References

https://www.microsoftpressstore.com/store/exam-ref-70-483-programming-in-c-sharp-9780735676824
https://github.com/tribet84/MCSD/tree/master/mcsd

Progressive .Net tutorials conference - last day

Today is last day for .Net conferences and here are some notes I took along the different talks. I hope you get something from it...

Why I'm not leaving .Net

Mark Rendle

Historical overview about the history of  .Net
c# name comes from c++++, taking all those pluses together and building the sharp symbol

c#6: string interpolation / asp.net dnx (shell: run clr on linux), (lot of stuff)
c#7: algebraic data-types / pattern matching / real tuples

ASP.NET5: How to get your cheese back

Tugberk Ugurlu

Cross platform development is one of the most important features revealed in the last BUILD conference from Microsoft.

In this new ASP .Net version you need to know exactly what are you doing when you deploy your web solutions because you have to create your Pipeline in IIS: all modules needed, configuration and so...

Compilation in memory is a new feature which makes the compilation process more fast. You will be able to hook directly with the compilation process. Even sharing my new compilation steps with other projects.

Dependency all the way down (customize with your own DI engine)

Change in the .csproj to .json project definition file


Going further with ASP.NET5

Tugberk Ugurlu

dnvm list: show all the frameworks installed and which is active
dnvm use "fr name": change active framework for current thread (cmd)
dnvm use "fr name" -p: same before but persist (every thread)
dnvm use "fr name"-r coreclr: using the coreclr
dnvm install "fr name" -u: last unstable version of a framework

dnu development utility: used for building publishing, restoring pckgs
where dnu: dnu runtime version tied

.json:
 - share: allows you to share wathever you want within your project. You'll be ending sharing .cs files with other people instead of the old nuget dll files.

Knowing what went bump in Production - modern monitoring in .Net

Chris O'Dell and Peter Mounce (JustEat)

Set of tools they work with to monitor in advance problems in production:

  • Kibana
  • Logstash
  • Grafana

Introduction to RabbitMQ & EasyNetQ

Mike Hadlow @mikehadlow (blog: coderant.com)

Why messaging?

  • Robust messaging for applications
  • Easy to use
  • Runs on all major operating systems
  • Supports a huge number of developer platforms
  • Open source and commercially supported


RabitMQ

  • Message Broker
  • Single point of management
  • Erlang (clustering ootb)
  • Multi-platform
  • Owned by Pivotal
  • AMQP
  • Client libs for most languages


AMQP Exchange-Binding-Queuing
tryrabbitmq.com
Exchange / Producer / Queue / Consumer

With RabbitMQ you have to manage:

  • Message patterns
  • Connection management
  • Serialization
  • Channels
  • Threading
  • Error handling strategy


EasyNetQ
Handy framework for .Net to create your messaging solutions with RabbitMQ set underneath. It has several qualities like connection management which will open a new connection with consumers as soon as it goes down.

Progressive .NET Tutorials 2015 (day 3)

Hiya!

Today I'm going to a interesting conference in London about:




Day 3: Day Three

Talks


TrackTrack OneTrack Two
9:00
9:30

Keynote - Why I'm *not* leaving .NET!

Mark Rendle

.net
10:30
10:45

ASP.NET 5: How to Get Your Cheese Back

Tugberk Ugurlu

.net asp.net-5

Reactive-interactive approaches to visualization of F# jobs

Alena Dzenisenka

signalr data-visualization data-science f# .net
11:30
11:45

Going Further with ASP.NET 5

Tugberk Ugurlu

asp.net-5 .net asp.net

Machine learning the F# way

Evelina Gabasova

data-science machine-learning f# .net
12:30
13:30
2

Knowing what went bump in Production - modern monitoring in .net

Chris O'Dell and Peter Mounce

kibana elasticsearch logstash grafana graphite statsd .net

Complete Code Craft

Alastair Smith

object-oriented-design story-mapping refactoring tdd .net software-craftsmanship
14:15
14:30

Introduction to RabbitMQ & EasyNetQ

Mike Hadlow

messaging rabbitmq .net

The Joy of REPL

Richard Dalton

testing repl .net
15:15
15:30

Visual Studio & .NET on OS-X, Linux, and Windows

Adron Hall

windows linux os-x visual-studio cross-platform .net

Monoliths to Microservices: A Journey

Sam Elamin

microservices .net
16:15
17:00

Don't hesitate to comment here below if you want more detail in one or more of the subjects here!
Cheers

https://skillsmatter.com/conferences/6859-progressive-dotnet-2015#program