December 09, 2016

Linq everything you need to know

 

Linq

Links

·         Linq cheat sheet to print http://nickberardi.com/linq-cheat-sheet/
·         Linq Immediate and Deferred execution (intermediate) http://www.dotnetcurry.com/linq/750/deferred-vs-immediate-query-execution-linq
·          


What is it for? What are the benefits? Do I need it?

Reduces code line numbers.
The Linq query can be passed around as a method parameter, it’s not actually run until the developer calls ToList() or something on the query

Linq is not required but it looks good.

Linq and Lambda

Linq is the queryable language, in C# this is implemented as two different types Extension methods (methods like Where() ) or Query Syntax (sql style).

Lambda is the part with the weird => notation known as “to”.
e.g. The complete expression can be known as a Linq query. Made up of a Linq extension method and a Lambda expression.
var allFirstsQuery = data.Where(d => d.Name == “First”);

From Stackoverflow: http://stackoverflow.com/questions/7391370/is-it-linq-or-lambda


This is LINQ (using query syntax):
var _Results = from item in _List
                where item.Value == 1
                select item;
This is also LINQ (using method syntax):
var _Results = _List.Where(x => x.Value == 1);
It's interesting to note that both of these flavors will end up producing the exact same code. The compiler offers you a service by allowing you to express your wishes in the manner that you prefer.
And this is a lambda:
x => x.Value == 1


How to use it

Replacing existing foreachs with linq.

Different types of Linq Methods

LINQ standard query operators (implemented with extension methods in C#) can be categorized into the following ones on the basis of their functionality. Filtering, Join, Projection, Sorting, Grouping, Conversions, Concatenation, Aggregation, Quantifier, Partition, Generation, Set, Equality, Element.

 

Common examples


More complex examples

Immediate and Delayed execution:

var allFirstsQuery = data.Where(d => d.Name == “First”);
When the runtime get to the line above it does not do anyting except create a variable result.
Only when the result is executed does the query actually run
var allFirstsResultAsList = allFirstsQuery.ToList();
So you could pass allFirstsQuery around as a normal object and execute later. The fact that a query can be created and later executed is important where the target will most likely be different at a later time e.g. the original data object could have a Database StoredProcedure call and the data in there could be different at a later time.


January 06, 2015

Design Patterns - Abstract Factory Pattern

What?
A more special and complex version of the Factory Pattern. The additional complexity is that the not only does the client now know the actual object being created but also does not know the Factory which is creating the object i.e. the Factory now also configurable.

Why?
Used in Dependency Injection. The pattern does not have the actual types that it needs to create but might have some way to create them based on information passed to it like the type name.

How?
In .NET reflection can be used to create instances using type names as strings see Activator.CreateInstance.



http://stackoverflow.com/questions/2280170/why-do-we-need-abstract-factory-design-pattern

February 19, 2014

Encryption and Decryption in .NET

You want to scramble data on the server side before it's saved and also want to retrieve it later and unscramble it.

Encryption and Decryption is what you want.
There are 2 types, Symmetric and Asymmetric.
Symmetric uses 1 key (private) and this same key is used to encrypt and decrypt.
Asymmetric uses 2 keys, 1 for encryption (public) and 1 for decryption (private)

So at least a key is required in all cases. This needs to be stored somewhere. Seems that hardcoded in code or in a config file seems to be the preference.
When storing the key in the web.config ensure that the section is encrypted itself so that it's not human readable http://msdn.microsoft.com/en-us/library/zhhddkxy(v=vs.100).aspx.

.NET has a bunch of classes which can be do the actual work, see the
System.Security.Cryptography namespace. See the SymmetricAlgorithm and the AsymmetricAlgorithm classes. Use the (SymmetricAlgorithm)CryptoConfig.CreateFromName(provider)) and get the provider form the web.config with something like ConfigurationManager.AppSettings["encryption"]


You can create create a key with



Additionally you can add Salt to the encryption, the following case basically adds complexity to the key before the encryption takes place. This makes it more difficult for someone to decrypt if they only have the key.
 
private static string CreateSalt(int size)
        {
            //Generate a cryptographic random number.
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] buff = new byte[size];
            rng.GetBytes(buff);
 
            var asHex = BitConverter.ToString(buff).Replace("-"",0x");
            // Return a Base64 string representation of the random number.
            return Convert.ToBase64String(buff);
        }

