patterns practices Summit by nikeborome

VIEWS: 2 PAGES: 40

									  patterns & practices
 Summit 2005
Enterprise Library for .NET 2.0:
Architecture & Lessons Learned
          Tom Hollander
       For those who have been
                living in a cave:
• Application Blocks are reusable, extensible
  source-code components that provide
  guidance for common development challenges
• Enterprise Library is a library containing
  seven general purpose application blocks
    – Caching, Configuration, Cryptography, Data
      Access, Exception Handling, Logging &
      Instrumentation, Security
    – Emphasis on Consistency, Extensibility, Ease of
      Use and Integration
    – Designed for .NET Framework 1.1. First released
      in January 2005 and updated in June 2005

2                                 patterns & practices Summit
                         Enterprise Library
                               Ecosystem
                                Customer Y library

                                                          Partner X library




                                          p&p
                                    Enterprise Library


                       Customer Z library




    p&p blocks


    Partner blocks

    Customer blocks
                                                                              Block Specification
    Community blocks
3                                                        patterns & practices Summit
    Enterprise Library for .NET
                Framework 2.0
• Major new release of Enterprise Library
• Designed for .NET Framework 2.0
    – Leverages key new capabilities provided by
      the platform
    – Certain features from EntLib v1.x have been
      deprecated in favor of the platform
• Scenarios and features largely
  unchanged
    – Public APIs not identical, but changes are
      minor
    – …but many improvements are hiding under
4     the covers!                patterns & practices Summit
      Key changes from EntLib
                          1.x
    • Configuration now built on System.Configuration
      – Configuration Application Block no longer exists
      – Easier to use blocks with or without configuration files
    • Instrumentation configurable and disabled by
      default
    • Much improved Logging Application Block
      – Flexibility and performance
    • Simpler and more powerful Data Access
      Application Block
      – Use with OLE-DB, ODBC or any managed provider
    • Most of the Security Application Block has been
      removed
      – Deprecated in favor of .NET‟s Membership and
        Profile features
5                                        patterns & practices Summit
                 Enterprise Library for .NET
                             Framework 2.0
                                            Data
      Caching                                                                Logging
                                           Access




                                           Core
                                                     Plug-in
                                 Config
                                                 Instrumen-
    Cryptography                Helpers
                                                    tation
                                & Design


                                           Object
                                           Builder



                                                                             Exception
      Security                                                               Handling


                                                         Optional Provider
                   Block Dependency                      Dependency
6                                                           patterns & practices Summit
                 Enterprise Library for .NET
                             Framework 2.0
                                            Data
      Caching                                                                Logging
                                           Access




                                           Core
                                                     Plug-in
                                 Config
                                                 Instrumen-
    Cryptography                Helpers
                                                    tation
                                & Design


                                           Object
                                           Builder



                                                                             Exception
      Security                                                               Handling


                                                         Optional Provider
                   Block Dependency                      Dependency
7                                                           patterns & practices Summit
                           The Core

•   Configuration
•   Configuration Design & Tooling
•   Instrumentation
•   Object Builder



8                        patterns & practices Summit
                         Configuration
    • All Enterprise Library blocks are
      configurable
      – Controls how the blocks work in your
        app
      – Specifies which plug-ins you are using
    • New architecture makes it much
      easier to use blocks with or without
      configuration files
      – Factories build up block objects using
        data from the configuration files
      – Objects can be “newed up” directly with
        primitive data types
9                              patterns & practices Summit
            Configuration Runtime
• Enterprise Library for .NET Framework 2.0
  uses System.Configuration, but provides
  additional helper classes to provide more
  functionality
     – All blocks provide ConfigurationSections which
       are stored in app.config / web.config by default
     – All System.Configuration features such as
       encryption and using external files are supported
• Configuration Runtime helper classes are in
  the Common assembly
     – Used by all Enterprise Library application blocks
     – Can be used by your apps, but you generally
       won‟t need to
