WHAT'S NEW?
Loading...

Repository pattern

Introduction to Repository pattern


I found this tutorial from Mosh Hamedani pretty good explained. It will give you a very good understanding on how to create a repository pattern in your C# applications.

Learn when to use it (not in small apps, POC,..) and how to implement it properly. Understand DbSet and UnitOfWork, and which functionalities you have to define on them.



Events in C#

Introduction


Today I wanted to create a basic example about how events work in C#. This is a great tool to create loosely coupled applications, removing dependencies and allowing you to change and fix your programs making the least amount of changes.

Events

First of all we need to define three steps that we need to follow when we work with events:

  1. We need to define a delegate with this convention:
    1. add the suffix "EventHandler" in the name of the envent
    2. Parameters: an object which is the source of the event and an EventArgs if we need to pass additional data
  2. Define an event based on that delegate. Note that the signature is defined by the delegate and every subscriber will have to follow that signature. Typically this has its name in past tense as it's something that already happened.
  3. Raise the event.
In the following example I've tried to use the analogy of a regular house with some pets and what they usually do when you get home. I've defined three classes: BreadWinner (you), Cat and Dog. There is an extra class to run the application and subscribe Dog and Cat to the event publisher, in this case, the BreadWinner.


Let's change a little bit my example to pass an argument when my event is raised. This time I've simplified the previous example. See how with my new PapaEventArgs class I can use it to pass content to my subscribers. See changes highlighted.


The final step is to show you how to the same thing but using less code. .Net provides a way to prevent create a delegate every time we want to create an event by using the class EventHandler. In our example we'll use the generic class EventHandler<> as we want to pass PapaEventArgs to our subscribers. Additionally, I've added some exception handling to prevent breaking the flow when a subscriber triggers an exception. See changes highlighted.



The GREAT thing of all of this happens when there is a problem in the subscribers. Imagine this used in a very complex application and an error is raised for a subscriber. We won't need to touch the publisher and we won't need to recompile as is completely de-coupled from the rest of the subscribers.

Day 19 Programming in C# 70-483

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.

Day 18 Programming in C# 70-483

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)

Today I'm in...

Hello everyone,

Today I'll be attending the Amazon Appstore Developer summit event at SkillsMatters London event. It's going to be a busy day with a lot of interesting people talking about the advantages of using the cloud from a developer perspective. Here I enclose the agenda for today and I hope hear from you about these topics



Day 1