The generated key could then be used with some of the .NET code like follows:
private const ushort ITERATIONS = 300;
private static readonly byte[] SALT = new byte[] { 0xBA, 0x72, 0xCA, 0xC6, 0x35, 0xC2, 0x9E, 0xAB, 0x87, 0x0F, 0x58, 0xD4, 0x5F, 0xF0, 0x88, 0x6E, 0xAA, 0x74, 0x9B, 0xE9 };
 
        private static byte[] CreateKey(string password, int keySize)
        {
            DeriveBytes derivedKey = new Rfc2898DeriveBytes(password, SALT, ITERATIONS);
            return derivedKey.GetBytes(keySize >> 3);
        }
 
  public static string DoEncrypt(string plainText, string key, string provider)
  {
   
            using (SymmetricAlgorithm algo = (SymmetricAlgorithmCryptoConfig.CreateFromName(provider))
   {
                var newKey = CreateKey(key, algo.KeySize);
 
                algo.Key = newKey;
    // BlockSize is in bits.
    algo.IV = new byte[algo.BlockSize / 8];
    
    using (ICryptoTransform tx = algo.CreateEncryptor())
    {
     byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
     byte[] cipherBytes = tx.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
     return BytesToHexString(cipherBytes);
    }
   }
  }

February 15, 2013

Closures in C#

Tricky topic to understand, this is is the simplest explanation I've found.

What?
A Closure is a function which can be passed around like an object but it also can maintain state in it's local variables.

Why?
Useful in asynchronous calls where you want something to happen when the task ends, the local variables can be used to store something that should be included in the work that needs to be done when the task completes.
Particularly useful in Javascript.

August 17, 2012

Debugging Windows Exceptions with Adplus V7


A tool called Adplus can be used on Windows to get more information on unexpected exceptions, hangs and crashes. If one of these has been reported on a production machine then run Adplus in the background, monitoring that process, and check from dump files afterwards. The dump files should include information on that happened just before the exception, crash or hang. The dump files can be opened in Visual Studio or Windbg for example and ran as a debugging session.


There is a new version of Adplus available, Adplus V7, it's an exe where the old was a vbs. Adplus comes with the "Debugging Tools for Windows", google this to find the latest version.


First stop
http://www.codeproject.com/Articles/315536/Adplus-Handling-managed-exceptions

To get a dump on an exe use the -sc switch with adplus (ensure this is the last switch on the command line) and point it to the full path of the exe. If adplus is working it will launch the exe in a separate window, if not then it hasn't found the exe.

To get a dump on a website running in IIS use the -iis switch.

For some brief information on the "differences between a mini and a full dump".


  1. Visual Studio 2010 is the most straight forward debugging tool I've seen to use when debugging dumps. Simply file open the dump file.
  2. Set the symbols path to the pdb location if there is one.
  3. Debug with mixed.
An alternative in Windbg, also provided with the Debugging Tools for Windows. This is not as intuitive.
Note that the sos will need to be loaded before debugging .NET code,

  1. Enter the command in the Windbg command box, the same command that you created the dump with e.g. .loadby sos clr.
  2. To show the stacktrace type !CLRStack -a. Scroll to the top of the list to see the last call in the stack, the objects can be seen here.
  3. To view the object type !do <object address>. 



http://windbg.info/doc/1-common-cmds.html



July 13, 2012

Immutable Objects

Immutable means "can not change". When an object is referred to as Immutable it means the internals cannot be changed i.e. it's member variables.

This is a design choice, where you might wish objects to be passed around as method arguments but you do not want the original object's internal values to change.
Remember that in C# when you pass an object of type Reference Type into a method as an argument, you are actually passing it by reference, meaning the method can change the original objects values.
To protect objects  from this make them immutable.

How:
Objects are made immutable by simply making their internal variable readonly.

January 19, 2012

Dependency Injection IoC



"Dependancy Injection" is a more specific name for and term used "Inversion of Control".

It's all about letting client developers of your code add there own implementations. To do this your code needs to be very loosly coupled (i.e. none of the usual newing up of concrete classes, the code must be written using Interfaces for example) and configurable (e.g. the client developer drops their implementation dll into a location and adds content to a XML configuration file to tell your host app what class to load and instantiate from the dll).

This is a good intro to "Dependancy Injection" http://www.martinfowler.com/articles/injection.html

These are intros to Microsofts "Dependancy Injection" framework, Unity.

https://www106.livemeeting.com/cc/wwe_us/view?cn=guest&id=1032382093&pw=23D44136 with sourcecode at http://unity.codeplex.com/wikipage?title=Webcast%20demos&referringTitle=Home

http://visualstudiomagazine.com/articles/2011/01/27/the-factory-pattern-in-net-part-3.aspx

May 11, 2011

XML file for Visual Studio HelpViewer 1.0

The following XML file is used in combination with the XSLT file posted in an earlier post. These 2 files transform into HMTL that views correctly in Visual Studio 2010 HelpViewer 1.0.


<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="../xslt/gravity.xslt"?>
<HelpDocument>
  <Meta>
    <title>Add</title>
    <metaTitle>Add</metaTitle>
    <metaID>Help3-7F3D2D9D-Add</metaID>
    <metaParent>Help3-7F3D2D9D-Entity</metaParent>
    
    <metaKeywords>
      <metaKeyword>
        <keyword>Add</keyword>
      </metaKeyword>
      <metaKeyword>
        <keyword>Add</keyword>
      </metaKeyword>
      <metaKeyword>
        <keyword>AddEntity</keyword>
      </metaKeyword>
    </metaKeywords>
    <metaF1s>
      <metaF1>
        <keyword>Add</keyword>
      </metaF1>
      <metaF1>
        <keyword>AddEntity</keyword>
      </metaF1>
    </metaF1s>
    
  </Meta>
  <HelpBody>
    <majorTitle>Add</majorTitle>
    <title>Add</title>
    <feedbackLink>Add</feedbackLink>
    <summary>
      <summaryText>
       
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
      </summaryText>          </summary>     <syntax>       <codeSnippet>Lorem Ipsum is simply dummy text</codeSnippet>              <example>         <codeSnippetFull> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </codeSnippetFull>
      </example>
    </syntax>
  </HelpBody>
</HelpDocument>



XSL XSLT file for Visual Studio HelpViewer 1.0

This XSL/XSLT file is based on a Visual Studio Help file found in "C:\ProgramData\Microsoft\HelpLibrary\content\Microsoft\store\Development_Frameworks_21823146_VS_100_en-us_9.msha"
I've stripped out the content and replaced with <xsl:if gt;
etc.
The values for this content are taken from XML files which have the corresponding XML element hierachy, e.g

XML to HTML

To convert XML to HTML you need an XSL (XSLT) file and some application to transform the XML to HTML using this XSL file. Usually this work is done by the browser, by including a link to the XSL file in the XML file, when you open the file in a browser it runs the transform on the XML using the XSL file you've pointed to.
But I wanted to run this as a seperate step from a batch file so that I can create my .html files on disk but maintain their content using XML.
Here's how I've done it:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Xsl;
using System.IO;
 
 
namespace Tool.XMLToHTML
{
    class Program
    {
        /// <summary>
        /// "Help\Help3\_HelpSource\gravity.xslt" "Help\Help3\_HelpSource"
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            
            try
            {
                Console.WriteLine("\n");
                Console.WriteLine("\tTransform xslt + xml to html.");
                Console.WriteLine("\n");
                
                
                if (args.Length != 2)
                    throw new ArgumentException("2 args required, .xslt file and the .xml file or directory");
                if (!args[0].ToString().Contains(".xslt"))
                    throw new ArgumentException(".xslt file expected as first arg");
                string xmlSourceLocation = Path.GetDirectoryName(args[1] + Path.DirectorySeparatorChar);
                if (!args[1].ToString().Contains(".xml"))
                {
                    if (!Directory.Exists(Path.GetDirectoryName(args[1] + Path.DirectorySeparatorChar)))
                         throw new ArgumentException(string.Format("Directory {0} does not exist."Path.GetDirectoryName(args[1]+Path.DirectorySeparatorChar)));
                    else
                       if(Directory.GetFiles(args[1], "*.xml").Length ==0)
                           throw new ArgumentException(string.Format("Directory {0} does not contain .xml files."args[1]));
                }
 
                Console.WriteLine("\n");
                Console.WriteLine("\tTakes the fullpath to the .xslt");
                Console.WriteLine("\tand the full path to the .xml or the directory containing the .xml.");
                Console.WriteLine("\tTransforms to .html files, in an output directory below the xml source location 'htmlOutput' i.e. {0}"Path.Combine(Path.GetDirectoryName(args[1]), "htmlOutput"));
                Console.WriteLine("\n");
 
                
 
 
                // Load the style sheet. 
                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(args[0]);
 
                string rootPath = Path.GetDirectoryName(args[1] + Path.DirectorySeparatorChar);
                string htmlOutputPath = Path.Combine(rootPath"htmlOutput");
                Directory.CreateDirectory(htmlOutputPath);
 
                if (Directory.Exists(xmlSourceLocation))
                {
                    string[] fileNames = Directory.GetFiles(xmlSourceLocation"*.xml");
 
                    foreach (string xmlFileName in fileNames)
                        xslt.Transform(xmlFileNamePath.Combine(htmlOutputPathPath.GetFileNameWithoutExtension(xmlFileName) + ".html"));
                }
                else//single file
                    xslt.Transform(args[1], Path.Combine(htmlOutputPathPath.GetFileNameWithoutExtension(args[1]) + ".html"));
                
                Console.WriteLine("\tSuccess. HTML output can be found at {0}\n"htmlOutputPath);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n");
                Console.WriteLine("\tFailure. Transform to HMTL failed.");
                Console.WriteLine("\t" + e.Message);
                Console.WriteLine("\n");
            }
            
 
        }
    }
}

