pro dlr in .net 4

329 2.8K 0
pro dlr in .net 4

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Books for professionals By professionals ® Pro DLR in .NET 4 Dear Reader, Microsoft’s Dynamic Language Runtime (DLR) is a platform for running dynamic languages such as Ruby and Python on an equal footing with com- piled languages such as C#. Furthermore, the runtime is the foundation for many useful software design and architecture techniques you can apply as you develop your .NET applications. Pro DLR in .NET 4 introduces you to the DLR, showing how you can use it to write software that combines dynamic and static languages, letting you choose the right tool for the job. You will learn the core DLR components such as LINQ expressions, call sites, binders, and dynamic objects—and how they work together to achieve language interoperability. You’ll see how to mix and match objects and functions from compiled and dynamic languages, so you can write code in the language of your choice while taking advantage of librar- ies written in other languages. And you'll discover how the various languages interoperate behind the scenes. With the basics out of the way, the book then details the various ways you can leverage the DLR in the design and architecture of your software applica- tions. You’ll learn about runtime code generation, which lets you avoid much of the boilerplate code typical in layered business applications. You’ll see practical examples of using the DLR to build domain-specific languages, and you’ll learn how the DLR helps enable aspect-oriented programming. I’m very excited about the DLR and the various ways it can be used in a developer’s day-to-day work. Pro DLR in .NET 4 will help you master the DLR, and I believe you’ll gain a competitive edge in your career as a result. Sincerely Chaur Wu Shelve in: .NET User level: Intermediate–Advanced THE APRESS ROADMAP Pro DLR in .NET 4 Pro C# and the .NET 4 Platform Introducing Visual C# 2010 www.apress.com SOURCE CODE ONLINE Companion eBook Wu DLR in .NET 4 Companion eBook Available Pro The eXperT’s Voice ® in .neT Pro DLR in .NET 4 Chaur Wu Integrate dynamic languages such as Python and Ruby into your .NET applications on an equal footing with C# www.it-ebooks.info www.it-ebooks.info Pro DLR in .NET 4          Chaur Wu www.it-ebooks.info Pro DLR in .NET 4 Copyright © 2010 by Chaur Wu All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3066-3 ISBN-13 (electronic): 978-1-4302-3067-0 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Jonathan Gennick Technical Reviewer: Scott Isaacs Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jennifer L. Blackwell Copy Editor: Sharon Terdeman Compositor: Bytheway Publishing Services Indexer: Brenda Miller Artist: Integra Software Services Pvt. Ltd. Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. www.it-ebooks.info I w ant to dedicate this book to Sarah, Everett, Cedric, and Chiachi www.it-ebooks.info  CONTENTS AT A GLANCE iv Contents at a Glance  About the Author xiv  About the Technical Reviewer xv  Acknowledgments xvi  Introduction xvii PART 1    DLR Fundamentals 1  Chapter 1: Introduction to DLR 3  Chapter 2: DLR Expression 27  Chapter 3: Late Binding and Caching 65  Chapter 4: Late Binding and Interoperability 87  Chapter 5: Dynamic Objects 109  Chapter 6: DLR Hosting API 133 PART 2    Applying the DLR 163  Chapter 7: DLR and Aspect-Oriented Programming 165  Chapter 8: Metaprogramming 185  Chapter 9: Stitch — A DSL for Hosting Languages 211  Chapter 10: Application Scripting 239  Chapter 11: DLR in Silverlight 251  Chapter 12: Dynamic Languages on JVM 275  Index 297 www.it-ebooks.info  CONTENTS v Contents  About the Author xiv  About the Technical Reviewer xv  Acknowledgments xvi  Introduction xvii PART 1    DLR Fundamentals 1  Chapter 1: Introduction to DLR 3 Setting Up Code Examples 4 Software Requirements 4 Installing the DLR, IronPython, and IronRuby 5 Hello World Examples 6 Implementing REPL for the Hello Language 9 Practical Uses for the DLR 9 Application Scripting and DSL 10 XML Builder 10 Aspect-Oriented Programming 11 Runtime 13 Runtime vs. Run Time 14 Run Time vs. Compile Time 14 Dynamic vs. Static 16 Dynamic Typing 17 Dynamic Dispatch 20 Language 21 Programming Languages in Practice 22 www.it-ebooks.info  CONTENTS vi Putting It Together 24 Summary 25  Chapter 2: DLR Expression 27 DLR Expression as a Language 27 Code as Data 28 A Common Denominator like CLR 28 Concrete Syntax and Serialization 29 Expressions vs. Statements 29 Expression Type and Kind 31 Binary Expressions 33 Flow Control Expressions 36 If-Then-Else Expressions 37 Switch Expressions 38 Scopes and Name Binding 39 Lexical vs. Dynamic Scope 40 BlockExpression and Lexical Scoping 41 Lambda Expressions and Closure 44 The GotoExpression Class 46 While Loops 48 Dynamic Expressions 49 Index Expressions 52 Expression Abstraction, Reduction and Extension 53 Immutability and the Visitor Pattern 55 Visitor Pattern in General 56 Visitor Pattern in DLR Expression 57 Expression Visitor Examples 59 Summary 63 www.it-ebooks.info  CONTENTS vii  Chapter 3: Late Binding and Caching 65 Binding 65 Call Sites and Early Binding 66 Call Sites and Late Binding 67 DLR Binders 68 Set Up Code Examples 69 Making a Debug Build of the DLR for .NET 2.0 69 Developing for Both .NET 2.0 and .NET 4.0 69 The CallSiteBinder Class 71 DLR Call Sites 72 Binding Restrictions and Rules 73 Checking Binding Rules in Debug Mode 75 Caching 78 Three Cache Levels 78 Late-Binding Context 80 L0 Cache Example 82 L1 Cache Example 83 L2 Cache Example 84 Creating Canonical Binders 86 Summary 86  Chapter 4: Late Binding and Interoperability 87 Language Interoperability 87 Static and Dynamic Objects 91 Late-Binding Logic in Two Places 92 Late Binding Logic in Binders 92 Late-Binding Logic in Dynamic Objects 94 Late-Bound Actions 94 www.it-ebooks.info  CONTENTS viii Examples 94 Common Type System 96 Class Hierarchy of Binders 99 Implement a Custom Binder Class 100 Interoperability Protocol 102 Summary 106  Chapter 5: Dynamic Objects 109 What is a Dynamic Object? 109 IDynamicMetaObjectProvider Interface 111 Dynamic Meta Objects 112 DynamicMetaObject and Binding Logic 113 DynamicMetaObject and Binding Result 114 Interoperability 115 DynamicObject Class 119 XML Builder 122 Summary 130  Chapter 6: DLR Hosting API 133 Life Without the DLR Hosting API 133 Using a Static Language’s Code in Another Static Language 134 Using a Static Language’s Code in a Dynamic Language 134 Using a Dynamic Language’s Code in a Static Language 135 Using a Dynamic Language’s Code in Another Dynamic Language 136 Overview of the DLR Hosting API 136 Major Classes in the API 137 The Tale of Two APIs 138 The DLR Hosting API in Relation to Binders and Dynamic Objects 139 Using Script Runtime to Execute Code 139 www.it-ebooks.info [...]... Listing 1 -4 shows the C# code for the application that hosts the Hello language Line 3 in Listing 1 -4 reads the configuration information from App.config and creates an instance of the ScriptRuntime class Given the information in App.config, line 4 is able to call our Hello language context behind the scenes and return an instance of ScriptEngine The code in line 5 passes in “any text” as the input... C:\Program Files (x86)\IronRuby 1.0v4\bin to C:\ProDLR\lib \DLR\ release: • IronRuby.dll • IronRuby.Libraries.dll • Microsoft.Dynamic.dll (This and the next assembly are the DLR version 1.0 binaries) • Microsoft.Scripting.dll 5 www.it-ebooks.info CHAPTER 1  INTRODUCTION TO DLR 4 Copy the following files from C:\Program Files (x86)\IronPython 2.6 for NET 4. 0\ to C:\ProDLR\lib \DLR\ release: • IronPython.dll... using the DLR xvii www.it-ebooks.info  INTRODUCTION Part II explores applying the DLR to various ends You’ll find chapters devoted to such topics as aspect-oriented programming, application scripting, domain-specific languages, metaprogramming, and more Part I consists of six chapters Chapter 1 stands out in that it gives you a whirlwind tour — code included! — showing all you can accomplish using... use the building of a programming language as a Hello World example After all, building a programming language is no trivial task But, as you will see, because of the rich features the DLR provides, we can do all the things mentioned with very little code The series of Hello World examples is about using, embedding, and building programming languages But the DLR also does a good job of enabling application... paradigm that is very well-suited to solving the problem of cross-cutting concerns Common cross-cutting concerns in a software system are things like transaction management, security, auditing, performance monitoring, logging, and tracing, and the like By virtue of addressing the problem of cross-cutting concerns in an elegant manner, AOP provides tremendous value in the design and architecture of software... begins with the runtime loading the binaries into memory If the binaries are compiled from C++ code, they will contain machine code and the C++ runtime will directly execute the machine code If the binaries are compiled from C# or VB.NET, the binaries will contain NET IL and the CLR will at its discretion interpret the IL or JIT (just -in- time) compile the IL to machine code 01101 1001 generated binaries... haven’t heard of the DLR, you may be wondering whether it’s worth your time learning it One reason people might regard the DLR as irrelevant to their work is that they think the DLR is for implementing new languages And since most of us write programs to solve specific problems and very few of us implement languages, learning the DLR may not seem like a good investment That was in fact my initial misconception... Software Requirements 240 Application Architecture 241 Application Object Model . 241 Application Scripting 244 The Physics Engine 246 xi www.it-ebooks.info  CONTENTS User Interface 248 Summary .250  Chapter 11: DLR in Silverlight 251 Different Client Side Web Scripting Approaches 251 Apache HTTP Server... Configuring the Languages You Want to Speak 140 Configuring Script Runtime Declaratively 141 Configuring Script Runtime Programmatically 142 Scripting an Object Model . 143 Script Scopes . 145 Value and Reference Variables 146 Global Scope and Variable Passing 147 Language Neutral Scope and Variable Passing 148 Level... classes in the System.Linq.Expressions namespace inside the System.Core.dll assembly The DLR AST in Figure 1-8 serves as input to the DLR expression compiler, which is implemented in the class Microsoft.Scripting.Ast.LambdaCompiler Compiling expressions is not an easy task Instead of doing all the work of compiling expressions by itself, LambdaCompiler leverages classes in the Microsoft.Scripting.Generation . ROADMAP Pro DLR in .NET 4 Pro C# and the .NET 4 Platform Introducing Visual C# 2010 www.apress.com SOURCE CODE ONLINE Companion eBook Wu DLR in .NET 4 Companion eBook Available Pro The eXperT’s. Voice ® in .neT Pro DLR in .NET 4 Chaur Wu Integrate dynamic languages such as Python and Ruby into your .NET applications on an equal footing with C# www.it-ebooks.info www.it-ebooks.info Pro. and Early Binding 66 Call Sites and Late Binding 67 DLR Binders 68 Set Up Code Examples 69 Making a Debug Build of the DLR for .NET 2.0 69 Developing for Both .NET 2.0 and .NET 4. 0 69 The

Ngày đăng: 24/04/2014, 15:47

Từ khóa liên quan

Mục lục

  • Prelim

  • Contents at a Glance

  • Contents

  • About the Author

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

  • DLR Fundamentals

    • Introduction to DLR

      • Setting Up Code Examples

        • Software Requirements

        • Installing the DLR, IronPython, and IronRuby

        • Hello World Examples

          • Embedding the Hello Language

          • Implementing REPL for the Hello Language

          • Practical Uses for the DLR

            • Application Scripting and DSL

            • XML Builder

            • Aspect-Oriented Programming

            • Runtime

              • Runtime vs. Run Time

              • Run Time vs. Compile Time

              • Dynamic vs. Static

                • Dynamic Typing

                • Dynamic Dispatch

                • Language

                • Programming Languages in Practice

Tài liệu cùng người dùng

Tài liệu liên quan