1. Trang chủ
  2. » Công Nghệ Thông Tin

aop in .net

298 2,6K 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 298
Dung lượng 8,8 MB

Nội dung

MANNING Matthew D. Groves FOREWORD BY Phil Haack Practical Aspect-Oriented Programming www.it-ebooks.info AOP in .NET www.it-ebooks.info www.it-ebooks.info AOP in .NET PRACTICAL ASPECT-ORIENTED PROGRAMMING MATTHEW D. GROVES MANNING SHELTER ISLAND www.it-ebooks.info For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 261 Shelter Island, NY 11964 Email: orders@manning.com ©2013 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. Development editors: Frank Pohlmann, Cynthia Kane 20 Baldwin Road Technical proofreader: Javier Lozano PO Box 261 Copyeditor: Nancy Kotary Shelter Island, NY 11964 Proofreader: Elizabeth Martin Typesetter: Dottie Marsico Cover designer: Marija Tudor ISBN 9781617291142 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – MAL – 18 17 16 15 14 13 www.it-ebooks.info To my children Matthew and Emma I will never grow tired of your yelling, “Daddy, Daddy!” and tackling me when you hear the creak of my office door. www.it-ebooks.info www.it-ebooks.info vii brief contents PART 1 GETTING STARTED WITH AOP 1 1 ■ Introducing AOP 3 2 ■ Acme Car Rental 21 PART 2 THE FUNDAMENTALSOF AOP 53 3 ■ Call this instead: intercepting methods 55 4 ■ Before and after: boundary aspects 79 5 ■ Get this instead: intercepting locations 115 6 ■ Unit testing aspects 141 PART 3 ADVANCED AOP CONCEPTS . 169 7 ■ AOP implementation types 171 8 ■ Using AOP as an architectural tool 191 9 ■ Aspect composition: example and execution 213 www.it-ebooks.info www.it-ebooks.info ix contents foreword xiii preface xv acknowledgments xvii about this book xix PART 1 GETTING STARTED WITH AOP 1 1 Introducing AOP 3 1.1 What is AOP? 4 Features 4 ■ Benefits 8 ■ AOP in your daily life 13 1.2 Hello, World 14 1.3 Summary 19 2 Acme Car Rental 21 2.1 Start a new project 22 Business requirements 23 ■ Necessary nonfunctional requirements 24 2.2 Life without AOP 24 Write the business logic 25 ■ Testing the business logic 28 Add logging 29 ■ Introducing defensive programming 31 Working with transactions and retries 32 ■ Handling exceptions 35 ■ Refactor without AOP 38 www.it-ebooks.info [...]... Listing 1.4 Using AOP instead of DI for cross-cutting concerns public class InvoiceService { IInvoiceData _invoicedb; InvoiceService(IInvoiceData invoicedb) { _invoicedb = invoicedb; } [TransactionAspect] void CreateInvoice(ShoppingCart cart) { _invoicedb.CreateNewInvoice(); foreach (item in cart) _invoicedb.AddItem(item); } Still only one service is being passed in CreateInvoice doesn’t contain any... example using AOP in NET I’ll break apart that example, identifying the individual puzzle pieces and explaining how they fit together into something called an aspect 3 www.it-ebooks.info 4 1.1 CHAPTER 1 Introducing AOP What is AOP? AOP is a relatively young concept in computer science Like many advancements in modern computing—including the mouse, IPV6, the graphical user interface (GUI), and Ethernet AOP. .. pseudocode in the following www.it-ebooks.info 10 CHAPTER 1 Listing 1.2 Introducing AOP Example of refactoring using DI instead of AOP public class InvoiceService { ITransactionManagementService _transaction; IInvoiceData _invoicedb; InvoiceService(IInvoiceData invoicedb, ITransactionManagementService transaction) { _invoicedb = invoicedb; _transaction = transaction; } void CreateInvoice(ShoppingCart... course in using AOP You’ll code the business logic for Acme Car Rental Company, add cross-cutting concerns without AOP, and then explore refactoring it to use AOP www.it-ebooks.info www.it-ebooks.info Introducing AOP This chapter covers  A brief history of AOP  What problems AOP was created to solve  Writing a very simple aspect using PostSharp In this first chapter, I’ll start in an obvious place—introducing... defining a pointcut, I need to define a join point A join point is a place that can be defined between logical steps of the execution of your program Imagine your program as a low-level flowchart, as shown in figure 1.1 Any gap in that flowchart could be described as a join point, as in figure 1.2 Now that you know what a join point is, I can define a pointcut A pointcut is a set of join points (or an... and constraints with INotifyPropertyChanged 132 Reducing boilerplate with AOP 134 5.4 6 Summary 138 Unit testing aspects 6.1 141 Writing tests with NUnit 142 Writing and running NUnit tests for aspects 145 6.2 Castle DynamicProxy testing Testing an interceptor 147 6.3 ■ 142 Testing strategies ■ 147 Injecting dependencies 149 PostSharp testing 156 Unit testing a PostSharp aspect 157 Injecting dependencies... the exit join points in that short snippet Suppose I want to insert advice (some piece of code) only on the exit join points of NameService objects My pointcut could be expressed in English as “exiting a method of NameService.” How to express that pointcut in code (if it can be expressed at all) is dependent on the AOP tool you’re using In reality, just because I can define a join point in English doesn’t... this instead: intercepting locations 115 5.1 Location interception 116 Fields and properties in NET interception 118 5.2 116 PostSharp location ■ Real-world example: lazy loading 119 Lazy loading approaches in NET 120 Implementing lazy loading with AOP 121 What about lazy-loading fields? 124 ■ ■ 5.3 Real-world example: INotifyPropertyChanged 128 Using INotifyPropertyChanged in a desktop application... to include you, I’m sorry; I owe you lunch Dan Allen, for giving me my first programming job Michael Kramer, for that fateful day when he unwittingly unleashed AOP into my life Everyone I’ve worked with at OSU, Quick Solutions, and Telligent Xiaoran Wang, for the tremendous diagrams (explaining tangling, scattering, and weaving) that he was kind enough to let me use in this book Vince Fabro, for being... _transaction.Commit(); } } www.it-ebooks.info The transaction Start is moved to OnEntry in an aspect The transaction End is moved to OnExit in an aspect 12 CHAPTER 1 Introducing AOP Note that AOP has at no point completely replaced DI (nor should it) InvoiceService is still using DI to get the IInvoiceData instance, which is critical to performing the business logic and isn’t a cross-cutting concern But ITransactionManagementService . code, making it harder to read and maintain. By chance, I was attending a .NET conference in Ohio, and Michael Kramer, an acquaintance of mine, was giving an introductory talk on AOP using PostSharp lazy loading 119 Lazy loading approaches in .NET 120 ■ Implementing lazy loading with AOP 121 ■ What about lazy-loading fields? 124 5.3 Real-world example: INotifyPropertyChanged 128 Using INotifyPropertyChanged. within code. In AOP in .NET, Matthew D. Groves deftly shines a light on the many ways develop- ers can take advantage of aspects in .NET. He begins with an approachable introduc- tion to AOP

Ngày đăng: 28/04/2014, 15:41

Xem thêm

TỪ KHÓA LIÊN QUAN

w