Visual Studio 2010 HelpViewer 1.0 Custom Help Creation

Recently I've been working with our legacy HTML help files, trying to add them the the new Visual Studio 2010 HelpViewer. This is not a simple task.
  1. We wanted the styling of our html to be the same as .NET help files
  2. We would also like there to be little or no maintenance required to keep them up to date with the latest Visual Studio Help Styles etc.
  3. We have a custom development language so the code snippets require their own color coding and the Language should be "Gravity" and not C# or VB.
  4. The content itself should be easily maintained.
  5. Our own applications logos and copyright should appear instead of Visual Studios.
The requirements above proved to be not a simple to implement as you might think. Below I'll mention the snags I came across and describe how I overcame them.

1. Styling:
The styling information was not simple to find, there are documents on http://www.helpware.net/mshelp3/intro.htm which explain some of the content of the help files, this is useful but where do you start to get a custom page up and running which looks like a .NET help file? The solution I found is by looking in the Help Store "C:\ProgramData\Microsoft\HelpLibrary\content\Microsoft\store". In here you'll find the help mshi files etc.
The .msha files are actually Zip files which have been renamed, these contain .html help files.
The trick is to find the correct .msha file unzip it and look through the HTML file for the content you require. I found the Int32.CompareTo help file and the Windows.Form help file of great use. These help files contain the bare HTML with CSS links before the HTML has been rendered, it's useless to start looking at the rendered HTML with "view source" from the browser, it's too large.
A Web Form class help file I found of great use was in the "Development_Frameworks_21823146_VS_100_en-us_9.msha" file. I renamed this to .zip and extracted it's contents (this can also be done with an application called FAR, it can read the contents of the .msha file and will give you the option to unzip the contents). I extracted the contents to "C:\Users\me\Documents\Development_Frameworks_21823146_VS_100_en-us_9" and in here searched the .html files for the content I wanted. The file with title <title>MenuStrip Class (System.Windows.Forms)</title>
was the file I chose. I've made a copy of this and used it as my template for most things.

