WHAT'S NEW?
Loading...
Showing posts with label MCSD. Show all posts
Showing posts with label MCSD. Show all posts

You can do it!


That's it, after all the preparation, I took the exam and I got a shiny 900/1000 on "Programming in C#" Microsoft Certification exam. Not bad, I know :) Very proud!

I just wanted to share this with you after all the posts I've been writing here. You can do it, if you are motivated and go over the topics on the Microsoft site, read the official book, understand the code samples and do some preparation on questions from previous exams (not sure what the guys from M think about this last one...).


Index


  • Introduction
  • Arrays
  • Generics
  • Lists
  • Dictionary
  • Using sets
  • Queues and stacks
  • Choose your collection
  • 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.

Arrays

This is the most basic data structure:
  • its size is specified during the creation process. (restrictive)
  • zero-based
  • it implements IEnumerable so you can use a foreach loop on it
  • Dimensions:
    • single dimension
    • multidimensional
    • jagged arrays

Generics

Most of the collection structures have both generic (C# 2.0 - System.Collections.Generic namespace) and non-generic (System.Collections namespace) versions. The generic collection is used when you work with objects of one specific type. This improves type safety and performance.

By using a value type as the parameter for a generic collection, you need to make sure that there is no any scenario where boxing could occur. If the value type does not implement IEquatable<T>, you object needs boxing to call Object.Equals(Object) to check equality.

Lists

This structure is similar to arrays but with some enhancements:
  • You can add additional items.
  • It implements IList and ICollection which allows you to call the following methods:
    • IndexOf()
    • Insert(int index, T item)
    • RemoveAt(int index)
    • Count()
    • Add(T item)
    • Clear()
    • Contains(T item)
    • CopyTo(T[] array, int arrayIndex)
    • Remove(T item)
  • Duplicates
  • Fast

Dictionary

It stores items and retrieve them by key:
  • Dictionary<TKey,TValue>
  • Don't duplicate keys
  • Two types, one for key and one for value
  • Fast O(1)
  • The hash value of a key shouldn't change and can't be null.
  • The value can be null (reference type)

Using sets

In Java exists a type called "set". In C# that's a reserved word but you can use the class HashSet<T>. This is a particular collection with no duplicates and no particular order. You can add new object to the structure and iterate over them.


Queues and stacks

Queue features:
  • FIFO (first in, first out)
  • By getting an item you remove it from the Queue
  • Typically used for messaging systems
  • Methods:
    • Enqueue(): adds and element
    • Dequeue(): removes the oldest element
    • Peek(): returns the oldes element but doesn't immediately remove it from the queue.
Stack features:
  • LIFO (last in, first out)
  • By getting an item you remove it from the stack
  • Methods:
    • Push(): adds a new item to the stack
    • Pop(): gets the newest item form the stack
    • Peek(): gets the newest item without removing it

Choose your collection

List and Dictionary offer random access. Dictionary offers faster read features, but it can't store duplicates.

Queue and Stack are used when you want to retrive items ina specific order. 

Set-based collections don't offer random access to individual items.

List is used more commonly but it worth to work out which collection fits better with our requirements.

Index


  • Introduction
  • XML serializer
  • Binary serializer
  • DataContract serializer
  • JSON serializer
  • 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.

XML serializer

Serialization is about transformation in order to send information. When you want to send data to a web service first you transform that data to a flat or binary form, send it and then you transform back that data. When you want to define what data you want to serialize, typically a DTO (Data transfer object) is created where you define all the properties you want to send.

Features:
  • XmlSerializer class
  • Simple Object Access Protocol (SOAP)
  • Not the best performance
  • Can't do private fields
  • Decoration:
    • class: [Serializable]
    • properties: by default members are serialized as XmlElement, ie, as nodes.
      • [XmlIgnore]: not serialize
      • [XmlAttribute]
      • [XmlElement]
      • [XmlArray]: for collections
      • [XmlArrayItem]: for collections

Binary serializer

This type of serializer is used more commonly with images and non-readable data. 

Features:
  • BinarySerializer class
  • namespaces: System.Runtime.Serialization and System.Runtime.Serialization.Formatters.Binary
  • Private fields are serialized by default
  • Decoration:
    • class: [Serializable]
    • properties:
      • [NonSerialized]
    • methods:
      • [OnDeserializedAttribute]
      • [OnDeserializingAttribute]
      • [OnSerializedAttribute]
      • [OnSerializingAttribute]
  • ISerializable: by implementing this you can define what is serialized, and if you have sensitive data you can encrypt prior to serialization. Implement:
    • GetObjectData(): called when your object is serialized. You can decorate it with a SecurityPermission attribute, so it is allowed to serialize and deserialize
    • protected constructor: called when you object is deserialized. You use it to retrive the values and build your object by defining 

DataContract serializer

Features:
  • DataContractSerializer class
  • It's used by WCF to serialize to XML or JSON
  • Decoration:
    • class: [DataContract]
    • operations: [DataMember]
    • methods:
      • [OnDeserializedAttribute]
      • [OnDeserializingAttribute]
      • [OnSerializedAttribute]
      • [OnSerializingAttribute]

JSON serializer

Features:
  • DataContractJsonSerializer class
  • small amounts of data
  • It uses Asynchronous JavaScript and XML (AJAX)

Index


  • Introduction
  • Linq features
  • Linq queries
  • Linq under the hood
  • Linq to XML
  • 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.

Linq features

Linq aka Language-Integrated Query was introduced with C# v3.5 and it's a widely language used to query data. There are some features in the language that make Linq possible:
  • Implicitly typed variables: this happens whenever you define you variables using the keyword "var" instead of doing an explicit type definition. This is necessary using Linq because sometimes you can't specify the return explicitly because you don't know it. It makes you code more readable as well.
  • Object initialization syntax: it enables you to combine creating a new object and setting its properties in one statement and this is required when working with anonymous types.
  • Lambda expressions: before lambda expressions, it's necessary to have clear what anonymous methods are (delegates). They were introduced in C# v2.0 to create, assign and pass methods in one line. 
    • .Net introduced shorthand 
      • Func<T,T> and 
      • Action<...> (returns nothing) to simplify the creation process. 
    • Lambdas use the => notation which can be read as "becomes".
    • Linq often has to pass a Func<> delegate to a method.
  • Extension methods: Extend an existing type with new behavior without using inheritance. It uses the special "this" keyword to mark itself as an extension method. Linq is entirely based on extension methods.
  • Anonymous types: this is a mix between implicit typing and object initializes. You create an anonymous type by using "var" and the new operator without specifying a type. This is used in Linq when you create a projection, so you select certain properties from a query and form a specific type.

Linq queries

There are two ways to query data with Linq: Query syntax or Method syntax, ie:


Linq query operators: these are the different actions we can perform over our data and that we know is implemented in other providers like Linq to XML, Linq to entities, Linq to Objects. Here are some: All, Any, Average, GroupBy, Join (it's used to combine data from two or more sources by specifying the property that needs to be equal), Max, Min, OrderBy, Select, Skip, Take (skip/take are used to add pagination, ie: when you call a db and you want to split the answer in pages so performance is not affected), Where...

Other useful features are projection (select) and grouping (groupBy). Projection is used to select another type or an anonymous type as the result of your query. Grouping, you group your data by a certain property and then work with that result.

If you want to test your queries there's a nice tool called linqpad which can help you to create a playground where do your experiments. 

Linq under the hood


As you already know how extension methods is used in Linq you might want to change the behavior of one of the operations provided. Then you have to remove the using System.Linq statement and try with the following approach:


Here you'll see how an iterator pattern is implemented. By using "this" we are extending the behavior of our method. The "yield" parameter is used to return values but one at a time. This is called "deferred execution".

This is important when you work with other Linq providers like Linq to entities or linq to SQL (it parses the query and transform it to SQL). The query won't be sent to the database until the result is iterated over. Iterating can happen when you call ToList() or when you iterate over the results in a for each statement (which calls the MoveNext() method).

Linq to XML

In previous lessons we've already talked about how to access XML files. Here I want to show you another approach. XDocument class is used to load a file, it works with the XNode abstract class. You can also use XDocument.Descendants or XDocument.Elements as well as XDocument.Nodes to access the child of a node. All those elements grant you access to the attributes within a node.

It also allows you to create your own XML. By calling the Add() method you can start building your file or in another way, you can use the XElement constructor that takes an array of objects that form the content.

Finally, in order to update your XML files you have two approaches:
  1. procedural way: it basically consists on building your objects one by one and then attach them to the xml structure.
  2. functional construction: it consists on using LINQ to XML syntax to find the bits you want to modify and update them with the new values.
It depends how difficult is your transformation whether you use functional or procedural transformations, If the XML structure changes the functional has a lot more benefits.

Index


  • Introduction
  • Databases
  • XML
  • JSON
  • Web services
  • 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.

Databases


Applications need a persist storage system to save data, this can be done in a database using ADO.NET or the Entity Framework. You can also use a web service and retrieve a response in JavaScript Object Notation (JSON) or Extensible Markup Language (XML). All the .Net functionalities to work with databases is stored within the System.Data namespace and it consists on two different approaches: 
  • connected: by running queries by using Structured Query Language (SQL) to create, read, update, and delete data (known as CRUD operations)
  • or disconnected data: you'll use DataSets and DataTables to mimic the database structures. Any change made can be sent back to the data store by using a DataReader.
Providers:
  1. Microsoft
  2. SQL
  3. Oracle
  4. MySQL
Connecting: we always need to define some connection (DbConnection class) details using a connectionString and providing the database type, the location and the credentials to log in. Connections are used within a using statement, IDisposable is implemented. Typically this strings are stored in a config file (app.config or web.config) but you can build your dynamically by using the DbConnectionStringBuilder class: OracleConnectionStringBuilder, SQLConnectionStringBuilder... Hard-code this is a bad practice, as mentioned before you can save them in a config file and retrieve them by calling the ConfigurationManager class like here:


Connecting is a time consuming operation and leave a connection open can prevent other users to access the storage, instead you can use the connection pool to save some resources and time.

Selection: when running a particular query against a database you can use the SqlCommand class which returns a SqlDataReader which keeps track of where you are in the result set. Asyn/Await is supported as well. SqlDataReader is a forward-only stream. You can't go back while you're reading, you can access the columns by index and by name by calling:

  • GetInt32(int index)
  • GetGuid(int index)
  • GetString(int index)
You can even batch multiple operations together and in result the SqlDataReader will return multiple result sets. Then you can move over them by calling NextResult() or NextResultAsync().

Update: here when you change something in a database you don't get a set with the information affected by your query, instead you end up with an integer which represents the amount of elements that have been modified. Run a ExecuteNonQuery() or ExecuteNonQueryAsync() in a command.

Parameters: typically you use parameters in your queries when filtering your selections or when updating data. Never hook your user interface components with you queries as this is a potential SQL injection attack candidate. Instead, use parameterized SQL which produces a more generic query and is easier to precompile an execution plan producing a more secure and better performance result.

Transactions: (ACID) key properties:
  1. Atomicity: if one fails, they all fail (rollback).
  2. Consistency: from one valid state to another.
  3. Isolation: Multiple concurrent transactions won't influence each other.
  4. Durability: committed transactions result is always stored permanently.
If nothing goes wrong you call TransactionScope.Complete() within a using statement. Transactions can be created using three options:

  1. Required: Join the ambient transaction or create a new one if it doesn't exist.
  2. RequiresNew: Start a new transaction.
  3. Suppress: Don't take part in any transaction.
The .Net framework manages transactions for you. If the transaction uses nested connections, multiple databases or multiple resources it will be promoted to a distributed transaction (avoid if possible).

ORM (Object Relational Mapper): you can manually write your SQL statements but if your app grows it end up being a nightmare to maintain or improve. Here is when ORMs like Entity Framework come handy generating for you all those queries. 

It provides three different approaches:
  1. Database First
  2. Model First: typically use a graphical tool
  3. Code First: you need to define all in code, your entities, relations and so... inherit 
    1. DbContext is used to create your own context, which is the interface between your code and the database. 
    2. By adding entities to the context you'll end up creating new rows and to commit that calling the SaveChanges() method. 
    3. Conventions are applied like the Id property is a primary key and more...

XML

It's a document formatted to be readable both by humans and computers. First line is an optional line which is called prolog and tells you are reading to an xml file and the enconding used, it typically looks like this:

<?xml version="1.0" encoding="UTF-8" ?>

Now we'll take a look at some .Net classes to help us work with this type of files:

  • XmlReader: read xml files in hierarchical manner and only forward without cached
    • Create(): static. Param XmlReaderSettings to configure how to read and skip some data.
  • XmlWriter: write xml files only forward without cached.
    • Create(): static. Param XmlWritterSettings.
  • XmlDocument: navigate and edit for smaller documents as it's slower than XmlReader/Writer. After editing you can save. It uses XmlNode to move through your document and perform changes of attributes in nodes. A nifty way to navigate is by using XPath (query language for xml). XPathNvigator class offers an easy way to navigate through an XML document.
  • XPathNavigator: navigating through an XML
See an example here of how XML looks like:

<employees>
    <employee>
        <firstName>John</firstName> <lastName>Doe</lastName>
    </employee>
    <employee>
        <firstName>Anna</firstName> <lastName>Smith</lastName>
    </employee>
    <employee>
        <firstName>Peter</firstName> <lastName>Jones</lastName>
    </employee>
</employees>

JSON

Lighter version of XML is JSON or JavaScript Object Notation, this contains less rules and therefor is why is light. The .Net library Newtonsoft.Json available at http://json.codeplex.com/ offers some functionalities to work with this format. Typically is used with asynchronous calls between web sites and servers or alson known as AJAX (Asynchronous JavaScript and XML, in reality XML was replaced by JSON so AJAJ is more accurate).

See here below the previous XML example this time using JSON

{"employees":[
    {"firstName":"John""lastName":"Doe"},
    {"firstName":"Anna""lastName":"Smith"},
    {"firstName":"Peter""lastName":"Jones"}
]}


Web services

Web services and micro web services turned in loosely coupled solutions for those who want to integrate to splitted systems. .Net offers the Windows Communication Framework (WCF) to build you own services. You will build a class with all the logic you service offers and it will be decorated with the attribute: ServiceContract close to the class name. Bear in mind you have to add System.ServiceModel reference into your project. For each of the methods exposed you will use the OperationContract decorator. Example:



Every WCF service has the following ABC properties:

  • Address: defines the endpoint. This is the URL you have to tackle to open the connection.
  • Bindings: it configures the protocols and transports that can be used to call your service: HTTP, HTTPS, named-pipe connections...
  • Contract: this defines which are the operations your web service exposes.
If the web service is already created, VS offers you a handy way to add a reference into your project which will help you to map all the methods exposed. Behind the scenes, the proxy uses the configuration file (app.config, web.config) to get the ABC settings.

Index


  • Introduction
  • Files directories and drives
  • Streams
  • Network
  • Async IO streams
  • 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.

Files directories and drives


Working with the System.IO namespace you'll find a lot of classes related with files, paths, drives, folders. See the main classes in this list:
  1. File
    1. Exists(string path)
    2. Delete(string path)
    3. Move(string source, string destination)
    4. Copy(path, destPath)
  2. FileInfo
    1. Exists: property
    2. Delete()
    3. MoveTo(string destination)
    4. CopyTo(destPath)
  3. Path static class within the System.IO namespace
    1. Combine(folder, fileName): returns a string with the full path for that file.
    2. GetDirectoryName(path)
    3. GetExtension(path)
    4. GetFileName(path)
    5. GetPathRoot(path)
  4. DriveInfo: it enumerates the current drives and display some information about them
    1. GetDrives()
    2. Name / DriveType / VolumeLabel / DriveFormat
  5. Directory
    1. CreateDirectory()
    2. GetFiles(string path)
  6. DirectoryInfo (multiple operations against a folder)
    1. Create()
    2. GetDirectories(): searchPattern can be passed as a parameter to reduce the amount of directories retrieved.
    3. EnumerateDirectories(): with GetDirectories you get a list and you need to wait until is completely retrieved but with this you can start enumerating the directories before been completly retrieved.
    4. MoveTo()
    5. Move()
    6. GetFiles();
  7. SearchOptoin: specify a search pattern to be performed agains a Directory/DirectoryInfo object
    1. wildcards: * stands for any group of characters, ? stands for any single character.
  8. DirectorySecurity: grant access to folders, hosted in System.Security.AccessControl namespace.

Insufficient privileges creating a folder can end up throwing a UnathorizedAccessException and trying to remove a folder that doesn't exists in a DirectoryNotFoundException.

Streams


Streams refers to abstractions of a sequence of bytes, which means, if you want to save text first you need to transform that text into an array of bytes. Types of streams:
  • Reading
  • Writing: when writing to a FileStream
    • Write(byte array)
  • Seeking: stands for some streams have the concept of a current position.
To convert between bytes and strings we use the System.Text.Enconding class, it uses different standards to make the transformation like:
  • UTF-8
  • ASCII
  • BigEndianUnicode
  • Unicode
  • UTF32
  • UTF7
To follow this process in an easier way, the File class supports a CreateText() method which returns a StreamWriter object that we can use to perform write operations directly to a file. To read the data you can use a FileStream object, you would need to read every byte one by one by using the ReadByte() method and finally, translate those bytes using a particular encoding like UTF8 calling the method: Encoding.UTF8.GetString(data) which returns a string. The alternative, if you know you are reading a piece of text, is the StreamReader and call the method ReadLine(). See example below:

using System.IO; using System.Text; namespace Chapter4 { public static class CompareFileStreamReader { public static void HowToReadAFile() { string path = @"c:\temp\test.txt"; // read a file using a FileStream object using (FileStream fileStream = File.OpenRead(path)) { byte[] data = new byte[fileStream.Length]; for (int index = 0; index < fileStream.Length; index++) { data[index] = (byte)fileStream.ReadByte(); } System.Console.WriteLine(Encoding.UTF8.GetString(data)); } // read a file using a StreamReader object using(StreamReader reader = File.OpenText(path)) { System.Console.WriteLine(reader.ReadLine()); } } } }

Working with multiple streams together (decorator pattern) can be handy sometimes, specially when compressing/decompressing streams (FileStream, MemoryStream,...) using the GZipStream class within the System.IO.Compression namespace.

Another interesting class is BufferedStream, which comes handy when you need to read/write big chunks of data (which is what drives are optimized for, instead of reading byte by byte). It wraps a FileStream object and works out if it's possible to read larger chunks of data at once.

To finish this chapter about streams, I want to highlight how files can be locked by the OS and you might think the file was deleted. For example a File.Exists call can return false and we assume there's no such a file, while in reality, the file is in use by another thread. This is important to consider when writing/reading resources and always wrap our code up with try/catch blocks. Typically we want to focus on DirectoryNotFoundException and FileNotFoundException.

Network


The .Net framework enable your apps to communicate over the network. It's then when the System.Net namespace turns interesting and the WebRequest and WebResponse classes vital. There are specific implementations by protocol like HttpWebRequest and HttpWebResponse based on the protocol HTTP. See an usage example here:


Async IO streams


All the classes showed in this post until now are synchronous classes. Working with resources, typically, implies some delays we need to take in consideration. If not we can make our apps looks unresponsive (it happens when an app looks freezes and the user can't perform any action). 

C#5 introduced async/await keywords, this will make the compiler responsible for turning your "synchronous-looking" code into a state machine that handles all possible situations. Many methods have an async equivalent that returns Task or Task<T>, if there's nothing in return or if the method returns a type (T), respectively. The use of this async methods will make your apps more responsible and scalable

Real async makes sure your thread can do other work until the OS notifies the I/O is ready. Bear in mind not to use the await operator multiple times within the same function if the async calls can be run in parallel. The following example shows you how you need to call the await operator at the end of your functions to just wait the minimum possible time, in this case, wait only for the longest call, instead of for each individual.