LightSpeed is a domain modelling tool. It prescribes some simple, predictable rules for expressing your domain model in object-oriented code and in relational database tables; if you follow these rules then LightSpeed takes care of the work of translating between the object and relational worlds, allowing you to focus on doing whatever it is you want to do at the domain level, rather than thinking about data plumbing.
And the great thing about simple, predictable rules is that you can make computers do the grunt work for you.
Imagine being able to design your domain model in the abstract: that is, instead of having to create separate representations of your model in the code notation and the relational notation, imagine being able to design the model using a model notation, and get the computer to translate that into the corresponding object and relational implementations using the LightSpeed rules.
Well, unfortunately, you’re going to have to keep imagining a little longer. But LightSpeed 2 is a small step in that direction. It gives you a way to design your models visually, using a simple graphical notation. As you do so, your models are automatically translated into C# or Visual Basic. Models aren’t currently translated into relational database schemas, because this introduces some additional complexities around versioning and migration, though there is a basic facility for updating your development database, intended to support rapid prototyping. (I’ll say more about this in a later post.)
In order to use the designer, you’ll need to have Visual Studio 2008, Standard Edition or higher, installed. (Sorry, the designer doesn’t currently support VS2005, and Microsoft don’t allow add-ins in the Express editions.) If you’ve got that, install LightSpeed — you can use the free Express edition — making sure that the “LightSpeed Model Designer” option is selected and you’re ready to go.
Start by creating a new C# or Visual Basic project.
Now right-click the project and choose Add New Item. Look under Data, and you’ll see that you now have the option to create a LightSpeed model:
This brings up an empty designer surface and a toolbox of things you can put on there. For most models, the interesting toolbox items are Entity and OneToManyAssociation, though the designer also supports value objects, one-to-one associations and inheritance.
Drag the Entity icon from the toolbox to the designer. Once you’ve created an entity, you can rename it through the Properties window or by double-clicking its title. (You can also use the Properties window to customise the behaviour of the entity, but we won’t discuss that right now.)
To add properties to your entity, right-click it and choose Add New Entity Property. You can edit the name of the property in place. To specify the data type, use the Properties window. You can also edit the name here, and as with entities you can customise the behaviour (again, we won’t go into that now).
Saving the model causes Visual Studio to generate appropriate C# or Visual Basic code:
If your project targets .NET 3.5 or above, the generated code includes a strong-typed unit of work class so that you can use LINQ. The designer also adds references to the LightSpeed (and, if appropriate, LINQ to LightSpeed) assemblies if the project doesn’t already have them.
To create associations between entities, select the OneToManyAssociation tool from the toolbox, and drag it from one entity to another. (Start at the “one” end and drag to the “many” end. For example, if a Contribution has many Comments, but each Comment applies to only one Contribution, you’d drag from Contribution to Comment.) The designer infers appropriate property names for each end of the association, though you can edit these if you want to (and you will need to if you have more than one association between the same two entities).
Notice that you don’t need to create a property to contain the ID of the referenced entity. For example, in the Comment entity above, you don’t have to create a ContributionId property. When the designer generates code for the association, it generates a ContributionId property as well as the Contribution EntityHolder. Notice also that you don’t need to define both the one-to-many association and the reverse many-to-one association. Again, the designer generates code into both the Contribution and Comment classes, handling both ends of the association.
This gives you some idea of how LightSpeed 2 enables you to perform domain modelling in the abstract, with automatic generation of the code.
Q. You and your fancypants domain modelling. Here in the real world, I have a database, and I need to get stuff into and out of it. Can your designer help me with that?
A. Yes. You can drag tables from Server Explorer onto the design surface, and LightSpeed will create appropriate model entities and relationships. And if you subsequently edit the table design in your database tool, LightSpeed can update the model to reflect your changes. (We might venture to suggest that, in this scenario, you’re almost using your database tool as your domain modeller, and using the designer to translate that into the code representation. But that’s just us and our fancypants domain modelling.) Here’s an example:
LightSpeed not only creates entities for the database tables, and properties for the columns, it also infers associations between them based on foreign key relationships. You can now save the model to generate the LightSpeed code. Of course, you can also edit the model further, for example to customise caching or eager loading. Additionally, LightSpeed remembers which database the entities were created from, so that it can keep the abstract model in sync with the database representation. I’ll talk more about this in a future post.
The designer is included with all editions of LightSpeed. If you want to take it for a test drive, download the free LightSpeed Express edition and give it a go!