3 & 5. Color coding and logos, Branding.
   There are issues here, it looks like you cannot mix and match, what I mean is you either go with the complete look and feel of Visual Studio's help including the logos and copyright or else you loose it all and provide your own styling. This is all controlled by one simple metadata tag provided in each html help file

<meta name="SelfBranded" content="false" >meta>

    Changing this to "true" results in you losing all the styles and logos etc that msdn gives you, you must then provide your own styles, this solution is not really feasable as the styling and layout of the msdn help is complex and also I'd imagine it's better to keep with the sytles provided by msdn and if they update them then they get pulled in hopefully without and work.
If you do change this setting to "true" applying styles for the color coding snippets is simple with adding html spans to the keywords etc, giving the Span a style class and adding the correct style to a linked stylesheet.
e.g.

  <span class="kwrd">
            controlspan> Button BtnHelloWorld
           {
            <span class="kwrd">eventspan> Click
            {
             UI.ShowMessage(<span class="str">"Hello World"span>);  
            }
            <span class="kwrd">propertyspan> Text := <span class="str">"Say Hello"span>;
            <span class="kwrd">propertyspan> ToolTip := <span class="str">"Say Hello!"span>;
              <span class="kwrd">propertyspan> NextControl := <span class="kwrd">nullspan>;
           }
the class "kwrd" has a style like so

.kwrd
{
 color#0000ff;
}


4. Easily maintained.
I've used XSL and XML to store my help content. The idea behind this is the XSL stores the HMTL, the .NET help files are chunky with alot of styling, so I have one large XSL file "gravity.xslt" which all the XML files reference.
The XML files are simple the text content and some links. This keeps the content files size to a minimum, they contain the title name, the description, the Help Meta information (keywords, id, parent, TocOrder) etc. When new content is required all that needs to be added is a new XML file, copy the content from another and change the text.
I've made the XSL file have as many <xsl:if> statements as possible
In order for XML to become (X)HTML it needs to be transformed using some application (usually a browser) and the XSL file. I wanted to do this in one go from command line so I've written a small application which takes the XSL file and a directory containing the XML files, this outputs a HTML file for each XML file. It's these HTML file's that I later add to my Zip and remame to a .msha file, my help file.



