You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+92-17Lines changed: 92 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,15 +2,15 @@
2
2
3
3
# Projection Tools
4
4
5
-
This package provides two primitives `Projection<TSource, TResult>` and `Specification<TSource>`for building reusable LINQ projections and predicates.
5
+
This package provides primitives for building reusable LINQ projections and specifications.
6
6
7
7
Package is available on [Nuget](https://www.nuget.org/packages/ProjectionTools/).
8
8
9
-
Install using dotnet cli:
9
+
Install using dotnet CLI:
10
10
```commandline
11
11
dotnet add package ProjectionTools
12
12
```
13
-
Install using package-manager console:
13
+
Install using Package-Manager console:
14
14
```commandline
15
15
PM> Install-Package ProjectionTools
16
16
```
@@ -21,24 +21,67 @@ My initial goal was to replace packages like AutoMapper and similar.
21
21
22
22
The common drawbacks of using mappers:
23
23
24
-
-Code "black hole" and dirty magic: IDE can not show code usages, mappings are resolved in runtime;
25
-
-Complex API: API is complex yet limited in many cases;
26
-
- Maintenance costs: authors often change APIs without considering other options;
24
+
- IDE can not show code usages, mappings are resolved in runtime (sometimes source generators are used);
25
+
- API is complex yet limited in many cases;
26
+
- Maintenance costs: authors frequently change APIs without considering other options;
27
27
- Do not properly separate instance API (mapping object instances) and expression API (mapping through LINQ projections) which leads to bugs in runtime;
28
28
- Bugs: despite all the claims you can not be sure in anything unless you manually test mapping of each field and each scenario (instance/LINQ);
29
-
- Poor testing experience;
30
-
- Compatibility with LINQ providers: AutoMapper has broken compatibility with EF6 for no reason at all;
29
+
- Poor testing experience: sometimes you have to create your own "tools" specifically for testing mappings;
30
+
- Compatibility with LINQ providers: e.g. AutoMapper has broken compatibility with EF6 for no reason at all;
31
31
32
-
In the most cases mapping splits into two independent stages:
32
+
In the most cases mapping splits into two independent scenarios:
33
33
34
-
- Fetch DTOs directly from DB using automatic projections and pass result to client;
35
-
- Map incoming DTOs to entities to apply changes from client and then save modified entities to DB;
34
+
1. Fetch DTOs from DB using automatic projections;
35
+
2.DTOs to entities and then save modified entities to DB;
36
36
37
-
In reality mapping from DTO to entity is rarely a good idea: there are validations, access rights, business logic. It means that you end up using custom code in each case.
37
+
In reality direct mapping from DTO to entity is rarely viable: there are validations, access rights, business logic. It means that you end up writing custom code for each save operation.
38
38
39
-
`Projection<TSource, TResult>` - provides option to define mapping from entity to DTO.
39
+
In case we want to support only 1st scenario there is no need to deal with complex mapper configurations.
40
40
41
-
Quick example, controller should return only active users and users should have only active departments:
41
+
`Projection<TSource, TResult>` - provides an option to define reusable mappings.
42
+
43
+
You can create projection using mapping expression:
Thanks to `DelegateDecompiler` package and built-in ability to compile expression trees all of the options above will work but with different performance implications.
83
+
84
+
Full example, controller should return only active users and users should have only active departments:
42
85
43
86
```csharp
44
87
publicclassUserEntity
@@ -129,17 +172,49 @@ public class UserController : Controller
129
172
130
173
## Specifications (reusable predicates)
131
174
132
-
Projection works but we have a problem: we do not reuse `Where(x => x.Active)` checks. There is one predicate in `UserController.GetUser` method and another in `UserDtoProjection`.
175
+
Projections work but we have a problem: we do not reuse `Where(x => x.Active)` checks. There is one predicate in `UserController.GetUser` method and another in `UserDtoProjection`.
133
176
134
-
This predicate can be more complex, often it is a combination of different predicates depending on business logic.
177
+
This predicates can be more complex, often a combination of different predicates depending on business logic.
135
178
136
179
There is a well-known specification pattern and there are many existing .NET implementations but they all share similar problems:
137
180
138
181
- Verbose syntax for declaration and usage;
139
182
- Many intrusive extensions methods that pollute project code;
140
183
- Can only be used in certain contexts;
141
184
142
-
This is how we can use `Specification<TSource>` to solve these problems:
185
+
`Specification<TSource>` can solve these problems.
0 commit comments