430 likes | 652 Views
Enterprise Library Building an Application Block. Ed Jezierski Solution Architect Scott Densmore Software Engineer Ron Jacobs Product Manager. Architectural Guidance from patterns & practices Application Blocks Enterprise Library Vision Enterprise Library Application Blocks Configuration
E N D
Enterprise LibraryBuilding an Application Block Ed Jezierski Solution ArchitectScott Densmore Software Engineer Ron JacobsProduct Manager
Architectural Guidance from patterns & practices Application Blocks Enterprise Library Vision Enterprise Library Application Blocks Configuration Cryptography Data Access Security Demonstration Questions Logging & Instrumentation Exception Handling Caching Agenda
D A D I A D I patterns & practicesArchitecture Guidance for the Enterprise ProvenBased on field experience Authoritative Offer the best advice availableAccurateTechnically validated and testedActionableProvide the steps to success RelevantAddress real-world problems based on customer scenarios Available online: http://www.microsoft.com/practices Books available: http://www.amazon.com/practices Patterns Application Blocks Reference Architectures Atomic solutions to recurring problems Sub-system-level guidance for common services System-level guidance for common customer scenarios D A D I D A D I A A D D I I Guides Guidance for broad horizontal topics such as security, performance, deployment and operations
Sound familiar? • Having similar code pasted throughout your applications • Writing components to enable and encourage reuse…then rewriting them to meet specific application needs • Having components that should work well together, but… • Each component mandating inconsistent requirements on the consuming application • Longing for a way to future-proof your software library
Enterprise Library Philosophy • Consistency • Apply consistent design patterns and implementation approaches • Extensibility • Include extensibility points allowing developers to customize the behavior of the blocks by plugging in their own code or customize by directly modifying source code • Ease of Use • Leverage a graphical configuration tool • Provide a simpler installation procedure • Include clear and complete documentation and samples • Integration • Application blocks should be designed to work well together and tested to make sure that they do. But it should also be possible to use the application blocks individually
Enterprise Library v1 Caching Exceptions Legend Security Data Access Logging Dependency Plug-in Crypto Configuration Config Tool
Customer Y library Partner X library p&p Enterprise Library Customer Z library Enterprise Library Vision p&p blocks Partner blocks Customer blocks Block Specification Community blocks
Elements of an Application Block • Core functionality • Pluggable providers • Factories • Configuration • Unit tests • Quick Starts • Guidance and reference documentation
Core Functionality • Central to its operation • May be influenced via configuration settings, but basic behavior cannot be changed without source code modifications • Often internal, but when exposed to an application, it is typically provided via a single class, interface, or facade • It’s OK to have core functionality – not everything must be replaceable or even configurable!
Core Functionality Examples • Caching • Cache implementation (hashtable) – not exposed • Scavenging algorithm –not exposed • Cache manager – exposed as a class • Exception Handling • Exception policy – exposed as a class with single public static method (HandleException) • Logging and Instrumentation • Log filters – not exposed
Pluggable Providers • Associated with an application block capability, defined by a “provider type.” • Caching: Backingstore • Data Access: Database • Exception Handling: Exception Handler • A provider is a specific implementation of a provider type. Each application block includes at least one provider implementation of each provider type • Caching: Database and isolated storage backingstores • Data Access: SQL, DB2 and Oracle databases
Pluggable Providers • Decouples the application block's core functionality from specific implementation, achieving the following goals • Variability • Extensibility • Encapsulation • Portability across environments • Minimized coupling between application blocks • Specified in configuration data with type information and a name
Provider Implementations and Configuration • Providers typically allow for configuration of each specific implementation • Example: the replace exception handler has a configuration setting for the new exception type • The abstract base class ConfigurationProvider defines the contract for initializing a provider with configuration information • Decision point: create a new provider implementation, or configuration property of an existing implementation
Provider Factories • Uses Plugin [Fowler] pattern to create providers • Neither applications nor application blocks require information about specific implementation of provider • Specific implementation to be constructed determined by configuration settings • Abstract base class (ProviderFactory) in Configuration Application Block contains code to instantiate configurable providers
Provider Factories • Static method to create provider objects • Allow for creation of default provider (as specified by configuration settings) • Allow for creation of named provider • ' Create the default database instance • Dim db As Database = DatabaseFactory.CreateDatabase() • ' Use a named instance to map to configuration • Dim salesDb As Database = DatabaseFactory.CreateDatabase("Sales") Allows you to reconfigure and execute the application without recompiling
Configuration • Allows you to easily adapt the application block to meet the needs of different enterprise scenarios • Creation and editing of configuration settings is done with the Configuration Console • Configuration settings may be defined for an application block’s core functionality and for each provider type
Configuration • Using configuration settings to adapt an application block to a particular situation has two advantages: • Characteristics of an application block can be configured by different people at different times in the application life cycle • The application block can be incrementally adapted for increasingly complex situations
Configuration • Runtime configuration • Object graphs that represent configuration settings • Configuration Application Block reads settings from storage and returns objects to application block • XML File Storage Provider and Transformer require that objects be serializable • Design-time configuration • Allow settings to be displayed, changed and validated using the Configuration Console
Design-Time Configuration • Based upon System.ComponentModel • Configuration node is of type Component • The Configuration Console provides the Site • Services are available through Config Block • Configuration node classes for each node in the configuration tree • Each node determines the properties to be displayed when node is selected • Validation rules can be included for any property
Configuration Nodes • Determine how the Configuration Console displays the hierarchy of configuration nodes • Determine which/how properties are exposed in the graphical interface • Enable the Configuration Console to validate that the values a user assigns to an application block's properties are appropriate • Can contain references that point from one application block to another (such as an authorization database that points to a database instance) • Contain references to the runtime data classes
Configuration Design Manager • Implements IConfigurationDesignManager interface • Allows an application block to be added to an application’s configuration hierarchy • Creates menu items and commands that are invoked as user interacts with the Configuration Console to configure the application block • Responsible for loading and saving configuration data • Assembly must be located in same directory as Configuration Console executable
Configuration Application Block Support The Configuration Application Block provides much of the plumbing for runtime and design-time configuration
Configuration Console Properties Configuration Nodes Validation Error
Unit Tests • Verify functionality of application block • Verify that modifications to application block have not cause regression • Enterprise Library developed using Test Driven Development (TDD)
Quick Starts • Demonstrate common scenarios of application block usage • Display output indicating what happened “under the covers” • Detect common errors (e.g. user didn’t perform required setup) and display useful information • Goal: Open solution, hit F5, and see it run
Documentation • Integrated into Visual Studio • Guidance • Introduction and goals • Developing applications using the application block • Design of the application block • Extending the application block • Deploying the application block • Quick start walkthroughs • Reference API • Public and protected • Summaries, parameters and return values
A Simple Example: Hello World Application Block • Client application can use the application block to return a greeting for a specified user • The implementation of how the greetings are returned is encapsulated in a greeting provider • An application can use multiple greeting providers, each identified by a unique name • The Configuration Console is used to select the default greeting provider, used when the client does not specify a name for the provider • Each greeting provider has a default message, set by the Configuration Console
Hello World Application Block Core Functionality • Minimal to keep things simple • Includes the interface definition IGreetingProvider, which all greeting providers must implement • Includes the class HelloWorldConfigurationView • Derives from ConfigurationView • Exposes the runtime configuration settings required by the application block’s providers • Passed to the Initialize method of each provider when it is created
Hello World Application Block Configuration View • Initialized with ConfigurationContext • public virtual GreetingProviderData GetGreetingProviderData(string name) • { • // HelloWorldSettings is our runtime configuration object • HelloWorldSettings settings = GetHelloWorldSettings(); • return settings.GreetingProviders[name]; • } Returns configuration data required by application block • public virtual string GetDefaultGreetingProviderDataName() • { • HelloWorldSettings settings = GetHelloWorldSettings(); • return settings.DefaultGreeter; • }
Hello World Application Block Providers • Text greeting provider • Returns plain text greeting for specified user • Greeting for a user is determined by configuration settings • Includes configuration design-time support to allow greetings to be entered and validated • Custom greeting provider • Allows user to select the FQTN of a class that implements IGreetingProvider • Configuration is provided via a property bag of name-value pairs (no design-time support)
Hello World Application Block Runtime Configuration • Requirements • Block allows multiple greeting providers • Each greeting provider has a name • Text greeting provider can have multiple greetings • A greeting is name and a message • Configuration Information is represented as serializable classes
Runtime Configuration Data <?xml version="1.0" encoding="utf-8"?> <helloWorldConfiguration> <xmlSerializerSection type="Microsoft.Practices.Samples.HelloWorld.Configuration.HelloWorldSettings, Microsoft.Practices.Samples.HelloWorld, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"> <helloWorldSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultGreeter="Casual Greetings" xmlns="http://www.microsoft.com/practices/samples/08-31-2004/helloworld"> <greetingProviders> <greetingProvider xsi:type="TextGreetingProviderData" name="Casual Greetings" defaultGreeting="Hello World!"> <greetings> <greeting name="John" message="Hi John." /> <greeting name="Maria" message="Ciao Maria!" /> </greetings> </greetingProvider> </greetingProviders> </helloWorldSettings> </xmlSerializerSection> </helloWorldConfiguration>
Runtime Configuration Classes • HelloWorldSettings • Includes configuration section name, collection of providers, and default greeting provider • GreetingProviderData (base class), TextGreetingProviderData, CustomGreetingProviderData • Common settings and settings specific to providers • TextGreetingDataCollection • Collection of text greetings • TextGreetingData • Name and message
Runtime Configuration Class Serialization • Root object serialization • [XmlRoot("helloWorldSettings", Namespace=HelloWorldSettings.ConfigurationNamespace)] • public class HelloWorldSettings • { • … • } Collection serialization • [XmlArray("greetings", Namespace=HelloWorldSettings.ConfigurationNamespace)] • [XmlArrayItem(Type=typeof(TextGreetingData), Namespace=HelloWorldSettings.ConfigurationNamespace)] • public TextGreetingDataCollection Greetings • { • get { return this.greetings; } • }
Runtime Configuration Class Serialization • XML include types for base classes • [XmlType("greetingProvider", Namespace=HelloWorldSettings.ConfigurationNamespace)] • [XmlInclude(typeof(CustomGreetingProviderData))] • [XmlInclude(typeof(TextGreetingProviderData))] • public abstract class GreetingProviderData : ProviderData • { • … • }
Hello World Design-Time Configuration • Required to allow the application block to be configured with the Configuration Console
Hello World Client • Create provider • // Create default greeting provider (determined by configuration) • IGreetingProvider defaultProvider = GreetingFactory.GetGreetingProvider(); • // Create named instance • IGreetingProvider formalProvider = GreetingFactory.GetGreetingProvider("Formal Greetings"); Call provider methods • string greeting = defaultProvider.SayGreeting(“John”);
Additional Resources • Improving Web Application Security http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/ThreatCounter.asp • Improving .NET Application Performance and Scalability http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenet.asp • Application Architecture for .NET http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/distapp.asp • PatternShare.org • Enterprise Library Communityhttp://go.microsoft.com/fwlink/?linkid=39209&clcid=0x09 • www.ronjacobs.com • Slides • Tech Tips • Podcasts
Announcing: Enterprise Library 1.0 Download it Today! http://www.microsoft.com/practices
Patterns and Practices Live! • Slides, Hands On Labs, On Demand Webcasts • Upcoming Live Webcasts • 3/28 Building your own block • 3/31 Enterprise Library Applied • 4/12 Global Bank Baseline Architecture • 4/14 Updater Application Block v2 http://www.pnplive.com