Managers template

The Managers.cst template gives you a starting point for the business logic of your LINQ entities. Each manager has common queries built from the table's keys and indexes. It also has rules for the entity's properties, which make sure required fields aren't null and strings don't exceed the column's maximum length.

The template creates a second partial class of the DataContext with a Manager property. The manager has a property for each entity that has a manager:

SampleDataContext db = new SampleDataContext();
// use the primary key
Task task = db.Manager.Task.GetByKey(taskId);
// use a foreign key
var myTasks = db.Manager.Task.GetByAssignedID(userId);
// the methods return IQueryable so you can add expressions
var openTasks = db.Manager.Task.GetByStatusID(statusId).OrderBy(t => t.CreateDate);

Business rules

The manager adds business rules to your entities based on metadata attributes. The rules are the same as the rules in System.ComponentModel.DataAnnotations.

private class Metadata
{
    // Only Attributes in the class will be preserved.
    public int OrderId { get; set; }

    [Required]
    [StringLength(20)]
    public string UserId { get; set; }

    [Range(typeof(DateTime), "10-31-2008", "12-25-2200")]
    public System.DateTime OrderDate { get; set; }

    // ...
}

The manager also provides a business rules engine for your entities. Besides the validation rules it generates, you can add custom rules by implementing the AddRules partial method in the custom entity class:

static partial void AddRules()
{
    // Rule allows the Name property to be a max of 150 characters.
    RuleManager.AddShared<Task>(new LengthRule("Name", 150));
    // Rule that validates the value of the property using regex.
    RuleManager.AddShared<Task>(new RegexRule("Name", ".*"));
    // Rule allows only users in certain security roles to update.
    RuleManager.AddShared<Task>(new UpdateRule(new string[] { "Administrator", "Updaters" }));
}

For more about rules, see Using the rules engine.

Properties

Category Property Description
1. Database SourceDatabase The database whose keys and indexes the manager classes are generated from.
2. Class Framework The .NET Framework version you generate for.
2. Mapping DbmlFile The path of the dbml file to generate the manager classes from.
3. Manager DataContextFile The path of the DataContext file to add the managers to.
DataManagerName The name of the data manager class.
ManagerDirectory The folder for the generated manager files.
ManagerNamespace The namespace of the generated manager classes.
MethodPrefix The prefix of query method names, for example GetBy.
MethodKeySuffix The suffix of primary key query method names, for example Key.