1000 Lines Of Code

Scott Watermasysk, of .Text and Community

Server fame, has a post on

his new blog which talks about the start of his

current project at Telligent called Graffiti.

In it he discusses his opinion that if a first version of your project cannot be accomplished

in under 1,000 lines of code, then it probably is being over-engineered from the get

go.

Key to this premise though is the idea that you re-use pieces produced by others in

order to avoid writing your own code. He discusses a large variety of other

libraries which Graffiti uses, or used in the first few releases. Some have

been replaced, some are still used, but the point is something that is discussed in

“agile” circles quite a bit and circles around lots of catch phrases like “Release

Often”, “Keep It Simple”, “YAGNI” and the like. What I particularly like about

Scott’s example is that it is a stand against “Not Created Here” which is a horrific

airborne virus that many corporate IT departments have caught.

How many things that you’ve written code for do you know for a fact have been solved

before and you’re re-writing simply yo re-write? Data Access, yup. ORM,

yup. Security, yup. Nearly every problem has been solved before, and using an existing

library does not diminish the value of your project in fact it increases it because

your “Time to Market” will be much higher.

Go read Scott’s post, and when your done riddle me this dear reader : What is the

biggest piece of code have you personally written that you know you shouldn’t have?

Mine is easy, I’ve re-engineered an entire Rules Engine when BizTalk Server’s Rules

Engine was available to me. That was a big piece of code, like 3 developers

for 4 months kind-of big. Live and learn.


Tim Rayburn is a consultant for Sogeti in the Dallas/Fort

Worth market.

Distinguished Fields and Optional Elements

Thought this might be of interest to a few BizTalk developers out there.

It’s quite common practice to use Distinguished Fields in orchestrations, to get/set
the value of an element or attribute.

However, if the element you’re trying to set/get doesn’t exist, then this poses a
few problems.

Setting a Distinguished Field

If the element/attribute doesn’t exist in the target message, then you’ll get an exception,
no questions asked – there’s no way around this.
Under the covers, the SetDistinguishedField method
is called to set the value – and it doesn’t check if the element/attribute exists
first.
The same thing happens if you use the xpath() function
to set a value, and the element/attribute doesn’t exist – BizTalk isn’t about
to modify your message and add the element/attribute for you.

In this case, it’s your responsibility to check that the element/attribute exists.
If it’s a message that you create, then you have to make sure that the element/attribute
is created (do this in a map by mapping an empty Value Mapping functoid to it, or
setting the Value property to “<empty>“).

Getting a Distinguished Field

This is more interesting.
In a nutshell, if the Distinguished Field is of type string (or
any other nullable type) then you’ll get null.
Otherwise, you’ll get an InvalidCastException.

The reason for this is in the underlying code.

The orchestration calls XSDPart.GetDistinguishedField to
get your value:
public override object GetDistinguishedField(string
dottedPath)
{
    XsdDistinguishedFieldDefinition dFDef = (XsdDistinguishedFieldDefinition)
base.GetDFDef(dottedPath);
    object val = base._getXPath(dFDef.XPath, dFDef.IsDynamic);
    if (val == null)
    {
        return null;
    }
    if (val.GetType() != dFDef.FieldType)
    {
        val = XmlHelpers.ChangeType(val, dFDef.FieldType);
    }
    return val;
}

This method will return null if the element/attribute doesn’t exist otherwise it will
attempt to convert (cast) the value to the appropriate type.

However, the InvalidCastException arises
from your orchestration.
Let’s assume you had a Distinguished Field called isSuccessful which is of type boolean.

When you write something like

if (Response.isSuccessful)

in an expression shape, the code generated is this:

if (!((bool) __ctx2__.__Response.part.GetDistinguishedField(“isSuccessful”)))

Ahah! The orchestration does an explicit cast of the Distinguished Field value to
a bool.
And an explicit cast of null to a bool will raise an InvalidCastException.

However, an explicit cast of null to a string is fine.

So if you had a Distinguished Field called employeeName of type string, then the same
code would be:
if (!((string) __ctx2__.__Response.part.GetDistinguishedField(“employeeName”)))

And no exception would be raised.

Conclusion

If you’re using Distinguished Fields with types other than string, and the element/attribute
you’re referring to is optional then you must check that the element/attribute exists
before attempting to set/get a value.

In most cases, you’d do this by using the xpath() function
in an orchestration
(e.g. getting a count of the element/attribute, and checking that count >
0)

Just FYI….!

Visual Studio 2008, WCF Service Libraries, and CTRL-F5

One of the cool new features in Visual Studio 2008 is automated debugging for WCF service libraries. Simply create a new WCF Service Library project (using the new WCF project templates), press F5, and viola!


When you press F5, Visual Studio launches WcfSvcHost.exe, a generic WCF host application that hosts your service library. WCF Service Library projects now come with an App.config file — this is where WcfSvcHost.exe reads the service/endpoint configuration during initialization. A generic WCF client application (WcfTestClient.exe) is also started, and it communicates with your service via MEX to download metadata. It provides a generic mechanism for filling out the data required by each service operation, invoking the operation, and viewing the results. It even works with complex types unlike the ASMX-generated test forms. Sweet.


You can configure the service library to use a custom client application if you desire. Go to the Debug tab within the project properties and specify your app as a command-line argument ( /client:“YourClient.exe“ ).


Note: for some reason the latest drop of the BizTalk Services SDK (0.9.0324.0) breaks this feature. So if you have the BizTalk Services SDK installed and you try pressing F5, you’ll most likely get the following exception:



Unhandled exception has occured in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.


Object reference not set to an instance of an object.


If you uninstall the BizTalk Services SDK, this feature will work normally once again.

Extract the ST information from an N1 loop and populate a flat file

If you’ve ever had problems with using looping on an N1 loop extracting the ship-to information if the ship-to is notin the first loop, this might help.

Old way of doing it.

New way of doing it.

Here’s the functiod logic.

Public Function ReturnSTInfo_1(ByVal IterationSTring_1 As String, ByVal N104_1 As String) As String
Dim BeginningIndexValue_1 As Integer
Dim EndingIndexValue_1 As Integer
Dim LengthOfElement_1 As Integer
BeginningIndexValue_1 = System.Convert.ToInt32(IterationSTring_1.Substring((IterationSTring_1.IndexOf(“ST”) – 2), 1))
EndingIndexValue_1 = BeginningIndexValue_1 + 1
If N104_1.IndexOf(System.Convert.ToString(EndingIndexValue_1) + “*”) = -1 Then

Return N104_1.Substring((N104_1.IndexOf(System.Convert.ToString(BeginningIndexValue_1) + “*”) + 2))

Else

LengthOfElement_1 = (N104_1.IndexOf(System.Convert.ToString(EndingIndexValue_1) + “*”)) – (N104_1.IndexOf(System.Convert.ToString(BeginningIndexValue_1) + “*”) + 2)
Return N104_1.Substring((N104_1.IndexOf(System.Convert.ToString(BeginningIndexValue_1) + “*”) + 2), LengthOfElement_1)

End If
End Function

XML Tools

Goal:
To describethe design time experiencein BizTalk R2EDI.
Overview:
XML Toolsextension hosted in Visual Studio is used to provide design time support to users to improve usability of EDI Schemas and also assist in the validation and generation ofBTS EDIartifacts.The 3 main functions are “Validate Schema“, “Validate Instance“and “Generate Instance“.
To perform these3 functions,a user needs to followseries of […]