56
Creating a Plug-in Architecture in .NET @OndrejBalas

Creating a Plug-In Architecture

Embed Size (px)

DESCRIPTION

Slides from my talk, "Creating a Plug-In Architecture in .NET"

Citation preview

Page 1: Creating a Plug-In Architecture

Creating a Plug-in Architecturein .NET

@OndrejBalas

Page 2: Creating a Plug-In Architecture

Ondrej Balas• Husband & Father• Small Guy, Big Data• Owner of UseTech Design• I build software that drives business

Blog: http://www.ondrejbalas.com Twitter: @OndrejBalas

Page 3: Creating a Plug-In Architecture

Plug-in Architecture

Page 4: Creating a Plug-In Architecture

• An external piece of functionality that may be added to an existing system by abiding by a contract pre-defined by that system.

Plug-in (my definition)

Page 5: Creating a Plug-In Architecture

Interface Segregation

Page 6: Creating a Plug-In Architecture

Examples of Plug-ins

Page 7: Creating a Plug-In Architecture

Benefits

Page 8: Creating a Plug-In Architecture

Benefits• Split work across natural boundaries• Incrementally create and deploy features• Deploy only new and updated modules• Don’t need source code for existing

system

Page 9: Creating a Plug-In Architecture

New & Old

Page 10: Creating a Plug-In Architecture

Licensing Flexibility

Page 11: Creating a Plug-In Architecture

• Changes to contracts are difficult to manage• Increased development time for

small projects

Drawbacks

Page 12: Creating a Plug-In Architecture

But How?

Page 13: Creating a Plug-In Architecture

// Get input – “HELLO”string input = Console.ReadLine().ToUpper(); // "Encrypt" itstring output = "";foreach (char c in input){ byte b = (byte) c; output += (char)(b < 78 ? b + 13 : b - 13);}

// Write output – “URYYB”Console.WriteLine(output);

Dependency Inversion

Page 14: Creating a Plug-In Architecture

Tightly Coupled Code

Page 15: Creating a Plug-In Architecture

