Archive for the_time('F Y')


11 June 2007 in .Net & Code & Mindscape & Tools | Comments (2)

Mindscape LightSpeed Logo

In the weekend I was reworking a prototype of something I’m working on and I wanted to use LightSpeed to manage the data access and thought it would be appropriate as a code example of how to quickly and easily get started with LightSpeed. To clarify, I only needed LightSpeed for the persistence mechanism as opposed to the full domain modeling capability at this stage of my project.

The Problem
My application needed a mechanism for persisting configuration. The configuration this application stores is extensible and ideally will be extended by 3rd party plug-ins which means I needed to expose a service that can be used by these 3rd parties to store their custom configuration in a manner that will give access both to their plug-in as well as our application.

I’m also a staunch hater of >300KB XML configuration files and it’s early days in the prototype :)

Step 1: Create your model classes
I have a simple configuration entity that I wanted to be able to persist to a SQLite database (LightSpeed supports many different database engines by the way). At this stage of my prototype I’m effectively only storing a key/value pair, a human readable name and description and a type (e.g. string, color, int). In this code example you will need to reference LightSpeed in your project to gain access to the Entity<> class that we are inheriting off.

Note that LightSpeed takes care of the enum as well and will happily convert it to an int at the database level :) Too easy.

using System;
using Mindscape.LightSpeed;
 
namespace Mindscape.Carbon.Core.Configuration
{
  public class ConfigurationItem: Entity<guid>
  {
    public enum ConfigValueType
    {
      STRING,
      INT,
      COLOR
    }
 
    private string _referenceName;
    private string _value;
    private ConfigValueType _valueType;
    private string _displayName;
    private string _description;
 
    public string ReferenceName
    {
      get { return _referenceName; }
      set { Set(ref _referenceName, value, "ReferenceName"); }
    }
 
    public string Value
    {
      get { return _value; }
      set { Set(ref _value, value, "Value"); }
    }
 
    public ConfigValueType ValueType
    {
      get { return _valueType; }
      set { Set(ref _valueType, value, "ValueType"); }
    }
 
    public string DisplayName
    {
      get { return _displayName; }
      set { Set(ref _displayName, value, "DisplayName"); }
    }
 
    public string Description
    {
      get { return _description; }
      set { Set(ref _description, value, "Description"); }
    }
  }
}

Step 2: Create your database
Currently you create your database manually however we are working on tools to do this for you. Note that I’m using a GUID as the primary key on this table and we didn’t need to add it to the model object in the previous step, LightSpeed does this for you. You only need to supply the primary key type in Entity. Now you can jump into whatever database you’re working with and create the table.

In my case I was using SQLite and just used a simple create table command:

CREATE TABLE ConfigurationItem(
  Id                GUID            NOT NULL PRIMARY KEY,
  ReferenceName     NVARCHAR(50)    NOT NULL UNIQUE,
  Value             NVARCHAR(100)   NOT NULL,
  ValueType         INT             NOT NULL,
  DisplayName       NVARCHAR(100)   NOT NULL,
  Description       NVARCHAR(1024)  NOT NULL);

Step 3: Add LightSpeed to your configuration
We need to tell our application about the database that LightSpeed should be working with and we can do this in code or in the .config file for our project. In this example I’ve elected to put it into the app.config of my project.

  <configSections>
    <section name="LightSpeed" 
        type="Mindscape.LightSpeed.Configuration.LightSpeedConfigurationSection, Mindscape.LightSpeed" />
  </configSections>
 
  <connectionStrings>
    <add name="ConfigDB" connectionString="Data Source=MyDatabase.db3"/>
  </connectionStrings>
 
  <LightSpeed dataProvider="SQLite3"
           connectionStringName="ConfigDB"
           identityMethod="Guid"/>

Step 4: Work with your data
That’s it! All the configuration work has been completed and there is no heavy XML mapping file or complex setup to tell LightSpeed about the database. The whole philosophy behind LightSpeed is to help developers get work done quickly and I hope this example and your own work with LightSpeed proves that. In following posts I will provide detail about working with your data however here is a taster of how easy it is to now put new objects into the database:

    ConfigurationItem item = new ConfigurationItem();
    item.ReferenceName = "MyRefName";
    item.ValueType = ConfigurationItem.ConfigValueType.STRING;
    item.DisplayName = "Example Config";
    item.Description = "This is an example configuration key";
    item.Value = "example";
 
    Repository.Add(item);
    Repository.CompleteUnitOfWork();

