AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

39
www.exoplatform.com Copyright 2011 eXo Platform IoC & AOP IoC & AOP Vu Cong Thanh Vi Quoc Hanh Social team

description

This presentation made by Vi Quoc Hanh and Vu Cong Thanh in SC Team from eXo Platform SEA.

Transcript of AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

Page 1: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

IoC & AOPIoC & AOP

Vu Cong ThanhVi Quoc Hanh

Social team

Page 2: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

2www.exoplatform.com

Copyright 2011 eXo Platform

AgendaAgenda

IoC - Introduction IoC – Definition & Benefits IoC – Concepts IoC – Design your own IoC. Demo AOP - Introduction AOP - Definition & Benefits AOP - Concepts AOP - How it works? Demo

Page 3: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

Inversion Of Control Inversion Of Control (IoC)(IoC)

Page 4: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

4www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Introduction - “Dependencies”

Page 5: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

5www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Introduction – Dependency Problems

»Code is tightly coupled

»Difficult to isolate when testing

»Difficult to maintain.

Page 6: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

6www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Introduction - WITHOUT IoC

»Construction by Hand.

»Factory Pattern.

Page 7: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

7www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Definition & Benefits

»Hollywood principle “Don't call us, we'll call you.”

»Creating, Assembling and Wiring the dependencies into Object graph.

»Type of Dependency Injector:

– Constructor

– Setter

– Method

– Field(Guice)

Page 8: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

8www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Definition & Benefits

»Pros:

– Loosely Coupled

– Increases Tesability

– Separates component clearly.

– Allows for use of Inversion of Control Container.

»Cons:

– Increases code complexity

– Difficult to understand at First

– Complicate Debugging at First

– Complicates following Code Flow.

Page 9: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

9www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Concepts – Time launcher

»Constructed and prepared before it can be used.

– In the init lifecycle stage of a web application.

– On startup of a desktop program

Page 10: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

10www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Concepts– Time launcher

»Constructed and prepared before it can be used.

– On demand, every time it is needed

– Lazily, when it is first needed

Page 11: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

11www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Concepts – Metadata Configuration

»XML configuration file.

»Annotations(@Injector, @Singleton, …)

»Invoking into a programmatic API.

Page 12: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

12www.exoplatform.com

Copyright 2011 eXo Platform

IoC – Concepts - Scope

Fixed duration of time or method calls in which an object exist. General-purpose scopes: Singleton and No-scope.

»Web Application:

– Request, Session and Conversation scope

– Transaction scope

Page 13: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

13www.exoplatform.com

Copyright 2011 eXo Platform

IoC - Concepts - Benefit of Scope

»Lets injector manage the latent state of your objects

»Ensures that your services get new instances of dependencies as needed

»Implicitly separates state by context.

Page 14: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

14www.exoplatform.com

Copyright 2011 eXo Platform

Design your own IoC

Source Code:

Guice: google-guice.googlecode.com/svn/trunk/

Vbox: https://github.com/thanhvc/etk-vbox

Page 15: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

15www.exoplatform.com

Copyright 2011 eXo Platform

Design your own IoC – Injector Diagram

Page 16: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

16www.exoplatform.com

Copyright 2011 eXo Platform

Design your own IoC – Context Diagram

Page 17: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

17www.exoplatform.com

Copyright 2011 eXo Platform

Design your own IoC – State Diagram

StartConstructing Finish

States: Constructing, Start and Finishhttp://www.slideshare.net/Thuy_Dang/regular-expression-made-by-to-minh-hoang-portal-team

Page 18: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

18www.exoplatform.com

Copyright 2011 eXo Platform

Design your own IoC – State Diagram(Cont)

Page 19: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

DEMODEMO

Page 20: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

Aspect Oriented Aspect Oriented Programming (AOP)Programming (AOP)

Page 21: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

21www.exoplatform.com

Copyright 2011 eXo Platform

AgendaAgenda

What is IOC? IOC – Time of Injector IOC – Pros & Cons IOC - Scope Demo AOP - Introduction AOP - Definition & Benefits AOP - Concepts AOP - How it works? Demo