static void Main(string[] args) { // Get input string input = Console.ReadLine().ToUpper();

// "Encrypt" it Rot13 encryptionAlgorithm = new Rot13(); string output = encryptionAlgorithm.Encrypt(input);

// Write output Console.WriteLine(output);}public class Rot13 { public string Encrypt(string input) { string output = ""; foreach (char c in input) { output += (char)(c < 78 ? c + 13 : c - 13); } return output; }}

Page 16: Creating a Plug-In Architecture

static void Main(string[] args) { // Get input string input = Console.ReadLine().ToUpper();

// "Encrypt" it IEncryptionAlgorithm encryptionAlgorithm = new Rot13(); string output = encryptionAlgorithm.Encrypt(input);

// Write output Console.WriteLine(output);}

public class Rot13 : IEncryptionAlgorithm { public string Encrypt(string input) { string output = ""; foreach (char c in input) { output += (char)(c < 78 ? c + 13 : c - 13); } return output; }}

public interface IEncryptionAlgorithm{

string Encrypt(string input);}

Page 17: Creating a Plug-In Architecture

DecoupledCode

Code Sample

Page 18: Creating a Plug-In Architecture

Dependency Injection

Page 19: Creating a Plug-In Architecture

Dependency Injection

Page 20: Creating a Plug-In Architecture

Dependency Injection

Page 21: Creating a Plug-In Architecture

Dependency Injection

Page 22: Creating a Plug-In Architecture

Dependency InjectionCode Sample

Page 23: Creating a Plug-In Architecture

Plugins!

Page 24: Creating a Plug-In Architecture

Code Sample

Page 25: Creating a Plug-In Architecture

• Single - Automatically choose one.• One of Many - Like Single, but give the user

the choice. Think smart phone apps• Many Simultaneously – Using the composite

pattern, many plug-ins are treated as one. Like browser plug-ins; they have the potential of overlapping

Plug-in Strategies

Page 26: Creating a Plug-In Architecture

• Most IoC containers– Ninject (using ninject.extensions.conventions)– Castle.Windsor– Unity– StructureMap– MEF – Managed Extensibility Framework– MAF – Managed AddIn Framework– Many other IoC containers

Frameworks

Page 27: Creating a Plug-In Architecture

MVC ExampleSite Demo

Page 28: Creating a Plug-In Architecture

Add Sales Tax to Calculation

Toppings!More Toppings!

RequirementsPlugin Demo

Page 29: Creating a Plug-In Architecture

Big Al’s Plug-inCode Sample

Page 30: Creating a Plug-In Architecture

Papa’s Plug-inCode Sample

Page 31: Creating a Plug-In Architecture

A New ClientCode Sample

Page 32: Creating a Plug-In Architecture

• Why not use config files? Config files are for changing the data/data source. Plug-in architecture is for dropping in functionality

• Deciding where to put plug-in points

Discussion

Page 33: Creating a Plug-In Architecture

• Recovering from plug-in crashes• Dynamic loading and unloading at run time• Updating an application while it’s running

Endless Possibilities

Page 34: Creating a Plug-In Architecture

@OndrejBalas

Thanks!Questions? E-mail or Tweet me

[email protected]

https://github.com/ondrejbalas/plugin-architecture

Page 35: Creating a Plug-In Architecture

@OndrejBalas

Thanks!Questions? E-mail or Tweet me

[email protected]

https://github.com/ondrejbalas/plugin-architecture

Page 36: Creating a Plug-In Architecture

@OndrejBalas

Thanks!Questions? E-mail or Tweet me

[email protected]

https://github.com/ondrejbalas/plugin-architecture

Page 37: Creating a Plug-In Architecture

@OndrejBalas

Thanks!Questions? E-mail or Tweet me

[email protected]

https://github.com/ondrejbalas/plugin-architecture

Page 38: Creating a Plug-In Architecture

@OndrejBalas

Thanks!Questions? E-mail or Tweet me

[email protected]

https://github.com/ondrejbalas/plugin-architecture

Page 39: Creating a Plug-In Architecture
Page 40: Creating a Plug-In Architecture

Any further and you will see my unused slides

Page 41: Creating a Plug-In Architecture

• Single - Automatically choose one.• One of Many - Like Single, but give the user

the choice. Think smart phone apps• Many Simultaneously – Using the composite

pattern, many plug-ins are treated as one. Plug-ins can interfere with each other using this strategy. Like browser plug-ins; they have the potential of overlapping

Plug-in Strategies

Page 42: Creating a Plug-In Architecture

• Dependency Injection• Composite Pattern

Plug-in Patterns

Page 43: Creating a Plug-In Architecture

• Single (Plug-in replaces functionality)• Multiple (Different implementations of the

same thing – examples doing stuff with files, compression, encryption)• Multiple (Each plug-in has different

functionality like iphone apps)• Single > Multiple would use the Composite

Pattern. Multiple should use Composite

Plug-in Architectures in the wild

Page 44: Creating a Plug-In Architecture

Code to Interface = Code to Contract

• It’s just broken out into optional assemblies

Page 45: Creating a Plug-In Architecture

• Plugins work due to the concept of abstractions or programming to interface and the idea of composable parts

• The basic idea is that your application loads pieces which by default can be nullable but be replaced by the client

Page 46: Creating a Plug-In Architecture

The Client - Big Al’s Pizza Pies

• Like most of our clients, wants “mostly custom” without “custom” cost

• Buys our software which is customizable by way of plugin

• Nephew can code in C# and make changes

Page 47: Creating a Plug-In Architecture

More toppings

Page 48: Creating a Plug-In Architecture

Sales tax for customers on the other side of the river

Page 49: Creating a Plug-In Architecture

Delivery Charges Extra

• Uhoh.. We didn’t give them enough plugin points to be able to do what they want to do.

• This shows one of the pain points – if we don’t think of it, they’ll have to wait for the next version that will include it.

• A new client can get hook points in their own branch that can be merged into the master later

• It isn’t perfect, but it’s a good solution that buys us a lot of flexibility. Piecemeal functionality, deployed to customer’s site, and customer can customize the code as needed – to a point

Page 50: Creating a Plug-In Architecture

Where to make plugin points

Page 51: Creating a Plug-In Architecture

A La Carte

Page 52: Creating a Plug-In Architecture

More Benefits

• Replace or accentuate existing code with dropped in code

• Doesn’t require recompiling the original project OR having the source code

• Plugins are usually not required

Page 53: Creating a Plug-In Architecture

Config Files?

Page 54: Creating a Plug-In Architecture

Text

Page 55: Creating a Plug-In Architecture

Why

Page 56: Creating a Plug-In Architecture

Applications become more like Frameworks