Code Generation with Roslyn

Über dieses Buch

Learn how Roslyn's new code generation capability will let you write software that is more concise, runs faster, and is easier to maintain. You will learn from real-world business applications to create better software by letting the computer write its own code based on your business logic already defined in lookup tables.Code Generation with Rosyln is the first book to cover this new capability. You will learn how these techniques can be used to simplify systems integration so that if one system already defines business logic through lookup tables, you can integrate a new system and share business logic by allowing the new system to write its own business logic based on already existing table-based business logic.One of the many benefits you will discover is that Roslyn uses an innovative approach to compiler design, opening up the inner workings of the compiler process. You will learn how to see the syntax tree that Roslyn is building as it compiles your code. Additionally, you will learn to feed it your own syntax tree that you create on the fly.What You Will Learn:• Structure logic to be stored in database design• Build complex conditional logic based on lookup data in the database• Compile code that you generate programmatically• Discover generated code and run it dynamically to implement new business logic• Debug problems in generated code• Deploy and access generated codeWho This Book Is For:Back end developers in very dynamic fast-paced business environments. Developers focused on integrating different systems across an enterprise should also find this information useful.

di</div>

Anzeige

Inhaltsverzeichnis

Frontmatter

You have probably already figured this out, but business logic is hard. It is rarely logical, often doesn't follow discernable patterns, is riddled with exceptions, and changes often and quickly. This can be frustrating but it is the world we live in.

You can structure your business logic in a variation or combination of the tables discussed. You have a lot of flexibility here. You just need to understand the business that you are working in and have some creativity.

Roslyn is the new compiler from Microsoft. It represents not only a rewrite of the compilers but also a reimagining of the compilation process in general. In the past, compiling code was a black box. Lines of code went in and a compiled assembly came out, or perhaps a slew of error messages. You could not interrupt this process to get access to any of the intermediate steps. All this changed with Roslyn. Figure 4-1 shows the steps Roslyn takes to compile code and identifies the APIs used to interact with the results at each stage.

So far we have explored the benefits of moving business logic from application code to reference data in the database. You saw how hand-coding business rules in your application logic can lead to brittle code that’s not resilient to change. At the very least, this code can leave you less responsive to changes in the marketplace, which could leave you out of the marketplace all together. We then explored various strategies for storing different types of business rules in variations on a standard decision table.

So far, we have simply written the generated code to the screen, but this is hardly the only option. Remember Roslyn is the compiler. When you have a generated SyntaxTree, you are already partway through compilation.

Generated code will always be code that was not known at compile time. The whole point of generating code is to make it easier to change the business rules after deployment. This is where reflection comes in. Reflection allows you to access metadata about your code at runtime. With it, you can load an assembly, discover the types in the assembly, create an instance of these types, and systematically execute any method discovered in these types. This is exactly what you need to evaluate new business logic.

We have covered a lot of material in this book. Along the way, we touched on various best practices. Some were expressly called out as tips and notes. Others were stated but a bit more subtly. In this last chapter, we review these best practices as a handy reference.