January 28, 2011

TFSbuild custom parameters and Testing

To test your Team Build Project locally with TFSBuild use the Visual Studio 2010 command prompt.
here's a sample
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>TFSBuild start /collectio
n:http://MYSERVER:8080 /builddefinition:"MYTFS/TestBuild" /droplocation:"\\
SOMECOMPUTER\SOMEDIRECTORY"


However firstly I recommend testing the syntax of your project with MsBuild
e.g.
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC>msbuild C:\SOMEDIR\TeamBui
ldTypes\MsBuildSamples\TFSBuild.proj


In order to tell msbuild to run a particular target within use something like
C:\MsBuildSamples>msbuild TfsBuild.proj /t:DesktopRebuild
this will run the Target "DesktopRebuild".


Passing Parameters/Arguments

To use parameters passed in through TFSbuild it's simple, just pass in your argument with the /property switch on the command line or do it through the Visual Studio Team Build UI.
this can first be tested with MSBuild
e.g.
C:\TeamBuildTypes\MsBuildSamples>msbuild TfsBuild.proj /t:DesktopRebuild
/property:MyArgument=2

inside in your TfsBuild.proj tjos argument can used anywhere with the syntax $(MyArgument)

And further test with TFSBuild from the command line
C:\Topoix\TeamBuildTypes\MsBuildSamples>TFSBuild start /collection:http://offaly
:8080 /builddefinition:"Topoix-scrum/TestBuild" /droplocation:"\\dunnacarraige\b
uildDrops" /msbuildarguments:"/p:MyArgument=2"

These same msbuildarguments can be passed in through the Visual Studio Queue Build dialog in the "MSBuild command line arguments" panel with
/p:MyArgument=2


January 27, 2011

Build with .NET 4.0 in Team Foundation Server

In order to force your Team Build to use the .NET assemblies and tools you'll need to make the following change.

Change the value in the

C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies\tfsbuildservice.exe.config

From

add key="MSBuildPath" value=""

to

add key="MSBuildPath" value="C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319"

TFS service must be restarted afterwards "Visual Studio Team Foundation Build"

http://blogs.infosupport.com/blogs/martijnb/archive/2010/05/04/building-visual-studio-2010-solutions-using-team-build-2008.aspx

November 01, 2010

Dependency Injection

Loose coupling i.e. making your code modules less tightly bound to each other.
Dependency Injection helps this process, allowing clients of your code to get the application using their own code without them having to know very much at all as to how the application works internally.

The article linked in the title of this post has a very brief outline of some how it can work but basically I'm interested in using Interfaces and registering my own classes with the host application with the minimum possible code work.

There are lots of third party frameworks which provide implementations.
The method is usually the same, create and instance of the third party class and then call a method on it which tells the framework to run a particular concrete class that you have provided when a particular interface or type is used, it's a form of mapping allowing client classes to be used at runtime.

August 11, 2010

ISerializable - Serializing objects

Serializing is a mechanism to create a text/binary version of your objects state, this can be saved or sent somewhere and then deserialized back into the instance again.
i.e. The member variables of your Type get written somewhere with their values of the instance of your Type.

e.g.
Here's the class who's instance I want to recreate somewhere else or at a later time.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleClass
{
public class Class1
{
public int _myMemberVar;

///
/// required for serialization
///
public Class1()
{
_myMemberVar = 0;
}

public Class1(int value)
{
_myMemberVar = value;
}
}
}

Here's a class which Serializes the instance of the class above with the XMLSerializer. The version below writes the XML version of the instance to the Console, you could replace the
using (Stream str = Console.OpenStandardOutput())
with a FileStream of some sort to write the XML to file.
This results in

www.w3.org/2001/XMLSchema">

<_mymembervar>10



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Xml.Serialization;

namespace CallMySimpleClass
{
class Program
{
static void Main(string[] args)
{
SimpleClass.Class1 c = new SimpleClass.Class1(10);

string result = Serialize(c);
Console.WriteLine(result);
}
static string Serialize(object o)
{
string result = null;
using (Stream str = Console.OpenStandardOutput())
{
XmlSerializer x = new XmlSerializer(typeof(SimpleClass.Class1));
x.Serialize(str, o);
}
return result;
}
}
}




So that's how the default Serialization works but what if you want to customize how Serialize works.To do this you can either make members Serializable and NonSerialiazed by putting the [Serializable] and [NonSerialized] attributes on your Types and members (Note: these only work when using the Formatters .net gives you BinaryFormatter and SoapFormatter).Or you can take over the process completely buy making the Type you want to serialize implement ISerializable.


ISerializable: