Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate
About the speaker Owner, Benjamin Day Consulting, Inc. – –Web: –Blog: Trainer –Visual Studio Team System, Team Foundation Server Microsoft MVP for C# Microsoft VSTS/TFS Customer Advisory Council Leader of Beantown.NET INETA User Group
Agenda Overview Policy Injection Logging Exception Handling Validation
Why Enterprise Library? Why not? –Why write and re-write common application code? Focus on solving the core business problem Microsoft solved it for you –Standardized solution –Hooks in nicely with.NET through configs It’s free
CachingCaching SecuritySecurity DataAccessDataAccessLoggingLogging ExceptionHandlingExceptionHandling Ent Lib 3.0 – Application Blocks Plug-in ConfigHelpers & Design ConfigHelpers Instrumen- tation Object Builder CryptographyCryptography Core Policy Injection ValidationValidation
Data Access Block Wraps common actions for database access Handles closing of connections, IDispose calls Some degree of database independence Relatively easy to understand
Adopting Enterprise Library Why is this talk titled “Doing Something Useful…”? Can be like swimming in the Atlantic at low tide in the spring What does this have to do with the Data Access Block? Does it have to be so difficult?
My $0.02 on where begin Start with the Policy Injection Block –Allows you to defer decisions on other blocks Configure PIB to use logging –Simple way to debug your configuration Exception Handling Block –(eat your vegetables) Caching –Fix performance problems
Policy Injection Block Uses magic Mostly elves Some wood nymphs for reliability Just kidding.
Policy Injection Block Layer of abstraction Lets you hook functionality in at runtime via config Intercepts calls in to an object’s methods Think aspect-oriented programming or dependency injection for methods
The Overall Design
Getting started with the PI block Two ways to use Policy Injection: –Objects extend from MarshalByRef –Code against interfaces instead of concrete classes Add references –Microsoft.Practices.EnterpriseLibrary.PolicyInjection –Microsoft.Practices.ObjectBuilder –Microsoft.Practices.EnterpriseLibrary.CallHandlers –Plus, whatever block you’ll reference from PI Edit app.config/web.config with EntLibConfig.exe
Calling into the PI framework Call PolicyInjection.Create () or one of the overloads Returns a wrapper to your object Call your methods through the wrapper
Configuration EntLibConfig.exe Uses the Remoting Policy Injector Policy consists of –Matching Rules –Handlers
Matching Rules Tells the framework when to run the policy
Handlers Tells the framework what to do when it finds a policy match
Logging Handler Through policy injection framework, intercepts calls Writes message to log before and/or after
Logging Application Block Logs activity in your app Trace Listeners –Where does it get stored? Formatters –How does it get stored? Filters –What to write?
Logging Block Design
Trace Listeners
Formatters
Demo Add Policy Injection to an existing app Configure logging
Exception Handling Block Allows you to define how to treat exceptions Option: Catch and re-throw different exception Option: Eat the exception
Design of the Exception Block
Demo Add exception handling to our app via Policy Injection Wrap Handlers Replace Handlers
Demo Add Exception Handling to a WCF application
The Validation Block
About the Validation Block Allows you to validate access to your objects Validation logic can be stored outside of your objects in the configs Validation can also be defined via attributes Similar to a rules engine –Think Windows Workflow Rules “light”
Available Validators And Composite Contains Characters Date Time Range Domain Enum Conversion Not Null Object Collection Object Validator Or Composite Property Compare Range Validator RegEx Relative Date Time String Length Type Conversion
Common Validator Fields MessageTemplate –Provides a human-readable error message –Overrides the default implementation MessageTemplateResourceName –Enables messages in resource files MessageTemplateResourceType Negate –If statement evaluates to true rule fails –false succeeds Tag –Category for the validation
Message Template Tokens Think String.Format() Use these to create custom messages {0} –Original value being validated {1} –“Key” for the value –If property or field name (m_id or Id) –If object null {2} –The tag value if available
Service Layer Pattern From “Patterns Of Enterprise Application Architecture” by Martin Fowler, Randy Stafford, et al. Chapter 9 “Defines an application’s boundary with a layer of services that establishes a set of available operations and coordinates the application’s response in each operation.” -Randy Stafford
Demo Validate Restaurant with the ValidationFactory
The Caching Block Allows you to cache return values from method, properties Easy to add with the Policy Injection Block
Demo Populate list of states using the caching block
Summary Policy Injection Block Helps Speed Adoption Logging Block Exception Handling Block Exception Handling in WCF Validation Block Caching Block
About the speaker Owner, Benjamin Day Consulting, Inc. – –Web: –Blog: Trainer –Visual Studio Team System, Team Foundation Server Microsoft MVP for C# Microsoft VSTS/TFS Customer Advisory Council Leader of Beantown.NET INETA User Group