Page 22: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

22www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION - OOP

»A set of objects that interact with each other

Page 23: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

23www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION - AOP

»View the system as a set of concerns

Page 24: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

24www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION – CONCERNS

»A particular role, concept or area of interest

Page 25: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

25www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION - CROSSCUTTING

Page 26: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

26www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION - CROSSCUTTING CONCERNS

»System level requirements that crosscut many core module level requirements

Page 27: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

27www.exoplatform.com

Copyright 2011 eXo Platform

INTRODUCTION – CROSSCUTTING CONCERNS PROBLEMS

»Code tangling, Code scattering, Less code reuse, More difficult evolution...

»Face problem in design when requirements growth

Page 28: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

28www.exoplatform.com

Copyright 2011 eXo Platform

AOP - DEFINITION

»Lets you implement individual concerns in a loosely coupled way and combine these implementations to form the final system.

»Complements object oriented programming.

» Is not about “patching” pieces of code. About performing an action systematically upon recognition of a behavior in the code. behavior in the code.

Page 29: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

29www.exoplatform.com

Copyright 2011 eXo Platform

AOP - BENEFITS

»Modularized implementation of crosscutting concerns behavior in the code.

»Easier-to-evolve systems - extensibility behavior in the code.

Page 30: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

30www.exoplatform.com

Copyright 2011 eXo Platform

AOP - BENEFITS

»Late binding of design decision behavior in the code.

»More code reuse

Page 31: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

31www.exoplatform.com

Copyright 2011 eXo Platform

AOP - CONCEPTS

» Aspect: The merger of advice and pointcuts. All information about where, when and what job is to be done.

» Advice: The job of an aspect. Defines what and when of an aspect.

» Pointcut: Pointcuts help narrow down the joinpoints advised by an aspect. Define the where of an aspect.

» Jointpoint: A point in the execution of an application where an aspect can be

plugged in. All the opportunities for advice to be applied. behavior in the code.

Page 32: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

32www.exoplatform.com

Copyright 2011 eXo Platform

AOP - CONCEPTS

» Target: Object being advised.

» Proxy: The object created after applying advice to the target object.

» Weaving: Process of applying aspects to a target object to created a new proxied object code.

Page 33: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

33www.exoplatform.com

Copyright 2011 eXo Platform

AOP – HOW IT WORKS

» Decorator Pattern

» Proxy Pattern Control access to an object

Allows for object level access control by acting as a pass through entity or a placeholder object. co

d

.

Page 34: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

34www.exoplatform.com

Copyright 2011 eXo Platform

AOP – HOW IT WORKS

Page 35: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

35www.exoplatform.com

Copyright 2011 eXo Platform

AOP – BEHIND THE SCENCES

Aspect

Object_BObject_A

advice

Object Oriented Flow

Aspect Oriented Flow

pointcut = method_B

Target Object = Object_B

Wea

ving

jointpoint = method invocation

Object Oriented Flow

Page 36: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

36www.exoplatform.com

Copyright 2011 eXo Platform

AOP – ENGINES & FRAMEWORKS

» JDK

» CGLIB really create new class through bytecode manipulation code.

» ASPECTJ

» JBOSS AOP

» SPRING FRAMEWORK c

…........ode.

Engines

Frameworks (Java)

Page 37: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

37www.exoplatform.com

Copyright 2011 eXo Platform

AOP vs OOP - CONCLUSION

Object Oriented Aspect Oriented

Class – code unit that encapsulates methods and attributes.

Aspect – code unit that encapsulates pointcuts, advice, and attributes.

Method signatures – define the entry points for the execution of method bodies.

Pointcut – define the set of entry points (triggers) in which advice is executed.

Method bodies – implementations of the primary concerns.

Advice – implementations of the cross cutting concerns.

Compiler – converts source code into object code.

Weaver – instruments code (source or object) with advice.

Page 38: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

DEMODEMO

Page 39: AOP-IOC made by Vi Quoc Hanh and Vu Cong Thanh in SC Team

www.exoplatform.com

Copyright 2011 eXo Platform

Thank you!