12 June 2007 in .Net, Code, Mindscape, Tools | Comments enabled

In my previous post about LightSpeed I discussed setting up a very basic LightSpeed instance but didn’t go into any detail about working with the data once we had configured it. In this post we’ll have a look at some basic ways of performing CRUD actions on our database through LightSpeed.

Creating Objects

To create an object we simply need to create an instance of our model class and assign the properties that we want set. This isn’t complicated and not LightSpeed specific so this isn’t a difficult mechanism to understand.

ConfigurationItem item = new ConfigurationItem();
item.ReferenceName = referenceName;
item.ValueType = type;
item.DisplayName = displayName;
item.Description = description;
item.Value = value;
 
Repository.Add(item);
Repository.CompleteUnitOfWork();

The important part here is at the end where we add the item to the repository and then tell the repository to complete the associated unit of work.

Retrieving Objects

There are many ways to retrieve objects in LightSpeed but I will detail two basic queries here. Retrieving my primary key and retrieving by a single field.

// Find the configuration item that has a primary key of 2
ConfigurationItem configItem = Repository.Find<ConfigurationItem>(2);

Here is a query by the DisplayName property which is just a normal property, not a primary key.

// Create a query to get an entity where the property "DisplayName" is equal to "Example"
QueryExpression query = Entity.Attribute("DisplayName") == "Example";      
 
// Get a collection of items that match that query from the database
IList<ConfigurationItem> items = Repository.Find<ConfigurationItem>(query);

Queries certainly are a breeze when working with LightSpeed. In later posts we will look at more advanced queries that have multiple criteria.

Updating Objects

Updating an object with LightSpeed is an easy operation of just updating one or many properties on your entity and telling the repository to save those changes.

// Retrieve an object
ConfigurationItem configItem= Repository.Find<ConfigurationItem>(2);
 
// Update the display name
configItem.DisplayName = "My Updated Display Name"
 
// Save the changes to the database
Repository.CompleteUnitOfWork();

Deleting Objects

Last but not least we occasionally need to delete data from our database.

// Get a configuration item of primary key 2
ConfigurationItem configItem = Repository.Find<ConfigurationItem>(2);
 
// Mark the entity as deleted
Repository.Remove(configItem);
 
// Save the change, actually commiting the delete to the database
Repository.CompleteUnitOfWork();

Now we have walked through CRUD interactions through LightSpeed.

Hope that helps,

– JD

Average Rating: 4.5 out of 5 based on 247 user reviews.


2 comments. Add your own comment.

Julius Fenata says 6 March 2008 @ 22:17

Hi JD’s,

I’ve trying your sample, but I have this message when I inserted new record to my table. How to resolve this issue?

{“Cannot insert the value NULL into column ‘Id’, table ‘Test.dbo.ConfigurationItem’; column does not allow nulls. INSERT fails. The statement has been terminated.”}

Thx a lot…

JF

traskjd says 17 March 2008 @ 10:52

Hi Julius,

More than likely this is from not having primary key set on your ID column in the database. Without this your database won’t automatically create a value when not supplied.

Alternatively use a KeyTable identity method to have LightSpeed insert a value.

Hope that helps,

John-Daniel Trask

Leave a Comment

Name (required)

E-mail (required - not published)

Website

Your comment: