doing something useful with enterprise library 3.0

37
Doing Something Useful with Enterprise Library 3.0 Benjamin Day Level: Intermediate

Upload: kimama

Post on 23-Feb-2016

23 views

Category:

Documents


0 download

DESCRIPTION

Doing Something Useful with Enterprise Library 3.0. Benjamin Day. Level : Intermediate. About the speaker. Owner, Benjamin Day Consulting, Inc. Email: [email protected] Web: http://www.benday.com Blog: http://blog.benday.com Trainer Visual Studio Team System, Team Foundation Server - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Doing Something Useful with Enterprise Library 3.0

Doing Something Usefulwith Enterprise Library 3.0

Benjamin Day

Level: Intermediate

Page 2: Doing Something Useful with Enterprise Library 3.0

About the speaker

Owner, Benjamin Day Consulting, Inc.– Email: [email protected]– Web: http://www.benday.com– Blog: http://blog.benday.com

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

Page 3: Doing Something Useful with Enterprise Library 3.0

Agenda

Overview Policy Injection Logging Exception Handling Validation

Page 4: Doing Something Useful with Enterprise Library 3.0

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 http://codeplex.com/entlib

Page 5: Doing Something Useful with Enterprise Library 3.0

Caching

Security

Data Access Logging

ExceptionHandling

Ent Lib 3.0 – Application Blocks

Plug-inConfig

Helpers & Design

Instrumen-tation

ObjectBuilder

Cryptography

Core

Policy InjectionValidation

http://tinyurl.com/3d8xfw

Page 6: Doing Something Useful with Enterprise Library 3.0

Data Access Block

Wraps common actions for database access

Handles closing of connections, IDispose calls

Some degree of database independence

Relatively easy to understand

Page 7: Doing Something Useful with Enterprise Library 3.0

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?

Page 8: Doing Something Useful with Enterprise Library 3.0

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

Page 9: Doing Something Useful with Enterprise Library 3.0

Policy Injection Block

Uses magic Mostly elves Some wood nymphs for reliability

Just kidding.

Page 10: Doing Something Useful with Enterprise Library 3.0

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

Page 11: Doing Something Useful with Enterprise Library 3.0

The Overall Design

Page 12: Doing Something Useful with Enterprise Library 3.0

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.CallHandle

rs– Plus, whatever block you’ll reference from PI

Edit app.config/web.config with EntLibConfig.exe

Page 13: Doing Something Useful with Enterprise Library 3.0

Calling into the PI framework

Call PolicyInjection.Create<T>() or one of the overloads

Returns a wrapper to your object Call your methods through the wrapper

Page 14: Doing Something Useful with Enterprise Library 3.0

Configuration

EntLibConfig.exe Uses the

Remoting Policy Injector

Policy consists of– Matching Rules– Handlers

Page 15: Doing Something Useful with Enterprise Library 3.0

Matching Rules

Tells the framework when to run the policy

Page 16: Doing Something Useful with Enterprise Library 3.0

Handlers

Tells the framework what to do when it finds a policy match

Page 17: Doing Something Useful with Enterprise Library 3.0

Logging Handler

Through policy injection framework, intercepts calls

Writes message to log before and/or after

Page 18: Doing Something Useful with Enterprise Library 3.0

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?

Page 19: Doing Something Useful with Enterprise Library 3.0

Logging Block Design

Page 20: Doing Something Useful with Enterprise Library 3.0

Trace Listeners

Page 21: Doing Something Useful with Enterprise Library 3.0

Formatters

Page 22: Doing Something Useful with Enterprise Library 3.0

Demo

Add Policy Injection to an existing app Configure logging

Page 23: Doing Something Useful with Enterprise Library 3.0

Exception Handling Block

Allows you to define how to treat exceptions

Option: Catch and re-throw different exception

Option: Eat the exception

Page 24: Doing Something Useful with Enterprise Library 3.0

Design of the Exception Block

Page 25: Doing Something Useful with Enterprise Library 3.0

Demo

Add exception handling to our app via Policy Injection

Wrap Handlers Replace Handlers

Page 26: Doing Something Useful with Enterprise Library 3.0

Demo

Add Exception Handling to a WCF application

Page 27: Doing Something Useful with Enterprise Library 3.0

The Validation Block

Page 28: Doing Something Useful with Enterprise Library 3.0

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”

Page 29: Doing Something Useful with Enterprise Library 3.0

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

Page 30: Doing Something Useful with Enterprise Library 3.0

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

Page 31: Doing Something Useful with Enterprise Library 3.0

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

Page 32: Doing Something Useful with Enterprise Library 3.0

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

Page 33: Doing Something Useful with Enterprise Library 3.0

Demo

Validate Restaurant with the ValidationFactory

Page 34: Doing Something Useful with Enterprise Library 3.0

The Caching Block

Allows you to cache return values from method, properties

Easy to add with the Policy Injection Block

Page 35: Doing Something Useful with Enterprise Library 3.0

Demo

Populate list of states using the caching block

Page 36: Doing Something Useful with Enterprise Library 3.0

Summary

Policy Injection Block Helps Speed Adoption

Logging Block Exception Handling Block Exception Handling in WCF Validation Block Caching Block

Page 37: Doing Something Useful with Enterprise Library 3.0

About the speaker

Owner, Benjamin Day Consulting, Inc.– Email: [email protected]– Web: http://www.benday.com– Blog: http://blog.benday.com

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