Home > .NET Domain Model, Developing Using CodeFluent Entities > Lightweight Entities, Views and Auto-Lightweight

Lightweight Entities, Views and Auto-Lightweight


In the previous post we saw how to leverage CodeFluent Entities’ DataSet built-in feature to load a custom set of columns from database. That’s practical, but what’s even more practical is to have an actual strongly-typed class to which I can data bind my UI controls.

Going back to my model we’re going to add a lightweight entity. As the name suggests it’s a light entity: it’s not persistent, it has no CRUD methods (e.g. load, save, delete) nor properties by default, and it does not implement any interfaces other than the ICodeFluentLightEntity by default.

<Address>
<Id typeName=”int” />
<Street />
<CityName />
</Address>

<CityAddress lightweight=”true”>
<CityName />
</CityAddress>

Using the Business Object Model producer, my lightweight entity will result in a single class (no collection class will be generated) and only contains the declared properties – CityName in our case – and a ReadRecord method which knows how to fill an instance of the class from a System.Data.IDataReader.

Alright so all we  have to do is add a raw method – lightweight entities only support raw methods since no table equivalent gets generated– which retrieves our desired data:

<CityAddress lightweight=”true”>
<CityName persistentName=”Address_CityName” />
<cf:method name=”LoadAllCities” body=”load()raw”>
SELECT $Address::CityName$ FROM $Address$
</cf:method>
</CityAddress>

As you can see, we also set persistentName attribute on the CityName property to match the Address.CityName property so the generated ReadRecord parses the result properly. Then in our application, we can now use the generated method:

List<CityAddress> addresses = CityAddress.LoadAllCities();
foreach (CityAddress address in addresses)
{
Console.WriteLine("Name: " + address.CityName);
}

Looks better than using DataSets as in this post right?

The last point I want to talk about is the autoLightweight attribute which you can define on a persistent view: doing so does automatically all what we did here! This time, instead of declaring a new lightweight entity, we’re going to define a view on our Address entity and a method using our view:

<Address>
<Id typeName=”int” />
<Street />
<CityName />

<cf:view name=”AddressCityView” autoLightweight=”true”>
<CityName />
</cf:view>

<cf:method name=”LoadCities” body=”load from AddressCityView” />
</Address>

Now this will generate an AddressCityView lightweight, and add a LoadCities method in the AddressCollection class which returns a list of AddressCityView instances. Lets update the code of our application to use this new method:

List<AddressCityView> cities = AddressCollection.LoadCities();
foreach (AddressCityView city in cities)
{
Console.WriteLine("Name: " + city.CityName);
}

And voilà! Now I’ve got a strongly-typed object to which I’ll easily be able to data bind my UI, behind the scenes it uses a persistent view, and all that without coding a single line of code!! Neat isn’t it?!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s