Technology with opinion

Wednesday, September 28, 2005

Using Development Tools via command line

In order to access many handy .Net tools or BizTalk for that matter, you must navigate to the proper directory first, not an option, or launch Visual Studio .NET 2003 Command Prompt.

A way around this is to add Paths to your system Path Environment variables. To do this, do the following:

  1. Right-click My Computer, click Properties
  2. Click the Advanced tab, then the Environment Variables button
  3. Under System variables select Path, then click Edit
  4. Add the following to the end of the Variable value field:
  5. ;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\;C:\Program Files\Microsoft BizTalk Server 2004\
Now you can perform the following utilities (and more) from any command prompt: installment, gacutil, ilasm & btsdeploy.

Wednesday, September 07, 2005

Best Practices

I've been working on a best practices document containing sections for C#, .Net, BizTalk 2004, SQL Server, and IIS. Below is the best practices for each of the following technologies, according to me (Scott White).

Feel free to comment, challenge or add to the respected lists. If change makes sense, I'm more than welcome to make it.

Thanks!

Best Practices: C# & .Net

  1. Private and class-level variables should be written using camel casing
  2. Public members should be named using Pascal casing
  3. Use proper namespacing within all your .Net classes & projects such as: [CompanyName].[BusinessUnit].[ProjectName].[VSNetProjectName]
    Example:
    Microsoft.Office.HelloWorld.Ui
    Microsoft.Office.HelloWorld.Data
  4. Create separate projects and namespaces for your UI, Data and Business layers
  5. Use DataSet when retrieving results from database and the data needs to be reread, serialized, cached or multiple sets of data need to be related and joined
  6. Use DataReader when retrieving results from database and the data needs to be read once and displayed quickly
  7. Use XPathDocument when reading XML and needing to extract elements (use XPath, etc) out of the XML document
  8. Use XmlDocument when needing to perform two or more of the following: read, write or serialize.
  9. Use Command objects to execute SQL code or stored procedures
  10. Use EnterpriseLibrary (Application Blocks) in your application where possible, as they are a collection of best practices in working with .Net
  11. Use StringBuilder object to construct strings instead of using plus (+) sign to concatenate
  12. Use regular ‘for’ loop instead of ‘for each’ loop where possible, as traditional ‘for’ loop is faster and more efficient
  13. Avoid using DataAdapter objects
  14. Avoid using the DataGrid for inline editing, especially the more complex the process and logic is, as this creates something resembling spaghetti code
  15. Add attributes to web service classes and their public exposable members by using [WebService] and [WebMethod]. Properly namespace your web service and document the web service and your web methods this way
  16. Add XML Comments to all private and public members
  17. Use source code control

Best Practices: BizTalk 2004

  1. Although simple mappings can be performed using BizTalk Mapper, use XSLT to perform mappings if they get to long and/or complex
  2. Do not use Human Workflow Services (HWS) or even install it, as it is not a good Human Workflow engine and Microsoft is going to deprecate it
  3. Fix all SQL Server jobs that are created when you install BizTalk, most of them will not successfully run when BizTalk is initially installed
  4. After installation of BizTalk (assuming you installed Enterprise Single-Sign On), make sure you backup and document your master secret
  5. Use separate Active Directory Users for each BizTalk service and Groups for BizTalk Hosts. This will keep BizTalk more secure by granted minimum access needed as well as making your BizTalk environment easier to scale out.
  6. Create separate BizTalk Hosts for each solution deployed
  7. Create separate BizTalk projects and namespaces for Orchestrations, Schemas and Pipelines
  8. Following:
  9. [CompanyName].[BusinessUnit].[ProjectName].[VSNetProjectName]
    Example:
    Microsoft.Office.HelloWorld.Orchestrations
    Microsoft.Office.HelloWorld.Pipelines
    Microsoft.Office.HelloWorld.Schemas
  10. Use Enterprise Library Configuration blocks within your Orchestrations to avoid hard coding information in them
  11. To avoid manual deployment with BizTalk, script it (using batch files) or create an MSI package to install it
  12. Use late binding for send/receive ports
  13. Name your send/receive ports after your solution’s namespace, using Pascal casing
  14. Use source code control

