2011-06-27 - AOP - .NET User Group Rhein Neckar

2,662 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,662
On SlideShare
0
From Embeds
0
Number of Embeds
820
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

2011-06-27 - AOP - .NET User Group Rhein Neckar

  1. 1. AspektOrientierteProgrammierungmit .NET<br />Eine praktische Einführung<br />27.06.2011<br />Dipl.-Inf. (FH) Johannes Hoppe<br />
  2. 2. Johannes Hoppe<br />ASP.NET MVC Webentwickler<br />www.johanneshoppe.de<br />
  3. 3. 01<br />Architektur und Patterns<br />
  4. 4. Patterns<br />Entwurfsmuster <br />==<br />unser täglicher Job<br />
  5. 5. Beispiel<br />
  6. 6. Aber…<br />
  7. 7. Business Code<br />publicclassCustomerProcesses<br />{<br />publicvoid RentBook( int bookId, int customerId )<br /> {<br />Book book = Book.GetById( bookId );<br />Customer customer = Customer.GetById( customerId );<br /> <br /> book.RentedTo = customer;<br /> customer.AccountLines.Add(string.Format( "Rental of book {0}.", book ), book.RentalPrice );<br /> customer.Balance -= book.RentalPrice;<br /> }<br />}<br />
  8. 8. Business Code<br />+ Logging<br />internalclassCustomerProcesses<br />{<br />privatestaticreadonlyTraceSource trace =<br />newTraceSource( typeof (CustomerProcesses).FullName );<br /> <br />publicvoid RentBook( int bookId, int customerId )<br /> {<br /> trace.TraceInformation(<br />"Entering CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId ); try{ <br /> Book book = Book.GetById( bookId );<br />Customer customer = Customer.GetById( customerId );<br /> <br /> book.RentedTo = customer;<br /> customer.AccountLines.Add( string.Format( "Rental of book {0}.", book ), book.RentalPrice );<br /> customer.Balance -= book.RentalPrice;<br /> trace.TraceInformation(<br /> "Leaving CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId );<br /> }<br /> catch ( Exception e )<br /> {<br /> trace.TraceEvent( TraceEventType.Error, 0,<br /> "Exception: CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} ) failed : {2}",<br /> bookId, customerId, e.Message );<br /> throw;<br /> }  <br /> }<br />}<br />
  9. 9. Business Code<br />internalclassCustomerProcesses<br />{<br />privatestaticreadonlyTraceSource trace =<br />newTraceSource(typeof(CustomerProcesses).FullName);<br /> <br />publicvoid RentBook(int bookId, int customerId)<br /> {<br />if (bookId <= 0) thrownewArgumentOutOfRangeException("bookId");<br />if (customerId <= 0) thrownewArgumentOutOfRangeException("customerId");<br /> <br /> trace.TraceInformation(<br />"Entering CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId);<br /> <br />try<br /> {<br />Book book = Book.GetById(bookId);<br />Customer customer = Customer.GetById(customerId);<br /> <br /> book.RentedTo = customer;<br /> customer.AccountLines.Add(string.Format("Rental of book {0}.", book), book.RentalPrice);<br /> customer.Balance -= book.RentalPrice;<br /> <br /> trace.TraceInformation(<br />"Leaving CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )“, bookId, customerId);<br /> }<br />catch (Exception e)<br /> {<br /> trace.TraceEvent(TraceEventType.Error, 0,<br /> "Exception: CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} ) failed : {2}",<br /> bookId, customerId, e.Message);<br />throw;<br /> }<br /> }<br />}<br />+ Logging<br />+ Vorbedingungen<br />
  10. 10. Business Code<br />+ Logging+ Transaktionen<br />+ Vorbedingungen<br />internalclassCustomerProcesses<br />{<br />privatestaticreadonlyTraceSource trace =<br />newTraceSource(typeof(CustomerProcesses).FullName);<br /> <br />publicvoid RentBook(int bookId, int customerId)<br /> {<br />if (bookId <= 0) thrownewArgumentOutOfRangeException("bookId");<br />if (customerId <= 0) thrownewArgumentOutOfRangeException("customerId");<br /> <br /> trace.TraceInformation(<br />"Entering CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )“, bookId, customerId);<br /> <br />try<br /> {<br />for (int i = 0; ; i++)<br /> {<br />try<br /> {<br />using (var ts = newTransactionScope())<br /> {<br />Book book = Book.GetById(bookId);<br />Customer customer = Customer.GetById(customerId);<br /> <br /> book.RentedTo = customer;<br /> customer.AccountLines.Add(string.Format("Rental of book {0}.", book), book.RentalPrice);<br /> customer.Balance -= book.RentalPrice;<br /> <br /> ts.Complete();<br /> }<br /> <br />break;<br /> }<br />catch (TransactionConflictException)<br /> {<br />if (i < 3)<br />continue;<br />else<br />throw;<br /> }<br /> }<br /> <br /> trace.TraceInformation(<br />"Leaving CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId);<br /> }<br />catch (Exception e)<br /> {<br /> trace.TraceEvent(TraceEventType.Error, 0,<br />"Exception: CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} ) failed : {2}",<br /> bookId, customerId, e.Message);<br />throw;<br /> }<br /> }<br />}<br />
  11. 11. Business Code<br />+ Logging+ Transaktionen<br />+ Vorbedingungen + Exception Handling<br />internalclassCustomerProcesses<br />{<br />privatestaticreadonlyTraceSource trace =<br />newTraceSource(typeof(CustomerProcesses).FullName);<br /> <br />publicvoid RentBook(int bookId, int customerId)<br /> {<br />if (bookId <= 0) thrownewArgumentOutOfRangeException("bookId");<br />if (customerId <= 0) thrownewArgumentOutOfRangeException("customerId");<br /> <br />try<br /> {<br /> trace.TraceInformation(<br />"Entering CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId );<br /> <br />try<br /> {<br />for ( int i = 0;; i++ )<br /> {<br />try<br /> {<br />using ( var ts = newTransactionScope() )<br /> {<br />Book book = Book.GetById( bookId );<br />Customer customer = Customer.GetById( customerId );<br /> <br /> book.RentedTo = customer;<br /> customer.AccountLines.Add( string.Format( "Rental of book {0}.", book ), book.RentalPrice );<br /> customer.Balance -= book.RentalPrice;<br /> <br /> ts.Complete();<br /> }<br /> <br />break;<br /> }<br />catch ( TransactionConflictException )<br /> {<br />if ( i < 3 )<br />continue;<br />else<br />throw;<br /> }<br /> }<br /> <br /> trace.TraceInformation(<br />"Leaving CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} )",<br /> bookId, customerId );<br /> }<br />catch ( Exception e )<br /> {<br /> trace.TraceEvent( TraceEventType.Error, 0,<br />"Exception: CustomerProcesses.CreateCustomer( bookId = {0}, customerId = {1} ) failed : {2}",<br /> bookId, customerId, e.Message );<br />throw;<br /> }<br /> }<br />catch ( Exception e )<br /> {<br />if (ExceptionManager.Handle(e)) throw;<br /> }<br /> }<br />}<br />
  12. 12. Business Code<br />+ Logging+ Transaktionen<br />+ Vorbedingungen + Exception Handling<br />+ Feature X<br />+ Feature Y<br />+ Feature Z<br />+ …<br />
  13. 13. Kern-funktionalitäten<br />(Core Concerns)<br />SeperationofConcerns<br />
  14. 14. VS<br />
  15. 15. VS<br />Nicht-Funktionale Anforderungen(CrosscuttingConcerns)<br />
  16. 16. Cross-CuttingConcerns<br />Data Binding<br />Thread Sync<br />Caching<br />Validation<br />…<br />Security<br />Exception Handling<br />Tracing<br />Monitoring<br />Transaction<br />
  17. 17. OOP<br />OOP+ AOP<br />
  18. 18. Spring.NET<br />Castle<br />MS Unity<br />PostSharp<br />LinFu<br />Build-Time<br />Run-Time<br />Hybrid<br />
  19. 19. Erfolgt zur Laufzeit<br />Code bleibt unverändert<br />Aufruf wird über Proxy umgeleitet<br />Zur Laufzeit keine Änderungen<br />Aufruf wird über Proxyumgeleitet<br />Aspekte zur Laufzeitkonfigurierbar<br />Erfolgt bei Kompilierung<br />Code wird direkt verändert<br />Keine Interfaces erforderlich<br />Zur Laufzeit keine Änderungen<br />Auch auf Properties, Felder,Events anwendbar<br />Kann Member und Interfaces hinzufügen<br />Build-Time: “Statisch”<br />Run-Time: “Dynamisch”<br />
  20. 20. 02<br />Live Coding<br />
  21. 21.
  22. 22. Logging<br />LogTimeAspect<br />webnoteaop.codeplex.com<br />
  23. 23. Exceptions<br />ConvertExceptionAspect<br />webnoteaop.codeplex.com<br />
  24. 24. Validierung<br />ValidationGuardAspect<br />webnoteaop.codeplex.com<br />
  25. 25. Caching<br />SimpleCacheAspect<br />webnoteaop.codeplex.com<br />
  26. 26.
  27. 27. 03<br />AOP 1 x 1<br />
  28. 28. AspectJ Begriffe<br />Join Point<br />Pointcut<br />Advice<br />Aspect<br />
  29. 29. AspectJ Begriffe<br />Join Point<br />Pointcut<br />Advice<br />Aspect<br />
  30. 30. IL Code Vorher<br />[LogTimeAspect]<br />publicActionResult Index()<br />{<br />IEnumerable<NoteWithCategories> notes = <br />this.WebNoteService.ReadAll();<br />return View(notes);<br />}<br />
  31. 31. IL Code Nachher<br />publicActionResult Index()<br />{<br />ActionResult CS$1$2__returnValue;<br />MethodExecutionArgs CS$0$3__aspectArgs =<br />new MethodExecutionArgs(null, null);<br /> <>z__Aspects.a68.OnEntry(CS$0$3__aspectArgs);<br />try<br /> {<br />IEnumerable<NoteWithCategories> notes =<br /> this.WebNoteService.ReadAll();<br />ActionResult CS$1$0000 = base.View(notes);<br /> CS$1$2__returnValue = CS$1$0000;<br /> }<br />finally<br /> {<br /> <>z__Aspects.a68.OnExit(CS$0$3__aspectArgs);<br /> }<br />return CS$1$2__returnValue;<br />}<br />
  32. 32. OriginaleMethode<br />Aspect Klasse<br />OnEntry<br />try{}<br />catch (Exception e){}finally{}<br />Method Body<br />OnSuccess<br />OnException<br />OnExit<br />: OnMethodBoundaryAspect<br />
  33. 33. Links<br />Spring.NET<br />springframework.net<br />Castle<br />castleproject.org<br />Unity<br />unity.codeplex.com<br />PostSharp<br />sharpcrafters.com<br />Demo Download<br />webnoteaop.codeplex.com<br />
  34. 34. FRAGEN?<br />
  35. 35. Vielen Dank!<br />Johannes Hoppe<br />
  36. 36. Aspect- Typen<br />MethodBoundaryAspect<br />OnEntry<br />OnSuccess<br />OnException<br />OnExit<br />OnExceptionAspect<br />OnException<br />MethodInterceptionAspect<br />OnInvoke<br />LocationInterceptionAspect<br />OnGetValue<br />OnSetValue<br />EventInterceptionAspect<br />OnAddHandler<br />OnRemoveHandler<br />OnInvokeHandler<br />MethodImplementationAspect<br />OnInvoke<br />CompositionAspect<br />CreateImplementationObject<br />
  37. 37. Bildnachweise<br />Ausgewählter Ordner © Spectral-Design – Fotolia.com<br />Warnhinweis-Schild © Sascha Tiebel – Fotolia.com<br />Liste abhaken © Dirk Schumann – Fotolia.com<br />3D rendering of an architecture model 2 © Franck Boston – Fotolia.com<br />Healthcare © ArtmannWitte – Fotolia.com<br />Stressed businessman © Selecstock – Fotolia.com<br />
  38. 38. Farben Primär<br />
  39. 39. Farben Code<br />

×