In this example I had an extremely simple model however you can appreciate that there is significantly less leg work required to get a LightSpeed solution up and running. I will post more advanced real world examples to help you gain more from working with LightSpeed in the near future. Also please leave any comments or questions you have on my blog regarding LightSpeed.

How can you use LightSpeed?
We currently are in late beta with an RTM of LightSpeed just around the corner. You can grab the download of LightSpeed from the Mindscape EAP site. You can also post in the forums to discuss any challenges that you have or to ask questions. We welcome any feedback.

Update: LightSpeed was released and is now available to play with here: http://www.mindscape.co.nz/products/LightSpeed/

Hope that helps,

– JD

Average Rating: 4.8 out of 5 based on 297 user reviews.


7 June 2007 in .Net & Code | Comments (31)

Last night I had the opportunity to speak to the Christchurch .NET User Group about web standards and took the opportunity to add some discussion about testing using a tool called WatiN (pronounced “what-in”, horrible I know). From the feedback I’ve received so far the WatiN demo captured everyone’s imagination. I had a great time as well, very friendly folks in Christchurch :)

WatiN effectively allows you to script interactions with Internet Explorer and do things like fill out forms, click through and test the results that come back. This makes testing things such as a process flow really easy to do in a repeatable manner and included in your unit tests which ideally are being executed as part of your continuous integration processes.

Examples:

The example I demonstrated last night was just doing a simple test of the BackgroundMotion search engine. You can probably understand what is going on just from the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
    [Test]
    public void TestSearchWithNoResult()
    {
      IE ie = new IE();
      ie.GoTo("http://backgroundmotion/");
      ie.TextField(Find.ById("ctl00_SearchBox")).Click();
      ie.TextField(Find.ById("ctl00_SearchBox")).TypeText("beach");
      ie.Button("ctl00_Search").Click();
 
      Assert.IsTrue(ie.ContainsText("No results found"));
 
      ie.Close();
    }

Automated Validation is a hot topic for me as I really hate working on a project and then remembering to validate well after I wrote the HTML only to find I have a lot of work ahead of me. I wrote the following test code to allow automated posting of web content to the W3C validation service for checking:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    private static bool IsValidPage(string url)
    {
      string rawHtml = GetFile(url);
      IE ie = new IE("http://validator.w3.org/fragment-upload.html");
      ie.GoTo("http://validator.w3.org/fragment-upload.html");
      ie.TextField(Find.ByName("fragment")).Click();
      ie.TextField(Find.ByName("fragment")).Value = rawHtml;
      ie.Button(Find.ByValue("Validate this document")).Click();
 
      return ie.ContainsText("Is Valid");
    }
 
    public static string GetFile(string url)
    {
      WebRequest myWebRequest = WebRequest.Create(url);
      WebResponse myWebResponse = myWebRequest.GetResponse();
      Stream ReceiveStream = myWebResponse.GetResponseStream();
      Encoding encode = Encoding.GetEncoding("utf-8");
      StreamReader readStream = new StreamReader(ReceiveStream, encode);
 
      string response = readStream.ReadToEnd();
 
      readStream.Close();
      myWebResponse.Close();
 
      return response;
    }

With these two methods you can do write a test per page to validate the content:

1
2
3
4
5
    [Test]
    public void TestValidHTML()
    {
      Assert.IsTrue(IsValidPage("http://www.mysite.com/mypage.aspx"));
    }

Note that this code doesn’t take into account subtle changes with the use of AJAX, it simply gets the raw content from the request. WatiN does however support handling AJAX changes quite nicely so upgrading to support a richer website would not present much of an issue. The interesting thing with the raw request is we could change the agent-type on the request to ensure that we’re getting valid HTML for various browsers despite being called from one central location. This can be great if you’re working with ASP.NET where the server controls will be rendered differently based on the agent type by default.

The code also doesn’t take account of potential network connectivity issues and was more for showing the sort of things that you might want to do with WatiN. WatiN is certainly a useful tool to have in your toolbox. If you want to learn more about WatiN just pull it down and also grab one of the few WatiN recorders as the one listed below and get a kick start in scripting your site interactions.

The Tools

WatiN: http://sourceforge.net/projects/watin/

WatiN Recorder: http://watintestrecord.sourceforge.net/

Hope that helps,

- JD

Average Rating: 4.4 out of 5 based on 182 user reviews.