10                                   patterns & practices Summit
                      Configuration Sources
                                      Application Blocks                            app.config
                                       or Custom Code                              / web.config

                                                                                     Default
                                                                               ConfigurationSource
                                                                                       =…


                                     IConfigurationSource




         SystemConfiguration          FileConfiguration                   SqlConfiguration
               Source                      Source                             Source


                         System.Configuration


                 app.config                foo.config      File Watcher
 File Watcher   / web.config

11                                                            patterns & practices Summit
        Choosing a Configuration
                         Source
• Several ways of choosing a configuration source
  when using Enterprise Library
• If you access blocks using static façades or factories
  (Examples: DatabaseFactory, Logger), you will
  always use the application‟s default
  ConfigurationSource:
     – If you define a ConfigurationSources section in your
       default.config file, you can specify which type of source
       should be used
     – If you don‟t have this section,
       SystemConfigurationSource is used
• If you use the instance factories
  (Examples:DatabaseProviderFactory,
  LogWriterFactory) you can specify a configuration
  source of your choosing
12   – Instantiated directly or using     patterns & practices Summit
          Configuration Design &
                         Tooling
     • Configuration tool eliminates the need to
       edit the blocks‟ XML configuration files
       – Quickly add default configuration for a block
       – Strongly typed properties and providers
       – Validate configuration before you save
     • No major changes to configuration tool
       experience from previous versions of
       Enterprise Library
     • Configuration Design-time subsystem can
       be used in your own applications and
       blocks to provide a similar experience for
       your users
13                                   patterns & practices Summit
                        Instrumentation
     • All Enterprise Library blocks include
       instrumentation to assist in development,
       testing and operations
       – Event Log events
       – Performance Counters
       – WMI events
     • All instrumentation is disabled by default,
       but each type can be individually enabled
       using the configuration tool
     • Instrumentation code contained in
       Common assembly can be reused in your
       apps
14                                patterns & practices Summit
       Enabling Instrumentation
• Run the installer classes to create the
  instrumentation artifacts:
     – Run installservices.bat (using an admin
       account), or
     – Run installutil.exe over each Enterprise Library
       assembly (using an admin account), or
     – Create your own installers/MSI that do this
• Configure instrumentation for your app using
  the tool

15                                  patterns & practices Summit
              Factories and Object
                           Builder
• Objects inside application blocks need to
  be constructed and configured
• There are different ways you can
  construct objects depending on what you
  want to do
     – Use a default configuration source
     – Use a custom configuration source
     – Don‟t use any configuration at all
     – Inject instrumentation
• Although each block is different, all rely
16                              patterns & practices Summit
                          Object Builder
     • New subsystem shared between
       EntLib and Composite UI Application
       Block
     • Responsible for building objects
       inside the application blocks
       – Injecting configuration data into the
         block objects
       – Configuring instrumentation for the
         blocks
     • Can be leveraged from your own
       apps, but understanding
17     ObjectBuilder is not required &to use Summit
                               patterns practices
                                           Factories
               Application Block                        Core

 User   Static Factory                             Configuration
 Code     or façade                                  Source



             Instance                            Enterprise Library
          Provider Factory                            Factory



                         Custom
                                                   Object Builder
                         Factory




                          Block Objects              Strategies


18                                        patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
19                                                           patterns & practices Summit
                      Exception Handling
                       Application Block
     • Provides simple mechanism that allows you to
       consistently deal with exceptions throughout
       your application
     • Define “Exception Policies” which link an
       exception to an action
       – Exceptions of type ApplicationException should be
         logged
       – Exceptions of type SqlClientException should be
         caught and wrapped with an exception of type
         DataLayerException and re-thrown
       – Exceptions of type SecurityException should caught
         and replaced with an AccessDeniedException which
         will be thrown
     • Actions provided include
       – Logging
       – Wrapping one exception with another
20     – Replacing one exception with an other & practices Summit
                                        patterns
                 Exception Handling -
                            Example
     Try
       ' some code that may throw
     Catch Ex As Exception
       If ExceptionPolicy.HandleException(ex,
       “DataLayer”) _ Then Throw
     End Try




21                                  patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
22                                                           patterns & practices Summit
     Logging Application Block
     • Provides a simple model for logging
       events
       – Strongly typed, extensible log schema
     • Built on top of System.Diagnostics
     • Configuration driven – you decide what
       messages are logged where at runtime.
     • Use any .NET TraceListener, including
       EntLib formatter-aware listeners:
       – EventLog, Database, Text File, MSMQ, E-
         mail, WMI, or create your own
     • Tracer class lets you time key activities
       and correlate any enclosed events
23                                 patterns & practices Summit
                    New In This Release
     • Built on System.Diagnostics features, such as
       TraceListener and CorrelationManager
        – Simplifies EntLib code, enables better integration with
          „core‟ System.Diagnostics functionality
     • Eliminated Distribution Strategies concept
        – Same scenarios supported by „chaining‟ instances of
          the block together via TraceListeners (eg MSMQ)
     • One LogEntry in multiple Categories
        – Increased flexibility and control in event routing,
          enabling/disabling
     • Pluggable, extensible filters
        – Can be queried from API to avoid creating expensive
          log messages


24                                          patterns & practices Summit
                    Logging - Examples
Dim log As LogEntry = New LogEntry
log.Message = “Your message here…”
log.Priority = 1
log.EventId = 100
log.Categories.Add("UI")
log.Categories.Add("Debug")
Logger.Write(log)

// Or if you prefer one line...
Customer cust = GetCustomer(123);
// Log the customer – will call cust.ToString() for the log entry
Logger.Write(cust, category, priority);

25                                         patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
26                                                           patterns & practices Summit
        Data Access Application
                         Block
     • Provides simplified access to the most
       often used features of ADO.NET with
       applied best practices
     • Improve Consistency
       – Write code that works against multiple
         database brands (caveats apply!)
     • Improve ease of use
       – Easily call a stored procedure with one line of
         code
       – Let the block manage the lifetime of database
         connections
       – Work with database connection strings stored
         in configuration or specified in code
27                                   patterns & practices Summit
            Data Access - Examples
     Public Function GetProductsInCategory(ByRef Category As Integer) As DataSet
            ' Create the Database object, using the database instance with the
            ' specified logical name. This is mapped to a connection string in
            ' the configuration file
            Dim db As Database = DatabaseFactory.CreateDatabase("Sales")

           ' Invoke the stored procedure with one line of code!
           return db.ExecuteDataSet("GetProductsByCategory", Category)

           ' Note: connection was closed by ExecuteDataSet method call
     End Function


     public Dataset GetProductsInCategory(string connectionString, int category)
     {
            // Create the Database object, using the specified connection string
            SqlDatabase db = new SqlDatabase(connectionString);

           // Invoke the stored procedure with one line of code!
           return db.ExecuteDataSet("GetProductsByCategory", category);

           // Note: connection was closed by ExecuteDataSet method call
     }
28                                                     patterns & practices Summit
                 New In This Release
     • Use with or without configuration
       – Reuses .NET‟s new <connectionStrings> section
       – Or, use your own connection string
     • Simplified API
       – Uses ADO.NET‟s DbCommand instead of
         DBCommandWrapper
     • Use with any managed ADO.NET 2.0 provider
       – GenericDatabase class works with any provider,
         including OLE-DB and ODBC (advanced functionality
         like parameter discovery is unavailable)
       – Use database-specific Database derived classes to
         support the full API with improved transparency



29                                    patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
30                                                           patterns & practices Summit
     Caching Application Block
     • Ideal for smart client applications that uses
       locally cached reference data to create
       requests and support offline operations
     • Supports backing stores that persist cache
       data into a database or isolated storage,
       so the data can survive app restarts
     • Note: ASP.NET cache
       (System.Web.Caching) can be used
       across multiple application types and is
       generally a better choice for applications
       that don‟t require the cache to be persisted

