We’ve released the second CTP for LLBLGen Pro v5.0! Since the first CTP which was released back in March, we’ve been hard at work to implement features we wanted for v5.0. It’s taken a bit longer than expected as the main feature, Derived Models (more on that below), turned out to be a bigger feature than we initially thought and it affected more aspects of the designer than anticipated at first. Nevertheless, I’m very happy with the result, as it turned out even better than I imagined.
This CTP is the last one before beta and RTM. It’s open for all V4 licensees and comes with its own temporary license which expires on December 31st, 2015. Beta and RTM are expected in Q1 2016. To download, log into our website and go to ‘My Account->Downloads->4.2->Betas’.
So what’s new since CTP 1? I’ve described the highlights below. Of course the usual pack of bug fixes and improvements across the board are also included in this CTP.
Ever since v4 I wanted to do more with the designer, to do more with the entity model a user defines using it: we have all this model information contained in a single place and there should be more ways to leverage that than just for a mapping model to use with an ORM. The idea started when I looked into usage patterns again where ORM models are used. Nowadays most people define a different model in their application which is used to transport data from the ORM to the UI, e.g. across service boundaries or inside an MVC model class. This model is often hand-written, and tied to the entity classes using hand-written queries or Automapper mapping files. In other words: a hand-written chain of dependencies stored in multiple places which can break at any moment without noticing.
Another use case of models defined on top of an entity model is with document databases. Web applications nowadays aren’t just monolithic stacks with one database and one runtime, they’re built as a group of technologies working together in harmony: a UI on the client in JS, a backend targeting multiple databases using multiple paradigms, etc. It’s more and more common to store non-volatile data in de-normalized form inside a document database for faster querying, and update that data on a regular basis, at runtime. This data is e.g. retrieved from the relational database which is used with the ORM and entity classes.
For these two scenarios I’ve developed the Derived Models feature: Derived Models are models of hierarchical elements defined on top of the entity model. They define an optionally de-normalized form of the underlying abstract entity model. This all might sound complicated, but it’s actually very straightforward.
Let’s see a picture of the editor to get started. (click the picture below for a bigger version)
For the picture, I’ve picked a complicated scenario as it illustrates most of what you can do with the editor. On the far left of the designer you see the Project Explorer which is the overview of the elements in your project. The project loaded has an entity model with entities in various inheritance hierarchies, and a Derived Model called Docs. The Derived Model uses the target framework Document Database. We’ll get to that in a minute.
The editor shown in the screenshot shows the derived element BoardMember, which is derived from the entity BoardMember. On the left of the editor you see the entity hierarchy in tree form: you can navigate from the root entity BoardMember to related entities infinitely. On the right you’ll see the shape of the derived element BoardMember. When you check a checkbox on the left, it’s included in the shape on the right.
That’s of course not all. You can also de-normalize fields. In the example above, you’ll see in the shape of the derived element a de-normalized field, namely BoardMember.ManagesDepartmentName. This field is de-normalized from BoardMember.ManagesDepartment, as you can see on the left. De-normalizing is as simple as selecting the fields and clicking a button. The shape shown above just takes a couple of seconds to create, as the editor is smart enough to e.g. add all fields of a related element if you check the checkbox of a navigator. We of course built in tooling to get these derived elements added quickly to derived models and e.g. you can add fields in bulk and de-normalize fields in bulk.
The advantage of this is that if something changes in your entity model, the derived model changes too. Be it a type of a field that gets changed, a field that gets removed, a relationship that changes… the designer picks this up and keeps your Derived Models in sync.
You can define as many Derived Models as you want, each with different derived elements and target frameworks. They’re for all supported ORMs, so they work equally for Entity Framework, NHibernate, Linq to Sql and our own LLBLGen Pro Runtime Framework.
“Models…. I need code!”
Now that we have a Derived Model with elements that derive from our abstract entity model, we can do things with it, like generate code. There are two different code bases generated per Derived Model: a set of DTO / Document classes (which represent the derived elements in the Derived Model) and a set of persistence methods to fetch the DTO / Document classes using the ORM used in the Entity Model.
With v5.0 we’ll ship with two target frameworks to be used with a Derived Model: DTO class model and Document Database. The DTO class model offers (through presets) a choice between read-only DTOs and read/write DTOs. Read-only DTOs offers Linq projections for the DTO classes generated, the Read/write DTOs also offer projections back to the root entity of the derived element so it can be persisted into the relational database using the ORM of choice.
The Document Database framework offers a choice (through presets) between RavenDB, MongoDB and ‘Generic Document Database’. The last one can be used with e.g. RethinkDB and Microsoft’s DocumentDB. This means that you can define your models in the designer, then decide how you want to use your derived models (e.g. as DTOs or for a document database), generate code and use them with very little code.
Let’s look at an example of such a DTO / Document class. Here we have a Customer derived element which is derived from the Customer entity which has a value-typed field named ‘Address’. This is the typical Northwind Customer. As you can see the fields contain attributes. These attributes are added to the fields based on rules. E.g. you can define a rule (as I did here) where all string-typed fields will get a StringLength attribute with the length of the field. You define this once per model (so the rule is used for all string fields), and can override it at the field level if you want to. This requires very little work. The ID field here is CustomerID and it’s marked with the KeyAttribute automatically. We’ll add one line of code later on to tell RavenDB how to determine the key, and that’s it.
Additionally to this class and its nested types, the designer also generates a projection extension method and, in the case of Read/Write DTOs, it will generate methods to project the DTO back to an entity. For this example I’ve chosen to store the data as documents in a RavenDB database so only the projection extension method is generated:
With these two classes I can now fetch Customer instances using any Linq query which produces Customer entities. The classes are set-up to be usable with RavenDB too, so I can just go ahead and fetch some entities and store them as Customer instances in RavenDB: This is the only code I have to write, the code above is generated, you don’t have to write that.
This works with Entity Framework, NHibernate and Linq to Sql as well: the only thing that changes is the way the query is fetched: the projection method, it’s the same. For the DTO class model the projection methods are the same as well. And as the Projection method returns an IQueryable, you can append other elements too, like ordering or additional where clauses, Distinct etc.
The shown derived model class and query above is of course rather simple, yet it illustrates the important aspects: the ties between your derived models and your entity model aren’t obscurely defined in several files without any connection but in the central place where you also define your entity model and more importantly: when the entity model changes the derived model will change with it, together with the projection methods.
Of course, the normal things like copy/paste are also supported for Derived Models. For instance if you copy a Derived Model from a project in one designer instance to an empty project in another designer instance, you’ll get the entities the model is derived from, and optionally the mappings and target table definitions brought in as well. All that with just two keystrokes.
So what else is new?
The LLBLGen Pro code generator is actually a hierarchical task executor where the tasks happen to be individual code generation tasks. Which tasks to execute and with which parameters is defined using Presets. The user picks a preset and executes the set of tasks defined by the preset with a click of a button. Sometimes you want to alter the parameters, or add an additional task to the set of tasks. In previous iterations we had a UI interface with complex controls to edit the tree, which was time consuming to use. It turned out if you write the preset in the XML editor (a preset is defined in an XML file) using some intellisense it was much faster. So we created a new preset viewer which allows the user to view and copy it, start edit operations etc. on presets and pair that with the already available text editor in the designer:
We used this system to write the Derived Element presets and templates and it felt much better than the previous system. ‘Dog-fooding’ it ourselves was a great way to see whether we were on the right track so our users won’t run into snags.
Code generation configuration overhaul
In previous versions when you started a code generation action you’d generate only code for the entity model. Now that we had multiple models inside the designer, we needed a new way to define what to generate code for. So we did:
The new system allows the user to select for which models to generate code and configure them individually.
There are more smaller things added and changed, like a new interface for the command line code generator for batch processes, and we’ll add more smaller features before beta/RTM. I hope you like what we have added and it meets what you’re looking for!