KEMBAR78
Creating a Plug-In Architecture | PPTX
Creating a Plug-in
Architecture
in .NET
ONDREJ BALAS @ONDREJBALAS
WWW.ONDREJBALAS.COM
ONDREJ@ONDREJBALAS.COM
ONDREJ BALAS
Microsoft MVP in .NET
Writer for Visual Studio Magazine
Owner of UseTech Design (est. 2001)
Building software that drives business
W W W.OND REJBALAS.COM
OND REJ@OND REJBALA S.COM
@ OND REJBALAS
• 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)
• Split work across natural boundaries
• Incrementally create and deploy features
• Deploy only new and updated modules
• Don’t need source code for existing
system
• Changes to contracts are difficult to
manage
• Increased development time for
small projects
// Get input – “HELLO”
string input = Console.ReadLine().ToUpper();
// "Encrypt" it
string output = "";
foreach (char c in input)
{
byte b = (byte) c;
output += (char)(b < 78 ? b + 13 : b - 13);
}
// Write output – “URYYB”
Console.WriteLine(output);
Tightly Coupled Code
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;
}
}
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);
}
Decoupled
Code
• 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
• Most IoC containers
– Ninject (using ninject.extensions.conventions)
– Castle.Windsor
– Unity
– StructureMap
– MEF – Managed Extensibility Framework
– MAF – Managed AddIn Framework
– Many other IoC containers
Add Sales Tax
to Calculation
Toppings!
More Toppings!
• 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
• Recovering from plug-in crashes
• Dynamic loading and unloading at run time
• Updating an application while it’s running
Thanks!
ONDREJ BALAS @ONDREJBALAS
WWW.ONDREJBALAS.COM
ONDREJ@ONDREJBALAS.COM
@ONDREJBALAS
GITHUB.COM/ONDREJBALAS

Creating a Plug-In Architecture

  • 1.
    Creating a Plug-in Architecture in.NET ONDREJ BALAS @ONDREJBALAS WWW.ONDREJBALAS.COM ONDREJ@ONDREJBALAS.COM
  • 2.
    ONDREJ BALAS Microsoft MVPin .NET Writer for Visual Studio Magazine Owner of UseTech Design (est. 2001) Building software that drives business W W W.OND REJBALAS.COM OND REJ@OND REJBALA S.COM @ OND REJBALAS
  • 4.
    • An externalpiece of functionality that may be added to an existing system by abiding by a contract pre-defined by that system. Plug-in (my definition)
  • 8.
    • Split workacross natural boundaries • Incrementally create and deploy features • Deploy only new and updated modules • Don’t need source code for existing system
  • 11.
    • Changes tocontracts are difficult to manage • Increased development time for small projects
  • 13.
    // Get input– “HELLO” string input = Console.ReadLine().ToUpper(); // "Encrypt" it string output = ""; foreach (char c in input) { byte b = (byte) c; output += (char)(b < 78 ? b + 13 : b - 13); } // Write output – “URYYB” Console.WriteLine(output);
  • 14.
  • 15.
    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; } }
  • 16.
    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); }
  • 17.
  • 25.
    • 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
  • 26.
    • Most IoCcontainers – Ninject (using ninject.extensions.conventions) – Castle.Windsor – Unity – StructureMap – MEF – Managed Extensibility Framework – MAF – Managed AddIn Framework – Many other IoC containers
  • 28.
    Add Sales Tax toCalculation Toppings! More Toppings!
  • 32.
    • Why notuse 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
  • 33.
    • Recovering fromplug-in crashes • Dynamic loading and unloading at run time • Updating an application while it’s running
  • 34.

Editor's Notes

  • #2 The icons use FontAwesome. If they appear to be missing, try installing the font from here: http://fortawesome.github.io/Font-Awesome/ As you may have guessed, this talk is about plug-in architectures in .NET.
  • #4 Whenever you talk about plugin architectures, the canonical example is this: our electrical system. It’s a great example because you can plug things into it without affecting other devices plugged into the same system. It also represents a common standard that vendors can comply with.
  • #5 My definition. Read it.
  • #18 After this slide, jump into example 4-ConsoleEncrypter
  • #23 After this slide, jump into example 5-CompositionRoot and 6-IoCContainer
  • #34 Mention the real application that I put into production based on this sample code