-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use the new Signum.MSBuildTask and Signum.Analyzer nugets
- Loading branch information
1 parent
bac25bd
commit c99d4da
Showing
6 changed files
with
17 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c99d4da
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New
[AutoExpressionField]
(and[ExpressionField("auto")]
removed)Writing methods and properties as expression tree instead of normal IL code is necessary so they can be translated to SQL.
Signum Framework uses this very often:
ToString
methods that can be executed in the database (for retrievingLite<T>
efficiently) usingtoStringExpression
snippetexpressionMethodQuery
.expressionProperty
orexpressionMethod
).The old
[ExpressionField]
Since C# has not direct support for writing expression methods and properties, we have typically done like this:
This pattern has been usefull for many yars, but has some problems:
@this
inf the expression field.Still, thanks to the snippets, was relatively easy to write expression properties and method, but the code looked bloated anyway, making it hared to read.
After some years, a roslyn analyzer was created in Signum.Analyzer to produce a compile time error when the two declarations get out of sync.
Additionally, some code in Signum.MSBuildTask was introduced to parse the body of the methods or properties decorated with
[ExpressionField]
(aka: `[ExpressionField("auto")]) and extract the name of the field, writing it automatically it in the attribute argument. This somehow fixed the overload problem with a little bit more of complexity :S.The end result is a lot of complication for something that should be easier.
The new
[AutoExpressionField]
Here is how you write the same now:
Awesome right? Just decorate the property of method with
[AutoExpressionField]
and surround your expression withAs.Expression(() => )
. It almost looks like direct C# support for it!.This is how
AutoExpressionField
andAs.Expression
are implemented:... and this took 10 years to be invented? Well the real magic happens in Signum.MSBuildTaks, that is able to read the IL in the body method and transform it, so when you write:
it converts it to something like:
Note how is able to:
GetNameExpression
static field with the implicitProductEntity this
argument and thebool full
.GetNameInit
method that will be called onece in the static constructor.As.Expression
, and move it toGetNameInit
. This is the most complicated part because it requires parsing the IL that constructs the expression tree and modify it, because what before where closures to the parameters in the top-most method (GetName
) now have to be converted in direct parameters of theExpression<Func<...>>
.GetName
with a call toGetNameExpression.Evaluate
.AutoExpressionFieldAttribute
with aExpressionFieldAttribute("GetNameExpression")
.That's what took 10 years to be invented!
Changes in snippets
The snippets that you're used to (
expressionToString
,expressionProperty
,expressionMethod
,expressionMethodQuery
) are still there, now producing the new simpler code.Changes Signum.Analyzer
Also, Signum.Analyzer is vigilant so you don't forget using
As.Expression(()=> )
in the body of a member decorated ithAutoExpressionField
and suggests to add it when not.So you can write just write
When you add
[AutoExpressionField]
you get a compile-time error, then use the Quick Fix and you get:How to change the code
While the new
AutoExpressionField
is lowered to the oldExpressionField
, the part of the MSBuildTask that was replacing[ExpressionField]
by [ExpressionField("YourExpression"]` now seem redundant, so I've removed it.This means
AutoExpressionField
is a breaking change and enjoying it is non-optional :).Update Framework and Extensions, and update the Nugets of your projects, specifically the Signum ones. If you get the downgrade error in VS edit manually the .csproj.
Then apply this renames in your .Entities and .Logic assemblies.
Note: Manual fixes may be needed after the regex is executed if the parameters of the expression field and the member (method or property) are different, or when 'this' is involved.
Hope the transition is easy and you can enjoy the new syntax soon.
Cheers!
c99d4da
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome. 😀