String to a Stream

The time has come where I needed to handle streams, so the first I needed to generate some tests and within these tests I need to generate a stream based on some text.  So the first task was to convert a String in to a Stream, and while I am at it how about the other direction to (Stream to a String)

String in to a Stream

string text = “To Jani, Reminder Don’t forget me this weekend!'”;
byte[] byteArray = Encoding.ASCII.GetBytes(text);
MemoryStream streamXML = new MemoryStream(byteArray);

Stream to a String

xml.Seek(0, SeekOrigin.Begin);
var readerXML = new StreamReader(xml);
string textXML = readerXML.ReadToEnd();

 

Rapid Generation of test objects

I came across a very easy to use and useful generation tool for objects today, NBuilder.

Through a fluent, extensible interface, NBuilder allows you to rapidly create test data, automatically assigning values to properties and public fields that are of type of the built in .NET data types (e.g. ints and strings). NBuilder allows you to override for properties you are interested in using lambda expressions.

If you understand Lambda then you’re going to be able to build test data for your objects.

All you need to do is reference FizzWare.NBuilder.dll in to your test project and you are off.

FizzWare.NBuilder-2.1.9-beta.zip (32.47 kb)

XSD Schema’s – Reference

If you have ever come across generating XSD’s then you will soon find out that it’s a new ball game.  So in order to help you along I have generated this reference point of items that I have found useful.

XML Schema data types. The most common types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Optional and Required Attributes

Attributes are optional by default. To specify that the attribute is required, use the “use” attribute:

<xs:attribute name=”lang” type=”xs:string” use=”required”/>

WCF Message can only be read once

WCF is a very powerful tool for developers, and I’m sure as you dive in to the development environment you will come across the WCF Message, which is part of System.ServiceModel.Channels Namespace. You should be aware that a message body can only be accessed once and a message can only be written once.  This will in turn can cause a problem if you are creating your own IDispatchMessageInspector, as at some you will need to read the message and once you have read it, you are buggered!

So in order to use the Message you will have to copy the message in to a buffer using the CreateBufferCopy method, here is how you go about it

public void Validate(ref Message message)
{
    MessageBuffer buffer = message.CreateBufferedCopy(int.MaxValue);
   
try
    {
    ValidateInput(buffer.CreateMessage());
    }
   
catch (Exception)
    {
   
throw;
    }
   
message = buffer.CreateMessage();
}

XSD to Objects

I’ve been dealing with REST services within WCF, and one thing that has come to my attention is that the calling service or third party only has access to the REST interface and the XSD’s.

The XSD’s holds all the contract information, therefore if you need to generate a concreteclass you are going to have to translate the XSD to an object. 

I’ve have come across the XSD Object Generator frm Microsoft which appears to do the job.

XSDObjectGen.msi (667.00 kb)

MVVM Material

After attending a talk on Silverlight 4, MVVM and TDD with Jesse Liberty, I thought it would be an idea to find out more information on MVVM, James Green provided me with a number of worth while links by Jason Dollinger from Lab49.  The main blog post is here http://blog.lab49.com/archives/2650 but the actual video is here http://www.lab49.com/files/videos/Jason%20Dolinger%20MVVM.wmv big download (about 1.5 hours).

The source code for the project he is doing is here http://blog.lab49.com/archives/2689

Another good one is by Karl Shifflet (he’s a VS IDE developer) http://channel9.msdn.com/shows/Continuum/MVVM/

This guy is worth following as well http://sachabarber.net/?cat=17

Want to know not only MVVM but all about Silverlight 4 too, check out Scott Gu’s post for Silverlight 4 Training Kit

Passing Validation exceptions via WCF REST

When using WCF and Rest one thing you’re going to have to consider is the validation of the object, the way to do this is using the Validation Application Block included in the Microsoft Enterprise Library to validate the data transfer objects. This way you can decorate the objects with attributes to validated the objects

Here is an example

public static void Validate<T>(T obj)
{
     ValidationResults results = Validation.Validate(obj);

     if (!results.IsValid)
         
string[] errors = results
          .Select(r => r.Message)
         
.ToArray();

     WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
     WebOperationContext.Current.OutgoingResponse.StatusDescription = String.Concat(errors);
}

thanks to Enrico Campidoglio for helping out on this

Lifestyles of the Castle Windsor IoC Containers

It’s worth knowing how the Castle Windsor IoC work, by default it uses Singletons, where this will work for the most of the time, there are a lot more options available to you depending on your needs.  These lifestyles that are available in Castle Windsor are:

  • Singleton: components are instantiated once, and shared between all clients
  • Transient: components are created on demand
  • PerWebRequest: components are created once per Http Request
  • Thread: components have a unique instance per thread
  • Pooled: Optimization of transient components that keeps instance in a pool instead of always creating them
  • Custom: allows you to specify a custom lifestyle… you’d have to specify a type that implements the ILifeStyleManager interface

For further information on this take a look at Castle Windsors Lifestyles or  Windsor and component instance lifetimes

this and that