Best Practices: SQL Server

  1. Casing for SQL Server objects should be similar to that of .Net: Public members (tables, sprocs, views, columns) Pascal cased & Private members camel cased
  2. Use Store Procedures everywhere possible
  3. Document and model all security access to database objects that need to be granted, then write the script for this
  4. Write scripts by hand using a text editor (Query Analyzer, UltraEdit, TextPad, VisualStudio.Net) or an intelligent tool such as Erwin
  5. Avoid triggers and give much thought to how you write them if you write them as they can have bad side effects
  6. Avoid using cursors within your stored procedures as logical code such as this should normally be performed in your application (middle) tier not your database tier
  7. Use source code control

Best Practices: IIS

  1. Create entirely separate web sites for each ‘Solution’ or ‘Project’ that is deployed to your web server. Do not put all web sites beneath the ‘Default Web Site’ and ‘wwwroot’
  2. Document and then modify the NTFS permissions of your web root directories as needed by your web site
  3. Use HTTP Header to host multiple web sites on one server/environment
  4. Do not install Front Page extensions on a web server

Friday, September 02, 2005

Overloading Web Service Methods in .Net

A largely misunderstood issue in .Net web services is the capability of overloading web service methods. Overloading web service methods is possible and very easy but it helps to have some understanding of SOAP first.

If you navigate to your web service and click the link "Service Description", or just add ?WSDL onto the end of your web service within your browser, example:

http://localhost/MyWebServices/WebService1.asmx?WSDL

You will see the Web Service Description Layer (WSDL). Inside your WSDL you will see your web service methods represented as operations such as:

<wsdl:operation name="Add">
...
</wsdl:operation>

Now if you go into your Add method inside .Net and add try to overload it, the problem you have is that method names within .Net map to both operations and message elements within your WSDL and you cannot have two message names that are the same, example:

<wsdl:message name="AddSoapIn">
<wsdl:message name="AddSoapIn">

Cannot exist twice. The fix is simple, add a WebMethod attribute to both methods and make sure they have distinct names between them.

[WebMethod(MessageName="Add_TwoNumbers")]
public int Add(int x, int y)
{
return x + y;
}

[WebMethod(MessageName="Add_ThreeNumbers")]
public int Add(int x, int y, int z)
{
return x + y + z;
}

Now your WSDL will have message elements with names:
Add_TwoNumbersSoapIn
Add_TwoNumbersSoapOut
Add_ThreeNumbersSoapIn
Add_ThreeNumbersSoapOut

But your operations (within WSDL) will have the following:
<wsdl:operation name="Add">
<wsdl:operation name="Add">

Thus when you generate your proxy class for your web service (IE: Web Reference, or WSDL.exe) then you can open your proxy class and see how it maps overloaded methods to the operations properly.

While it's not necessary to rewrite the WSDL yourself, understanding how to manipulate the WSDL through attributes within your .Net web service is invaluable.

See Also: WSDL spec from the W3C

Wednesday, July 27, 2005

Run NUnit Tests on Compilation

Test driven development is an approach to programming where you write your unit testing code to test for the functionality & requirements first. Then code is written to write the unit of code (class, etc).

For .Net programmers, the tool of choice for unit testing is NUnit. For more information about NUnit, see toolkit below.

I recommend creating a project for your unit testing, .UnitTesting will suffice. To automate the process of testing, it is helpful to have unit testing performed upon compilation, thus every time your code changes you can rerun your unit tests and not have to manually rerun NUnit.

To do this, you need to run NUnit's commandline utility, located in NUnit's Program Files folder \bin.

Create your testing class and methods:

[TestFixture]
Class1
{
[Test]
public void Test1()
{
Project1.MyUnit1 minute = new Project1.MyUnit1();
Assert.AreEqual(myUnit.Add(1,2), 3); // assert an exception if 1 + 2 <> 3
}
}

Next, go to the project properties for your NUnit project, click Build Events on the left-hand side. In the Post-build Event Command Line field paste the following:

"%programfiles%\NUnit 2.2\bin\nunit-console" "$(SolutionDir)\Project1.nunit" > "$(SolutionDir)\Project1.nunit.log"

This assumes your .nunit file is in the root of your solution. Variables above might need to be changed to work with your project. Once you build this logic into your solutions, unit testing can become seamless.

Tuesday, July 12, 2005

XSLT Date/Time & Upper Case functions in .Net Framework

Due to Microsoft's lack of support for XPath 2.0, as well as their dropped support for most of the own extended fuctions, all that remain accessible now is customizable script support in XSLT, for those who need functionality outside of the scope of XSLT 1.0.

To perform many useful functions such as upper case, date/time formatting, etc, you must write custom script. Below is an example of date/time formatting I'm performing within my XSLT document for a BizTalk project, although this will work for any .Net application.

First: Add the following namespaces to your XSLT declaration

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://www.tempuri.org/User">


Second: Write Custom functions

<msxsl:script implements-prefix="user" language="C#">
<![CDATA[
public string FormatDate(XPathNodeIterator node, string xpathExpr, string format)
{
XPathNavigator nav = node.Current;
if(nav.MoveToAttribute(xpathExpr, ""))
{
DateTime dt = DateTime.Parse(nav.Value);
return dt.ToString(format);
}
else
{
throw new ApplicationException("Cannot transform document, specified XPath expression is not valid.");
}
}

public string ToUpper(XPathNodeIterator node, string xpathExpr)
{
XPathNavigator nav = node.Current;
if(nav.MoveToAttribute(xpathExpr, ""))
{
string fullName = nav.Value;
return fullName.ToUpper();
}
else
{
throw new ApplicationException("Cannot transform document, specified XPath expression is not valid.");
}
}
]]>
</msxsl:script>


Finally: Call your custom XSLT function, inline within your XSLT


<xsl:value-of select="user:FormatDate(., 'Value', 'M/d/yyyy h:mm:ss tt')"/>
<xsl:value-of select="user:ToUpper(., 'Value')"/>

Thursday, June 30, 2005

Test Driven Development - NUnit

NUnit Pocket Reference by Bill Hamilton, published by O'Reilly is a good overview and reference of test driven development and the usage of the NUnit tool for .Net software development.

Google Print has a section of this book for free and is a good place to start learning test driven development.

Friday, June 03, 2005

Microsoft removes XQuery from .Net framework 2.0

Microsoft has unfortunately decided to remove support for XQuery (XML Query) language from the .Net Framework 2.0. XQuery was planned to be in Microsoft's next version of the framework and was even somewhat implemented in previous beta versions.

Microsoft has also chosen to not implement XSLT 2.0 standard (4.0 working draft) in .Net framework 2.0.

Thursday, May 19, 2005

Mozilla: Thunderbird 1.0.2 Download & Review

I downloaded and installed Mozilla Thunderbird in the past and tried it out. It's a free, user friendly and highly tweakable Email client (replacement for Outlook Express for most of us). I personally replaced Microsoft Outlook with it at home.

Review:
This is a very good product and will only get better. The spam filtering is excellent, it learns very quickly and hotkeys make marking (and unmarking) spam easy. No note taking functionality is built-in, however many great note taking extensions exist. If you love FireFox, you will love Thunderbird. The product is not fully mature yet, but is a good product that will continue to get better.

FireFox 1.0.4

FireFox 1.0.4 has been released. It's awesome as always, just some bug fixes.

MDAC 2.8 SP1

MDAC 2.8 SP1 has been released. For a full list of bug fixes & enhancements click here.

SQL Server 2000 SP4

SQL Server 2000 Service Pack 4.0 has been released. This is a recommended download as it fixes several previous bugs. Note though you might not want to install this on your server yet, from Microsoft:

"Microsoft has found an issue with the final build of SP4 that impacts customers who run SQL Server with AWE support enabled. This issue only impacts machines with more than 2GB of memory where AWE has been explicitly enabled. Customers with this configuration should not install SP4. Microsoft is currently working on the problem and will issue an update soon."

This should be a safe install for developers though.