Home » Blog

rounded header

Archive for August, 2010

LightSpeed 3.11 Released!

tag icon Tagged as LightSpeed, News

Mindscape LightSpeed 3.11 for Workgroups

The bestest .NET ORM just got more awesomer!

We’re stoked to have just released LightSpeed 3.11! Originally going to be 3.2 but we thought we’d pay homage to one of the greats in software history :-)

So, fresh from the oven, what’s new? Here’s a high level list of the improvements:

  • Support for SQL CE 4 both in the core and designer (meaning currently the LightSpeed model designer is about the only visual tool for designing a SQL CE 4 schema!)
  • Support for user-defined runtime conversions between database and CLR types, including designer support
  • Super sparkle keyboard support in the designer for users who love hot keyboard action
  • Improvements to querying, particularly in nested queries and complex grouping statements
  • Improvements to handling of inheritance structures both in the core and designer
  • Serialization of entities improved for remoting situations
  • Improved support for Visual Basic oddities with LINQ queries
  • Improved model checking and messaging in the designer to make it easier for new users
  • Usability and visual improvements for linked model files which is great for large models.
  • Added UpdateEntityState for manually setting entity state for testing purposes (in the Mindscape.LightSpeed.Testing namespace)
  • Improvements to the samples – better DTO sample, updated SQLite version in samples, and more
  • Refresh on data providers to new versions of the SQLite, PostgreSQL, MySQL and VistaDB
  • Batch support for SimpleDB users to improve insert speed by a lowly 2500%

Read the complete change log here.

If you’ve never used LightSpeed, grab the free edition and have a play – we’re sure you’ll enjoy it and you’ll be up in running in about 10 minutes (start the timer!). If you’re already a customer you can grab the 3.11 release from your account page – we strongly recommend upgrading.

SUMMER PROMOTION SALE
We’re offering a staggering 50% off if you purchase LightSpeed before Friday this week – that’s a saving of $174.50 USD! Head to the store, grab the LightSpeed 3 – 1 Developer edition (RRP $349 USD) and use the following discount code:

ISEETHELIGHT

Just make sure you use it by Friday! Grab a copy and see why you’ll never want to use a different ORM again! :-)

LightSpeed and VistaDB 4.1

tag icon Tagged as LightSpeed

As VistaDB users will be aware, VistaDB 4.1 was released towards the end of July, and we’ve had a few questions asking when we were going to switch over.

We’ve been holding off because we’re not sure how quickly people have been migrating to 4.1, but with the next LightSpeed release coming rapidly down the tracks, we’ll be making the switch as of tonight’s nightly build (dated 24 August, and available from about 1500 GMT).

If you’re a VistaDB 4.1 user, please pull down the new nightly when it’s available and let us know if you run into any problems!

Ninja data type mappings in LightSpeed

tag icon Tagged as LightSpeed

Mindscape LightSpeed NinjaLightSpeed has traditionally supported a wide variety of .NET primitive types, with built-in mappings to the equivalent database types. Sometimes, however, you’ll want to map a database column to a custom type, or persist a particular field in a special way.

For example, a common idiom in some legacy databases (especially older Oracle databases) is to represent a boolean value by a CHAR(1) column with values like ‘Y’ and ‘N’ or ‘T’ and ‘F’. In your domain model, of course, you don’t want to work with these strings: you want to work with a CLR Boolean.

One way to do this is to set the Generation option to FieldOnly, and write the property accessors by hand, using your preferred type. But this isn’t always an option. For example, it can cause problems with mappings to DTOs; or if you want to model a .NET TimeSpan by a MySQL or SQL Server time column, LightSpeed’s built-in handling of TimeSpans will get in the way. Plus, it means you have to create a partial class and write several whole lines of code for every property that needs the custom mapping. Bo-ring! Don’t you guys know Starcraft 2 is out?

Enter ninja data type mappings.

In LightSpeed 3.11 (and the latest LightSpeed 3.1 nightly builds), you can customise how LightSpeed maps database values to CLR values and vice versa. This involves two steps: first, writing some conversion code to define the type mapping, and second, telling LightSpeed to use this mapping for a particular field.

Defining a ninja data type mapping

To define a data type mapping, you need to implement the new IFieldConverter interface. IFieldConverter has two methods, one for each conversion direction. Here’s how an IFieldConverter might look for the Y/N boolean example.

public class YesNoConverter : IFieldConverter
{
  public object ConvertFromDatabase(object databaseValue)
  {
    string s = (string)databaseValue;
    return s == "Y";
  }
 
  public object ConvertToDatabase(object value)
  {
    return ((bool)value) ? "Y" : "N";
  }
}

And that’s it for the mapping. Now we need to tell LightSpeed to use it on our favourite fields. The way you do this depends on whether you’re writing code by hand, or using the designer.

Applying a mapping to a field in code

To apply a mapping to a field in code, use the new ColumnAttribute.ConverterType property, passing the type of your IFieldConverter implementation:

[Column(ConverterType = typeof(YesNoConverter))]
private bool _yesNo;
 
public bool YesNo
{
  get { return _yesNo; }
  set { Set(ref _yesNo, value, "YesNo"); }
}

Voila! Job done.

Applying a mapping to a property in the designer

The designer surfaces ninja data type mappings in a slightly different way. Instead of applying the mapping separately to each property that needs it, you create a user-defined type that bundles up a CLR property type (like boolean), a database column type (like string) and the mapping between them. You can then select that user-defined type just as if it were a built-in type. Let’s take a look.

First, we create the user-defined type. This is done via the LightSpeed Model Explorer (View > Other Windows > LightSpeed Model). Right-click the model node, choose Add New User Defined Type, give it a name by which you’d like to refer to it in the Data Type drop-down, and enter the relevant details:

Let’s quickly run over the entries here:

  • CLR Type Name: This is the type that you want to use in your domain model. Your IFieldConverter.ConvertFromDatabase should be returning this type, and ConvertToDatabase should be expecting this type.
  • Is Value Type: This should be true if the CLR type is a value type, otherwise false (typically if the CLR type is System.String). This is relevant only if you have nullable instances of this type.
  • Name: How you want to refer to it in the Data Type drop-down.
  • Converter Type: The type name of your IFieldConverter implementation. This doesn’t have to be fully-qualified, but if it’s not, then it has to be in the same namespace as your model classes, or in one of the model’s imported namespaces.
  • Data Type: The column type in the database. Your IFieldConverter.ConvertFromDatabase should be expecting this type, and ConvertToDatabase should be returning this type. Here, it’s used when you sync the model and database, so that you don’t get spurious “change column YesNo to Boolean” suggestions.
  • Is Standard Data Type: Set this to false if you need to want the column type to be a database special (like time) rather than one of the LightSpeed built-in data types. You can then enter the database type name instead of the LightSpeed data type.

Okay, now we’ve created our YesNo user-defined type with all the code generation, database synchronisation and runtime mapping information nicely bundled up together, how do we apply it to an entity property? Simple: just select the property and choose the user-defined type from the Data Type drop-down:

And there it is, done. LightSpeed will now generate a boolean property, but map it to a string column. Of course, now we’ve set up the user-defined type, we can apply it to as many properties as we want, which makes this really convenient for mapping patterns that are used widely across a database.

Want to take it for a spin? Grab the latest nightly of the free Express edition from the Downloads page, or update your Professional edition from the store.

Many to many relationships with LightSpeed through associations

tag icon Tagged as LightSpeed

LightSpeed models many-to-many relationships between entities using a feature called through associations. In a through association, each pair of entities is represented by a third entity of a through type, which maps in the database to a through table.

For example, in a media sharing site, a Contribution may have many Tags, and each Tag may be associated with many Contributions. So you’d represent this using a ContributionTag through table, each row of which represents the relationship between one Contribution and one Tag, but at the LightSpeed level you’d like to work directly with the Contribution.Tags and Tag.Contributions collections rather than down at the ContributionTag level.

Using a Through Association

The nice thing about through associations is that you can work with them as easily as with any other collection. For example, you can use the Add and Remove methods to add Tags to or remove Tags from a Contribution:

public void ThatsNoDromedary(int id)
{
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag penguinTag = unitOfWork.Tags.Single(t => t.Value == "penguins");
  Tag zooTag = unitOfWork.Tags.Single(t => t.Value == "zoo");
  Tag dromedaryTag = unitOfWork.Tags.Single(t => t.Value == "dromedaries");
 
  contribution.Tags.Add(penguinTag);
  contribution.Tags.Add(zooTag);
  contribution.Tags.Remove(dromedaryTag);  // it was a mistake anyone could have made
}

You don’t need to worry about the lower level of the through entity or the through table: LightSpeed takes care of all that bookkeeping for you.

ThroughAssociation also supports other common collection methods such as Clear() and Contains():

public void UntagThatDromedary(int id)
{
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag dromedaryTag = unitOfWork.Tags.Single(t => t.Value == "dromedaries");
 
  if (contribution.Tags.Contains(dromedaryTag))
  {
    contribution.Tags.Clear();
  }
}

And just as with EntityCollections it automatically adds newly created items to the unit of work for you:

public void YouMightGetSquished(int id)
{
  Contribution contribution = unitOfWork.FindById<Contribution>(id);
  Tag heffalumpTag = new Tag { Value = "heffalumps" };
  contribution.Tags.Add(heffalumpTag);
 
  unitOfWork.SaveChanges();  // heffalumpTag gets inserted into the database
}

In short, most of the time you can work with a many-to-many ThroughAssociation just as you’d work with a one-to-many EntityCollection.

Defining a Through Association

The easiest way to set up a through association is using the designer. If your through type is ultra-simple — containing nothing but the foreign keys to the entities being related — then you can just draw a through association arrow and tell the designer to create an auto through entity. If you need more control or want to store data against each entity pairing, or if you want to write the classes or associations by hand, you need to know a bit more about how through associations sit over the more fundamental one-to-many and many-to-one associations.

We’ll describe how to do this in the next installment, or for a sneak preview see the documentation.

Nightly news, 20 August 2010

It’s been an enterprisy kind of a week: started out with a meeting, then our Dauntless Leaders went to an industry seminar and to cap it all off we’ve entered the SharePoint market. We’ve even got a pretty thin change log for this week. It doesn’t get much enterprisier than that.

LightSpeed

  • Improved DTO guidance sample
  • Added batch insert support for Amazon SimpleDB
  • Fixed a couple of cases that could cause “unresolved identifier” alias errors in generated SQL

WPF Elements

  • Added controls for single-day display — see the multiple schedule sample here
  • Fixed an issue where TimePicker wasn’t showing the current value

WPF Diagramming

  • Fixed an issue where printing wasn’t respecting node elements being hidden

As always, these fixes and enhancements are in the current nightlies — free and trial editions from the downloads page, retail editions from the store.

Data Products Visual Controls Community Store
LightSpeed ORM
NHibernate Designer
SimpleDB Tools
SharePoint Tools
WPF Elements
WPF Diagrams
Silverlight Elements
Forums
Blog
Register
Login
Subscribe to newsletter
Buy Now
My Account
Volume Discounts
Purchase Orders
Contact Us