Skip to content

Commit 5b7b9bb

Browse files
Using projections with the parser
1 parent 43f82de commit 5b7b9bb

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,77 @@ The included Dockerfile-websample builds, packages and runs the web sample proje
118118

119119
docker run -p 80:80 garvincasimir/datatables-aspnet-core-sample:0.0.2
120120

121+
Projections
122+
========================
123+
I recommended always using a projection with the query that is sent to the parser. This strategy has 4 main benefits:
124+
* Avoid inadverently serializing and sending sensitive fields to the client.
125+
* Avoid custom non-database fields in your model
126+
* Inlcude parent table fields
127+
* Include computed fields
128+
129+
Below is an example of a self referencing table:
130+
131+
| EmployeeID | FirstName | LastName | ManagerID | Token | Birthdate |
132+
| ----------- | --------- | -------- | -------- | ----------- | --------- |
133+
| 1 | Mary | Joe | null | s38fjsf8dj | 3/3/1921 |
134+
| 2 | Jane | Jim | 1 | 9fukfdflsl | 2/2/1921 |
135+
| 3 | Rose | Jack | 1 | s9fkf;;d; | 1/1/1931 |
136+
137+
138+
The model class:
139+
140+
```csharp
141+
public class Employee
142+
{
143+
public int EmployeeID {get;set;}
144+
public string FirstName {get;set;}
145+
public string LastName {get;set;}
146+
public int? ManagerID {get;set;}
147+
public Manager Manager {get;set;}
148+
public string Token {get;set;}
149+
public DateTime BirthDate {get;set;}
150+
}
151+
```
152+
153+
Projection class:
154+
155+
```csharp
156+
public class EmployeeResult
157+
{
158+
public int EmployeeID {get;set;}
159+
public string FullName {get;set;}
160+
public int? ManagerID {get;set;}
161+
public string ManagerFullName {get;set;}
162+
public DateTime BirthDate {get;set;}
163+
public string BirthDateFormatted
164+
{
165+
get
166+
{
167+
return String.Format("{0:M/d/yyyy}", BirthDate);
168+
}
169+
}
170+
}
171+
```
172+
Query:
173+
174+
```csharp
175+
var query = from e in context.Employees
176+
let FullName = e.FirstName + " " + e.LastName
177+
let ManagerFullName = e.Manager.FirstName + " " + e.Manager.LastName
178+
select new EmployeeResult
179+
{
180+
EmployeeID = e.EmployeeID,
181+
FullName = FullName,
182+
ManagerID = e.ManagerID,
183+
ManagerFullName = ManagerFullName,
184+
BirthDate = e.BirthDate
185+
};
186+
187+
var parser = new Parser<EmployeeResult>(Request.Form, query);
188+
189+
```
190+
191+
121192
Custom Filter Expressions
122193
========================
123194
The parser builds a set of expressions based on the settings and filter text sent from Datatables. The end result is a *WHERE* clause which looks something like this:

0 commit comments

Comments
 (0)