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

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
  • 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
  • Managing integrity
  • Using parsers
  • Regular expressions for validation
  • Validating JSON and 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.

Managing integrity


The data used by your applications is really important and manage it properly will make your users feel they can rely on your tools. We are humans so we can make mistakes when using some particular web or desktop application. It's on our developer condition where we find the debt to handle this data accordingly to its type, so we can provide the best UX. In case your application crashes for some wrong data introduced by the user it's something you have to avoid but it's not the worst thing you can get. Imagine you work with a Live system used by thousands of users and you don't manage some particular information properly, and even that, your app doesn't crash. Then, that information will end up in you database as corrupt information.

Here is when data integrity has a main role. There are four different types of data integrity:
  1. Entity integrity: this refers to the entities saved within a database. Those need to be identified uniquely. This is achieved by using a primary key column which can be auto generated by the database or by the application.
  2. Domain integrity: this one stands for the information saved within an entity. Imagine you are saving the post code in  one of your entities. You need to ensure that the postcode field only saves real postcodes.
  3. Referential integrity: this one stands for the relation between entities. Imagine the relation between a table Employee and a table called Payroll.
  4. User-defined integrity: this refers to particular rules of your business like the number of orders per day your customers can create in the system.
Working with Entity Framework you can define different "conditions" within your entities by decorating its properties with the following attributes (System.ComponentModel.DataAnnotations.dll):
  • DataTypeAttribute: it helps you define which type of data your application will expect to be introduced by the user. This way, if you don't provide a valid email address within the email text field, you'll get an error message after validation is made.
  • RangeAttribute
  • RegularExpressionAttribute: for some special fields there are no standard validation rules like for dates, emails, strings... then you have to define you own ones.
  • RequiredAttribute: very common to have required fields in a web form.
  • StringLengthAttribute
  • CustomValidationAttribute: you can create your entire validation attribute.
  • MaxLengthAttribute
  • MinLenthAttribute
Another important data integrity feature in databases is called transactions. It helps you to create a window time when different queries are triggered against your database and if at some point something failed and the process cannot be continued, then you can revert all those changes applied within the transaction in an action called: rollback.

Using parsers

The use of parser if very common in programming. It is mainly used to transform text variables into its real type. For example, a string variable could contain 'true' and by calling bool.Parse(value); you end up with a real boolean type containing true. In case you are not sure if the transformation process will work without errors, there's a method called TryParse which has the same behavior as the Parse() method but additionally returns a true or false whether the transformation worked or not. This is really helpful in case you're not completely sure if the transformation will work. This method is perfect when you parse user inputs.

In case you want to parse numbers, there are some extra functions you can use to apply some rules during the number transformation process. For example, you can use the CultureInfo class from the System.Globalization namespace to apply rules over currencies regarding the money symbol or the decimal character.

In a similar way you can parse DateTime types and apply rules for different time zones, cultural and so by using the following overload methods:

  • Parse(string)
  • Parse(string, IFormatProvider)
  • Parse(string, IFormatProvider, DateTimeStyles)
The .Net framework offers also the Convert() method to perform transformations between base types (Boolean, Char, Byte, Int, Single, Double, Decimal, DateTime, String...). The only difference between Parse/TryParse and Convert is that the last one allows null values. Instead of trigger an exception, this method supplies the default value for the supplied type. Parse only takes string types as input, Convert accepts other types as input parameter.

Regular expressions for validation


A regular expression is a pattern used to parse or find matches in strings. They are very flexible and you will find lots of them already written and ready to be used in sites like http://regexlib.com/. You can define your own regexs to match your business rules like post codes, email addresses or phone formats allowed in your site. In the following example we define a regex to match email addresses:

^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$

Really useful when working with user inputs to validate.

Validating JSON and XML


This is a format widely used when communicating separated systems. JSON or JavaScript Object Notation is light weight compared with XML which has a stricter schema. .Net offers a library to de-serialize JSON called JavaScriptSerializer (System.Web.Script.Serialization) and turns it into an object you can access, ie:

var result = serializer.Deserialize<Dictionary<string, object>>(json)

With the previous call you are converting your json object into a Dictionary of <string,object> types. If the json object is invalid you'll get an exception with an "Invalid object passed in" message in. See here below an example of a JSON object:

 {"menu": {
   "id": "file",
   "value": "File",
   "popup": {
     "menuitem": [
       {"value": "New", "onclick": "CreateNewDoc()"},
       {"value": "Open", "onclick": "OpenDoc()"},
       {"value": "Close", "onclick": "CloseDoc()"}
     ]
   }
 }
}

In XML you can describe its content with an XSD (XML Schema Definition) providing a way to validate your XML file, as well as defining some rules to fill your xml up. Visual Studio provides a tool called xsd.exe which you can use to generate xsd files. See the following XSD example:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Address">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Recipient" type="xs:string" />
        <xs:element name="House" type="xs:string" />
        <xs:element name="Street" type="xs:string" />
        <xs:element name="Town" type="xs:string" />
        <xs:element name="County" type="xs:string" minOccurs="0" />
        <xs:element name="PostCode" type="xs:string" />
        <xs:element name="Country" minOccurs="0">
          <xs:simpleType>
            <xs:restriction base="xs:string">
              <xs:enumeration value="IN" />
              <xs:enumeration value="DE" />
              <xs:enumeration value="ES" />
              <xs:enumeration value="UK" />
              <xs:enumeration value="US" />
            </xs:restriction>
          </xs:simpleType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This rules define a way to validate your xml file when you request someone to send his address to your system. You can start playing with the XMLReader and XMLDocument classes in your next .Net application in order to validate your xml files through your xsd files

References

https://github.com/tribet84/MCSD/tree/master/mcsd
http://regexlib.com/
https://es.wikipedia.org/wiki/JSON
https://en.wikipedia.org/wiki/XML_Schema_(W3C)