Rui Machado

Technologies and Information Systems

  Home  |   Contact  |   Syndication    |   Login
  4 Posts | 3 Stories | 2 Comments | 0 Trackbacks



Article Categories


Post Categories

Sunday, October 7, 2012 #

Nullable types vs Non-nullable types


While developing our C# projects its frequent the null comparison operation to avoid null exceptions. This simple operation is mainly coded using the "var x = null" code example inside an if clause. However not all types of variables are nullable, which means that setting a variable to null is not allowed in every cases, it depends on what kind of type are you defining. But what if there was an extension to your non-nullable type that would convert your variable types to nullable? This extension really exists.

As I said before in C# you have nullable types which represent all the values of an underlying type, and an additional null value and can be declared easily using "T?", where T is the type of the variable and for example the normal int type cannot be null, so its a non-nullable type, however if you define a "int?" your variable can be null, what you do is convert a non-nullable type to a nullable type.


int x=null;     Not allowed




int? x=null;   Allowed




While using nullable types you can check if a variable is null the same way you do it with nullable types:




But what about setting a default value when a certain variable is null?


In this cases the c# .net framework let you set a default value when you try to assign a nullable type to a non-nullable type, using the ?? operator.

If you don't use this operator you can still catch the InvalidOperationException which is throw in this cases.

For example  without the ?? operator :




Using the ?? operator your code becomes cleaner and more easy to read and you get a bonus, you can set a default value for multiple variables using the ?? in a chain set.




That’s it,


Thanks, Rui Machado

Nowadays comunication between applications is an active topic with daily usage and a large amount of pratical appliances. While developing an app in witch I had to consume an OData I found out that combining Linq with my code made this operation pretty easy.

The algorithm to consume OData starts with adding a service reference to Visual Studio:


After adding the service reference in wich you define the uri to the service, we start building our code.

In your code the algorithm is the following:

  1. Define the Uri to your OData Service
  2. Define the context of your odata, wich contains all entities exposed by the service.
  3. Query the context using Linq
  4. Print the result

Easy and simple.

Example code:

01public static void Main(string[] args){
03        Uri serviceUri= newUri("", UriKind.Absolute);
04        ODataService.ServiceEntities context = newODataService.ServiceEntities (serviceUri);
06        context.Credentials = newSystem.Net.NetworkCredential(Username,Password);
08         var query = from ServiceObject in context.YourEntity
09                     select ServiceObject ;
11        foreach (var myObject in query)
12        {
13            Console.WriteLine("\n Field1: {0} | Field2: {1}",
14            myObject .Field1, myObject .Field2);
16        }

That’s it.

Thank you,

Rui Machado

Thursday, October 4, 2012 #

BizTalk Server 2010 Mapping Challenge

Many to One Looping

Imagine that you have a scenario where in the source schema you have a structure with three records with the same elements, however the first record is unbounded while the sencond and the third one as only one occurance. Now you need to map this three records into one single record in the destination schema.


Source Schema Records:
- Vendor (Unbounded)
- Client (Occurs once)
- Owner (Occurs once)

Destination Schema:
- Participants (unbounded)

Check out my two solutions for this challenge bellow.

Here is the XSLT Code:

03    <xsl:value-of select="//Personal/ID"/>
06<xsl:for-each select="Personal">
07<!-- Process each vendor -->
08<xsl:for-each select="Vendor">
09    <xsl:variable name="fname" select="FName"/>
10    <xsl:variable name="lname" select="LName"/>
11    <xsl:variable name="addre" select="Address"/>
12    <xsl:variable name="city" select="City"/>
14    <Participant>
15        <FName>
16            <xsl:value-of select="$fname"/>
17        </FName>
18        <LName>
19            <xsl:value-of select="$lname"/>
20        </LName>
21        <Address>
22            <xsl:value-of select="$addre"/>
23        </Address>
24        <City>
25            <xsl:value-of select="$city"/>
26        </City>
27    </Participant>
29<!-- Process the client-->
31    <FName>
32        <xsl:value-of select="Customer/FName"/>
33    </FName>
34    <LName>
35        <xsl:value-of select="Customer/LName"/>
36    </LName>
37    <Address>
38        <xsl:value-of select="Customer/Address"/>
39    </Address>
40    <City>
41        <xsl:value-of select="Customer/City"/>
42    </City>
44<!-- Process the owner -->
46    <FName>
47        <xsl:value-of select="Owner/FName"/>
48    </FName>
49    <LName>
50        <xsl:value-of select="Owner/LName"/>
51    </LName>
52    <Address>
53        <xsl:value-of select="Owner/Address"/>
54    </Address>
55    <City>
56        <xsl:value-of select="Owner/City"/>
57    </City>


This Custom Pipeline Component for BizTalk Server 2010, receives a compressed message, unzips it and sends one message per each file that the compressed file as in it. Imagine the following scenario: you have ten files that you want to send to some destination, instead of sending ten messages (one per file) you can easily send one compressed file (with all the ten files in it), taking as only precaution  the insurance that the destination system as this component in its receive pipeline.

Building the sample

After downloading the project folder, you just need to bild it and copy the DLL generated to C:\Program Files (x86)\Microsoft BizTalk Server 2010\Pipeline Components, after that Create a new BizTalk Server Empty Project, add a new Pipeline, go to your Pipeline toolbox, right click -> Choose Item -> Pipeline Componentes -> Check the option “DisUnzip”. Now drag this component to the Dissamble fase andadd a XML Disassembler right next to DisUnzip.

Unzip Method

The main method in this project has a very simple algorythm that you can check bellow:

01 public voidDisassemble(Microsoft.BizTalk.Component.Interop.IPipelineContext pc
02 , Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
03         {
04             IBaseMessage Temp = inmsg;
05             using (ZipFile zip = ZipFile.Read(inmsg.BodyPart.GetOriginalDataStream()))
06             {
07                 foreach (ZipEntry e in zip)
08                 {
09                     var ms = new MemoryStream();
10                     IBaseMessage outMsg;
11                     outMsg = pc.GetMessageFactory().CreateMessage();
12                     outMsg.AddPart("Body", pc.GetMessageFactory().CreateMessagePart(), true);
13                     outMsg.Context=inmsg.Context;
15                     e.Extract(ms);
16                     string XMLMessage = Encoding.UTF8.GetString(ms.ToArray());
18                     MemoryStream mstemp = new System.IO.MemoryStream(
19                     System.Text.Encoding.UTF8.GetBytes(XMLMessage));
21                     outMsg.GetPart("Body").Data = mstemp;
23                     _msgs.Enqueue(outMsg);
24                 }
25             }
26         }

Besides this method the dissambler component should Have the GetNext() method to enforce the pipeline to send each one of the messages that you enqueue in the Queue collection:

01 public Microsoft.BizTalk.Message.Interop.IBaseMessage
02             GetNext(Microsoft.BizTalk.Component.Interop.IPipelineContext pc)
03         {
05             // get the next message from the Queue and return it
06             Microsoft.BizTalk.Message.Interop.IBaseMessage msg = null;
07             if ((_msgs.Count > 0))
08             {
09                 msg = ((Microsoft.BizTalk.Message.Interop.IBaseMessage)(_msgs.Dequeue()));
10             }
11             return msg;
12         }


The source code can be found and download here: