Extending Sublogix Entities

If you’ve followed my posts on Sublogix, or even better, given Sublogix a spin, you’ll know that if you’re doing SalesLogix development Sublogix can save you a ton of time and give you a much easier to use data access and entity model. This post will show you how to extend the Sublogix entities to give you even easier access and save even more time.

The Sublogix Entity Model

Before we look at extending Sublogix entities, let’s take a look at the standard generated Sublogix entities. The Sublogix library has a built-in entity model for the standard out of the box SalesLogix database. There are T4 templates you can add to your project to generate the entity model based on your own custom SalesLogix database schema. Either way, you have and entity class for each table in the database. If you’ve used teh T4 templates, you can find a file in your project called SublogixEntityObjects.cs which contains all of these classes and you can take a look. There’s nothing special about these classes. The important part of these objects are the attributes they’re decorated with that tells Sublogix how the classes and properties relate to the SalesLogix database tables and fields.

Extending A SubLogix Entity

Let’s look at a sample of extending the Account entity. Let’s add a property to get the related Contacts for the account. To do this, we simply need to inherit the Account entity as our base class, then add soem new custom properties.

First, let’s create a new class, we’ll call it AccountExtended. Then we’ll have it inherit from account. At this point we have the following:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { public class AccountExtended : Sublogix.Entities.Account { } }

There is one crucial thing missing from this class. The attributes that normally decorate a Sublogix entity. You can see these by looking at the Account class in the SublogixEntityObjects.cs file that was generated by the T4 templates. Once we add that our class will look like this:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="ACCOUNT", PrimaryKey="ACCOUNTID")] public class AccountExtended : Sublogix.Entities.Account { } }

Now we can add whatever custom properties to the class we need. One thing to note, the base Account class has a built-in repository. So, to add the property to get the related child Contacts, we just need to add the following:

using System;
using System.Collections.Generic;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="ACCOUNT", PrimaryKey="ACCOUNTID")] public class AccountExtended : Sublogix.Entities.Account { public IList<Contact> Contacts { get { return this.Repository.Find<Contact>(x => x.AccountId == this.AccountId); } } } }

Pretty simple. Now we could just use account.Contacts to get a list of the Contacts related to the account. Often, when I do this sort of thing, I store the retrieved items locally in a private variable to avoid going back to the database to retrieve them. For example, this sample extends the OpportunityProduct entity to provide the related Product entity built-in. The difference here is that I only retrieve it once, then store the reference so next time I ask for it I don’t make a trip back to the database for it.

using System;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="OPPORTUNITY_PRODUCT", PrimaryKey="OPPPRODUCTID")] public class OpportunityProductExtended : Sublogix.Entities.OpportunityProduct { private Product _product; public Product Product { get { if (_product == null) _product = this.Repository.GetById<Product>(this.ProductId); return _product; } } } }

One of the cool things about doing this is that if you’re binding the Sublogix entity to a datagrid, you can create properties for related items so you can easily bind as you normally would since the values from related tables are all built in. Also, it goes without saying that you can certainly do a lot more than just retrieving related entities. You could do any sort of calculations or complex things there too, of course. Here’s one that extends the Opportunity entity to add a calculated property to get the total price of all related opportunity products.

using System;
using System.Linq;
using System.Linq.Expressions;
using Sublogix.Model;
using Sublogix.Entities;
namespace SublogixDemo.Entities { [EntityElement(Name="OPPORTUNITY", PrimaryKey="OPPORTUNITYID")] public class OpportunityExtended : Sublogix.Entities.Opportunity { public double? OpportunityProductTotal { get { return this.Repository.Find<OpportunityProduct>(x => x.OpportunityId == this.OpportunityId).Sum(x => x.Price); } } } }

The sky is the limit. If you need to add any writable properties, that is, a property with a “set” as well as a “get”, you’ll need to tell Sublogix to ignore that property, otherwise it will try to match that up with a SalesLogix database field with the same name. To tell Sublogix to ignore that property, simply add the EntityElement attribute with an Ignore flag.

[EntityElement(Ignore = true)]
public string MyCustomProperty { get; set; }

Using Extended Sublogix Entities

To use an extended Sublogix entity, you just replace the standard entity name with your extended one.

So, instead of doing this:

var accountList = repo.Find<Account>(x => x.AccountName.StartsWith('Ab'));

You just do this to use your extended entity (Notice the change from Account to AccountExtended):

var accountList = repo.Find<AccountExtended>(x => x.AccountName.StartsWith('Ab'));

Pretty easy. Are you drinking the Sublogix koolaid yet?


Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

1 Comment

  1. Hi John,

    Yes, you absolutely can create a custom Save method. The Save method is implemented in the entity itself. In fact, you can see the existing code for what Save does in the generated code file SublogixEntityObjects.cs (which is created from the templates).

    Basically, all the Save method does is check to see if the entity exists already and then calls either repository.Add or repository.Update. That is it. So, if I were going to create my own custom Save method, I would start by just copying the Save method from the generated class in SublogixEntityObjects.cs and put that in your custom entity, then add whatever else you’d need.

    Make sense?


Submit a Comment

Your email address will not be published.

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) and Creatio (bpm'online) news and product updates!

You have Successfully Subscribed!