...
The manager template is for helping you get started with business logic for the LINQ entities. The managers will have common queries that are created from keys and indexes on the table. The manager will also have rules for the entity properties to make sure required fields are not null and that the length of a string does not exceed the max length the column allows.
The template works by creating a second partial class that has a Manager property. The manager will then have a property for each entity that has a manager. Here is a sample of the syntax for using the managers:
Code Block | ||||
---|---|---|---|---|
| ||||
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);
|
The manager will add business rules to your entities based on metadata attributes. These rules are based on the same rules found in the DataAnnotations.
Code Block | ||||
---|---|---|---|---|
| ||||
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 to your entities. In addition to the default validation rules that are generated, you can add custom rules by implementing the AddRules partial method in the custom entity class.
Code Block | ||||
---|---|---|---|---|
| ||||
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" }));
}
|
Properties on the Managers.cst template:
Property | Description |
---|---|
1.Database |
|
SourceDatabase | The source database to keys and indexes from for generating the manager classes. |
2.Class |
|
Framework | The version of the .Net Framework that is being used. |
3.Mapping |
|
DbmlFile | The path to the dbml file used generate the manager classes from. |
4.Manager |
|
DataContextFile | The location of the DataContext File. |
DataContextName | The class name of the DataContext that supports the managers. |
ManagerDirectory | The folder to save the generated manager files. |
ManagerNamespace | The namespace to use for the generated manager class files. |
MethodPrefix | The prefix of query method names. For example, 'GetBy'. |
MethodKeySuffix | The suffix of the primary key query method names. For example, 'Key'. |