Skip to main content

Posts

Linq everything you need to know

  Linq Links ·          Refactor foreach to Linq https://jasonneylon.wordpress.com/2010/02/23/refactoring-to-linq-part-1-death-to-the-foreach/ ·          Linq tutorial https://app.pluralsight.com/library/courses/linq-fundamentals/table-of-contents ·          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 ·          Deep Dive (advanced) https://www.simple-talk.com/dotnet/net-framework/giving-clarity-to-linq-queries-by-extending-expressions/ ·          Linq operators https://www.tutorialspoint.com/linq/linq_query_operators.htm ·       ...

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

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 w...

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.

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...

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.

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/ar...

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 >   ...

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"...

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. We wanted the styling of our html to be the same as .NET help files 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. 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. The content itself should be easily maintained. 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...

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" http://msdn.microsoft.com/en-us/library/ms181742.aspx 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 http://msdn.microsoft.com/en-us/library/ms181723(VS.90).aspx 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 o...

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

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. http://www.martinfowler.com/articles/injection.html

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 = Consol...