Spring.Net, Feb 2008, PostSharp: A Technical Introduction

15
PostSharp & Spring.NET PostSharp & Spring.NET 2. PostSharp Architecture

description

 

Transcript of Spring.Net, Feb 2008, PostSharp: A Technical Introduction

Page 1: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

PostSharp & PostSharp & Spring.NETSpring.NET

2. PostSharp Architecture

Page 2: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

you are now supposed to be woken up!you are now supposed to be woken up!

Page 3: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

however, you should already know that!however, you should already know that!

Page 4: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

The Big PictureThe Big Picture

Plug-InsPlug-Ins

PostSharp CorePostSharp Core

PostSharp LaosPostSharp Laos

HostsHosts

MSBuild HostMSBuild Host Command-Line Host

Command-Line Host

Runtime Host

Page 5: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

A finer pictureA finer picture

PostSharp CorePostSharp Core

Code Object Modelrepresents a .NET module

Code Object Modelrepresents a .NET module

Module Readerreads a .NET binary assembly into the Code Object Model

Module Readerreads a .NET binary assembly into the Code Object Model

Module Writerwrites the Code Object Model

into a binary assembly (using ILASM)

Module Writerwrites the Code Object Model

into a binary assembly (using ILASM)

Code Weaverprovides the infrastructure

for MSIL injection

Code Weaverprovides the infrastructure

for MSIL injection

Platform Infrastructure

executes projects, loads plug-ins,loads and schedules tasks

Platform Infrastructure

executes projects, loads plug-ins,loads and schedules tasks

Plug-inperforms specific analysis

or transformation

Plug-inperforms specific analysis

or transformation

Hostbootstraps PostSharp andinvoke project executions

Hostbootstraps PostSharp andinvoke project executions

Independent Programuses PostSharp without

the Platform Infrastructure

Independent Programuses PostSharp without

the Platform Infrastructure

PostSharp LaosPostSharp4EntLib

PostSharp.Samples.Tracecustom plug-ins

PostSharp LaosPostSharp4EntLib

PostSharp.Samples.Tracecustom plug-ins

command-line utilityMSBuild task

PostSharp.Samples.Hostcustom application servers

command-line utilityMSBuild task

PostSharp.Samples.Hostcustom application servers

PostSharp.Samples.ExplorerPostSharp.Samples.Explorer

Page 6: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

Core vs. LaosCore vs. Laos

PostSharp Core PostSharp Laos

Objective Absolutely free editing of .NET assemblies

Aspect-Oriented Programming Easily

Primitives Low-Level (MSIL) High-Level

Audience ISV, MSIL/PostSharp specialist

Everyone

Learning Curve Steep High

Complexity / Error Proneness

High Low

Runtime performance Fast Slower

Licensing of extensions

GPL/Commercial Any

Page 7: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

Hello,World

Compared Example: LoggingBoring but instructive

Page 8: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

have you done your homework?have you done your homework?

Page 9: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

The Laos SolutionThe Laos Solution

Derive a custom attribute.Implement “event handlers”.Apply/multicast the custom attribute.Compile-time instantiation.Deserialization.Reflector.

Page 10: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

The Core SolutionThe Core Solution

Define the Public Interface.◦Custom Attribute in the example, but anything

else is allowed (XML, DSL).Implement the weaver.

◦Advice.◦Advice Provider (pointcut evaluation).

Plug-In Manifest.Discovery.Reflector.

Page 11: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

Compound AspectsImplement INotifyPropertyChanged.

Page 12: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

Translating the implementation Translating the implementation pattern to AOPpattern to AOP

Aspected Class

Property 1Property 1

Property 2Property 2

Property 3Property 3

NotifyPropertyChangedImpl

internal OnPropertyChangedinternal OnPropertyChanged

event PropertyChangedevent PropertyChanged

Property Setter Aspect

OnSuccess handlerOnSuccess handler

Composition Aspect

Page 13: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

Putting it together: Putting it together: Compound AspectCompound Aspect

True meta-programming at compile-time.No limitation to custom attributes.The base of PostSharp bridges to Microsoft

Enterprise Library or Microsoft Entity Framework.

Page 14: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction

SummarySummary

With PostSharp Core, you are completely free but you have to pay the price.

PostSharp Laos allows to do pretty much things, but:◦It is slower: it generates suboptimal instructions.◦No division of compile-time and run-time logic.◦Attention to abuses!

Page 15: Spring.Net, Feb 2008, PostSharp:  A Technical Introduction