31                                patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
32                                                           patterns & practices Summit
         Cryptography Scenarios
     • You need to encrypt sensitive data using a
       symmetric key before storing in the
       database and decrypt when reading
     • You need to encrypt information (without
       using keys) for use on a single machine
     • You need to create a hash of a password
       to store in a database and be able to
       compare that hash with a user supplied
       hash to see if you have a match without
       storing the user password
33                              patterns & practices Summit
       Cryptography Application
                         Block
     • Provides a simplified approach to implementing
       common cryptography scenarios using
       symmetric encryption and hashing
     • Provides operations on both strings and byte
       streams
       –   CreateHash
       –   CompareHash
       –   EncryptSymmetric
       –   DecryptSymmetric
     • Supports all .NET crypto algorithms out of the
       box
     • Supports DPAPI for keyless crypto on a single
       machine or using a single user account
     • Algorithms and keys can be managed through
34
       the configuration tool
                                    patterns & practices Summit
                  Enterprise Library for .NET
                              Framework 2.0
                                             Data
       Caching                                                                Logging
                                            Access




                                            Core
                                                      Plug-in
                                  Config
                                                  Instrumen-
     Cryptography                Helpers
                                                     tation
                                 & Design


                                            Object
                                            Builder



                                                                              Exception
       Security                                                               Handling


                                                          Optional Provider
                    Block Dependency                      Dependency
35                                                           patterns & practices Summit
                    Security Scenarios
     • You need to authorize users
       – Using one or more security systems or mechanisms
     • You need to cache authentication or
       authorization data for the duration of a logon
       session
     • Note: Previous versions of the Enterprise Library
       Security Application Block also supported
       Authentication, Profile and Roles. This is now
       supported by the .NET Membership and Profile
       class, so this functionality has been removed
       from the block.



36                                    patterns & practices Summit
          Security Application Block +
                             ASP.NET
                  Security Application Block
                                             Authorization
                                                                               • Encapsulate
                                                 Rule
                                               Provider
                                                                                 common application
            Authorization   IAuthorization                                       security tasks
                                                                               • Present a standard,
              Factory          Provider
                                                AzMan
                                             Authorization
                                               Provider                          provider model for
                                                                                 common security
              Security        ISecurity         Caching         Caching
                                                                                 tasks
               Cache
              Factory
                               Cache
                              Provider
                                                 Store
                                                Provider
                                                               Application
                                                                 Block         • Minimize the need
 Client
                                                                                 for custom security-
 Code                       ASP.NET          ActiveDirectory
                                                                                 related code
                                              Membership
                                                Provider                       • Incorporate best
                            Membership
            Membership
                             Provider
                                                 Sql
                                                                                 practices for
                                              Membership
                                               Provider
                                                                                 application security
                                                  Sql
                               Profile
               Profile                           Profile
                              Provider
                                                Provider

37                                                                           patterns & practices Summit
          Migration from previous
                         versions
     • This release is not 100% API compatible with
       previous releases of Enterprise Library
     • Most API changes are minor and upgrading will
       be simple
     • Exceptions:
       – Configuration Application Block, which no longer
         exists  convert to System.Configuration
       – Authentication, Role and Profile providers in Security
         Application Block  convert to ASP.NET providers
       – Migration guidance is included in docs and samples
     • Configuration file formats have changed, and all
       configuration needs to be rebuilt using the tool


38                                       patterns & practices Summit
                               Availability
     • Pre-release versions available from:
       – http://practices.gotdotnet.com/projects/e
         ntlib
       – November CTP is a complete MSI
         including documentation
       – December “interim community drop” is
         more complete and includes the
         configuration tool, but is source only
       – No cryptography block in either release
     • Final release in January on
       http://msdn.microsoft.com/practices
39                               patterns & practices Summit
                      Questions?

• tom.hollander@microsoft.com
• http://practices.gotdotnet.com/
  projects/entlib




40                      patterns & practices Summit

								
To top