Pages

Showing posts with label DOTNET. Show all posts
Showing posts with label DOTNET. Show all posts

Sunday, 20 July 2014

Packaging and Consuming NuGet packages in .NET projects

NuGet Packaging

Package manager tool with the ability to produce and consume packages, makes a clutter free references when installing/uninstalling packages.
Upon installing a package will automatically add references added in packages and update appropriate configuration files.

Pre-requisites


Available packaging options

1.       Command line
2.       Package Explorer [GUI]

Packaging using command line

  • Launch command prompt [run as administrator]
  • Get in to NuGet executable path


  • Pick the assembly you want to create package(for e.g. DocumentFormat.OpenXml.dll)
  • Get in to NuGet installation folder and create “lib” folder
  • Place the assembly to be packaged in lib folder
  • Run the command in command prompt

  • Creates a new spec file in the NuGet installation folder

  • Open the spec file in notepad, and Update as per spec file mentioned below with appropriate details. Note: Do not copy content from the below mentioned spec file

  • Run command in command prompt with appropriate version details of the assembly

  • Package file will be created


Packaging using package explorer

  •  Launch NuGet Package Explorer and create new package
 

  • Enter appropriate details in meta data and click tick mark in top left corner of the view
  • Add lib folder to the package contents

  • Right mouse click on lib folder and add .NET folder [this may change based on your usage]

  • Right click on the .NET created on step above and add existing assembly file

  • Save the package

  • Verify the package in local folder

Consuming packages in projects using visual studio extension

  • Launch Visual studio and select package manager console
  • Click on Settings options located beside Package source list and add the location for local packages
  • Package repositories from web locations can also be referred and added in package sources.

  • Open project file to add references using NuGet Package
  • Right mouse click on project file and select Manage NuGet Packages

  • Select Local NuGet Packages Added in settings of package source in prior steps, you will be able to see packages dropped in the local folder

  • Click Install

  • Verify references in project references, assembly added in package will automatically added as reference to the project.

Wednesday, 4 April 2012

Return multiple variables from VB.NET function


Problem:

How to return multiple variables from a single method call?

Scenario:

To fetch error messages with the warning level based on the error code, there could be more scenarios in similar fashion. Almost every developer will be muddled for a moment how [?] and start thinking to overcome the situation in a better way.

Problem Code
Public Function GetErrorMessage(errorCode As Integer) As ??
     Dim errorMessage As String = "How to return multiple values!"
     Dim errorWarningLevel As Integer = 3

     Return errorMessage
     Return errorWarningLevel
End Function


Queries while implementing the above code,

  •        What could be the return type [String/Integer/ etc…]?
  •        If we write multiple return code lines, will it return to the calling method what we expect?


There have been lots of workarounds to handle this scenario here are few,

  •        Split the method into two business operations/methods
  •        Create a type which holds the required properties and return the type
  •        Adding in a list/hash-table and read the variables looping or with specific key
  •        Return as object [base type] and type cast in the calling method


Couple of advanced coding workarounds,

  •        Create an anonymous type in the called method and return as object, caller will type cast and convert into specific type
  •        Create an [Iterator] function and [Yield] the return variables/types, we can implement this using C# and also added as new feature in VB11

  Workaround:

Here is simple data structure introduced in .NET framework 4.0 TUPLE
Tuple can hold up to 8 different types in a single instance and return back to calling method, it not only holds the values but also acts as factory class that create instance of its static members.

Workaround Code
Private Sub MyTuple_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim errorDetails = GetErrorMessage(555)
        Dim errorMessage = errorDetails.Item2 'Item2 holds Error message in Tuple
        Dim errorWarningLevel = errorDetails.Item3 'Item2 holds Error WarningLevel in Tuple
    End Sub
 
Public Function GetErrorMessage(errorCode As Integer) As Tuple(Of Integer, String, Integer)
        Dim errorMessage As String = "How to return multiple variables in a single method call!"
        Dim errorWarningLevel As Integer = 3
 
        'Commented old code for reference
        'Return errorMessage
        'Return errorWarningLevel
 
        'Option 1: using TUPLE helper method 
        Dim errorDetails = Tuple.Create(errorCode, errorMessage, errorWarningLevel)
 
        'Option 2: Using TUPLE constructor
        Dim errorDetails As New Tuple(Of Integer, String, Integer)(errorCode, errorMessage, errorWarningLevel)
 
        Return errorDetails
 
    End Function


Advantages of using Tuples:

  •        Strongly typed
  •        Can hold any defined type [Structured  data types]
  •        Implements IStructuralComparable and IStructuralEquatable