Download presentation
Presentation is loading. Please wait.
Published byDorthy McCarthy Modified over 9 years ago
1
Getting Started with Enterprise Library 4.x in ASP.NET Pawas Goyal Pawas_goyal@rediffmail.com +91 9967502429
2
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues References
3
What is Enterprise Library? Enterprise Library is: ◦ A library of application blocks that solve common challenges ◦ A set of helper classes that work in any architectural style ◦ Architectural guidance embodied in code, which ships with full source allowing you to modify and extend it ◦ Available as a free download
4
What is Enterprise Library? What is Enterprise Library? Enterprise Library is NOT: ◦ Part of the.NET Framework ◦ An application framework that imposes an architectural style ◦ A Microsoft product with support, compatibility and localization ◦ Available to purchase
5
Enterprise Library and ASP.NET Can be used in any ASP.NET application Configure just the blocks you require The most useful blocks in ASP.NET are: ◦ Data Access ◦ Caching ◦ Logging ◦ Cryptography ◦ Policy Injection ◦ Validation
6
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues References
7
Enterprise Library Architecture
8
Security Crypto Configuration Data Access LoggingCaching Exception Handling Config Tools Core/Design Enterprise Library Components Policy Injection Validation Dependency Plug-in
9
Enterprise Library Application Blocks CachingCaching SecuritySecurity DataAccessDataAccessLoggingLogging ExceptionHandlingExceptionHandling ConfigHelpers & Design ConfigHelpers Instrumen- tation Object Builder CryptographyCryptography Core Policy Injection ValidationValidation
10
The Application Blocks (1) Caching Application Block ◦ incorporate a cache in applications. Pluggable cache providers are supported. Cryptography Application Block ◦ incorporate hashing and symmetric encryption in their applications. Data Access Application Block ◦ incorporate standard database functionality in their applications. Exception Handling Application Block ◦ consistent strategy for processing exceptions that occur throughout the architectural layers of enterprise applications. Validation Application Block ◦ create validation rules for business objects that can be used across different layers of their applications.
11
The Application Blocks (2) Logging Application Block ◦ include standard logging functionality in their applications. Policy Injection Application Block ◦ The Enterprise Library Policy Injection Application Block provides a mechanism for automatically applying policies to object instances;. Security Application Block ◦ Developers can use this application block to incorporate authorization and security caching functionality in their applications Unity Application Block ◦ Lightweight and extensible dependency injection container with support for constructor, property, and method call injection, as well as instance and type interception (via an extension).
12
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues References
13
Common Scenarios (1) Enterprise Library can be useful in a variety of situations: Enterprise Library provides enough functionality to support many common scenarios that enterprise-level applications must address. Enterprise Library can serve as the basis for a custom library. You can take advantage of the extensibility points incorporated in each application block and extend the application block by supplying new providers. You can also modify the source code for the existing application blocks to incorporate new functionality. You can also add new application blocks to Enterprise Library. You can either develop extensions for existing application blocks and new application blocks yourself or you can use extensions and application blocks developed by others. Enterprise Library is designed so that its application blocks can function independently of each other. You have to add only the application blocks that your application will use; you do not have to add the entire library.
14
Common Scenarios (2) Enterprise Library includes the source code and the unit tests for all application blocks. This means you can modify the application blocks to merge into your existing library or you can use parts of the Enterprise Library source code in other application blocks or applications that you build. Enterprise Library includes documentation, quick start samples, and source code. Hands-on-labs and webcasts are posted as separate downloads on the Enterprise Library Home page. This means that you can use the library as a tool for learning architectural, design, and coding best practicesEnterprise Library
15
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues References
16
Prerequisite For all application blocks except for the Unity Application Block, the Enterprise Library core features, and the configuration tools, the minimum requirements are: ◦ Microsoft Windows XP Professional, Windows Server 2003, Windows Server 2008, or Windows Vista operating system ◦ Microsoft.NET Framework 3.5 or later ◦ Microsoft Visual Studio 2008 development system (any of the following editions): ◦ Standard Edition ◦ Professional Edition ◦ Team Edition for Software Developers ◦ Team Edition for Software Testers ◦ Team Edition for Software Architects ◦ Team Suite
17
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues References
18
Security Crypto Data Access Logging Caching Exception Handling Enterprise Library Components Details Policy Injection Validation Unity
19
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Caching Application Block
20
◦ Brief The Caching application block has been designed to simplify development tasks that implement caching functionality. It provides a thread and exception safe model that give support for local caches that can help improve performance, scalability and availability. The caching block is not designed to replace the ASP.NET cache but to work with it. The caching block provides a number of features that are not available to the ASP.NET cache such as: The ability to use a persistent backing store Multiple methods of setting expiration times Can be used for project types other than web applications The core settings are described in configuration and can be changed without recompilation of the project. In addition to the caching block can be extended to create your own expiration policies and storage mechanisms
21
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Caching Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Caching.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Caching Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Caching; using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations; Caching Application Block
22
Example Code The following code shows how to Add item in Cache ICacheManager productsCache = CacheFactory.GetCacheManager(); string id = "ProductOneId"; string name = "ProductXYName"; int price = 50; Product product = new Product(id, name, price); productsCache.Add(product.ProductID, product, CacheItemPriority.Normal, null, new SlidingTime(TimeSpan.FromMinutes(5))); Caching Application Block
23
◦ Scenarios ◦ A consistent form of caching is required across different application environments. ◦ If there is a requirement for a persistent backing store. ◦ Caching configuration may need to change after deployment. ◦ When one of the following expiration policies of cached data need to be used. absolute time, extended time, sliding time, file dependency or never expired.
24
◦ Issues/Disadvantages ◦ You cannot synchronize caching across a Web farm. ◦ The Caching Application Block does not support encryption of data that is cached in memory. If a malicious user finds a way of compromising the system and accessing the memory of your application's process, he or she would be able to access information stored in the cache. If this is a significant threat to your application, do not store sensitive information, such as credit card numbers or passwords, in the cache. Caching Application Block
25
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Cryptography Application Block
26
◦ Brief The Cryptography application block has been designed to simplify development tasks that implement cryptographic functionality. It encapsulates the available windows security API's simplifying and standardising the way cryptographic functionality is performed within an application. The key design goals of this application block that improve coding and productivity for developers are. ◦ Provide a simple and intuitive interface to the commonly required functionality. ◦ Present a standard consistent model for common cryptography tasks. ◦ Make sure minimal or negligible performance impact compared to manually written cryptography code that accomplishes the same functionality. ◦ Provide a key protection model that can be customized to satisfy your organization's security requirements.
27
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Caching Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click Browse to locate the Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll. Select the assemblies and then click OK to add the reference. To use elements from the Caching Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography ; Cryptography Application Block
28
Example Code The following code shows how to encrypt and decrypt data. This code shows how to use the overloads that accept a string. string encryptedContentsBase64 =Cryptographer.EncryptSymmetric("symmProvider", "SensitiveData"); // Decrypt the base64 encoded string string readableString; readableString = Cryptographer.DecryptSymmetric("symmProvider", encryptedContentsBase64); Cryptography Application Block
29
◦ Scenarios ◦ There is a requirement for secure encryption of data. ◦ There is a requirement for Hash values to be create. E.g. Password Hashes ◦ The encrypted data does not need to be sent across a network and decrypted at the other end. This is better done using Asymmetric encryption ◦ If there is a requirement to standardize how encryption is handled both within an application and across the enterprise.
30
◦ Issues/Disadvantages ◦ There is a requirement to use Asymmetric encryption. This is not supported by the application block ◦ An encryption algorithm provides no security if the encryption algorithm is cracked or is vulnerable to brute force cracking. Cryptography Application Block
31
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Data Access Application Block
32
◦ Brief ◦ The Data Access application block has been designed to simplify development tasks that implement data access functionality, regardless of the source of that data. The Data Access Application Block provides the following benefits: It uses the functionality provided by ADO.NET 2.0 and with it, you can use ADO.NET functionality along with the application block's functionality. It reduces the need to write boilerplate code to perform standard tasks. It helps maintain consistent data access practices, both within an application and across the enterprise. It reduces difficulties in changing the database type. It relieves developers from learning different programming models for different types of databases. It reduces the amount of code that developers must write when they port applications to different types of databases.
33
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Data Access Application Block assembly. In Visual Studio, right- click your project node in Solution Explorer, and then click Add References. Click Browse to locate the Microsoft.Practices.EnterpriseLibrary.Data.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll. Select the assemblies and then click OK to add the reference. To use elements from the Data Access Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Data; Data Access Application Block
34
To configure a connection string ◦ Click the Connection String node. ◦ (Optional) Set the Name property by typing a new name. This is the name of the Connection String node. The default name is Connection String. ◦ (Optional) In the ProviderName property section, change the provider name. Enter the name of the provider or select it from the drop-down list. The default provider name is System.Data.SqlClient. The ProviderName property must be a provider name specified in a DbProviderFactory class. ◦ Update the ConnectionString property as required. For example, this connection string specifies the local SQL Server Express named database instance using itegrated Windows security: ◦ Database=Database;Server=(local)\SQLEXPRESS;Integrated Security=SSPI
35
Example Application Code The following code shows how to call a stored procedure that returns a DataSet. Database db = DatabaseFactory.CreateDatabase(); DbCommand dbCommand = db.GetStoredProcCommand("GetProductsByCategory"); db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, 7); DataSet productDataSet = db.ExecuteDataSet(dbCommand); Data Access Application Block
36
◦ Scenarios The Data Access Block should be used if the application will be using standard data access techniques and there is a need to use a simple standardized method of data access that conforms to Microsoft's Patterns and Practices recommendations. If however the application needs to get data in a specialized way or customizations are required then using ADO. NET directly may be a better option
37
◦ Issues/Disadvantages ◦ DAAB places quite a bit of logic into configuration files. ◦ Does not support Connected Architecture. ◦ Connection string is not stored in encrypted format in the configuration file. ◦ you are using another layer over ADO.NET, which can make debugging a little harder, but the speed loss and complexity are miniscule. Data Access Application Block
38
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Exception Handling Application Block
39
◦ Brief The Enterprise Library Exception Handling Application Block helps developers and policy makers to create a consistent strategy for processing exceptions that occur in all architectural layers of an enterprise application. It does this in the following ways: ◦ It supports exception handling in all architectural layers of an application and is not limited to service interface boundaries. ◦ It allows exception handling policies to be defined and maintained at the administrative level so that policy makers, who might be system administrators as well as developers, can define how to handle exceptions. They can maintain and modify the rules that govern exception handling without changing the application block code. ◦ It invokes exception handlers in a consistent manner. This means that the handlers can be used in multiple places within and across applications.
40
Exception Handling Application Block Brief(continue) ◦ It provides commonly used exception handling functions, such as the ability to log exception information, the ability to hide sensitive information by replacing the original exception with another exception and the ability to maintain contextual information for an exception by wrapping the original exception inside another exception. These functions are encapsulated in.NET classes named exception handlers. ◦ It can combine exception handlers to produce the desired response to an exception, such as logging exception information followed by replacing the original exception with another. ◦ It lets developers create their own exception handlers.
41
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Exception Handling Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click Browse to locate the Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll. Select the assemblies and then click OK to add the reference. To use elements from the Exception Handling Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling; Exception Handling Application Block
42
Example Code The following code shows how to execute the policy named "Data Access Policy" when an exception occurs. try { // Run code. } catch(Exception ex) { bool rethrow = ExceptionPolicy.HandleException(ex, " Data Access Policy"); if (rethrow) throw; } Exception Handling Application Block
43
◦ Scenarios A method should catch exceptions only when it has to perform one or more of the following actions: ◦ Gather information for logging. ◦ Add any relevant information to the exception. ◦ Try to recover ◦ When you need to change policies because of changing security or other operational issues. ◦ When you need to log exception information or display exception information to the user.
44
◦ Issues/Disadvantages It depends on that you have configured all policies that you use in code. This could be a problem if you develop generic components or smaller frameworks because you are suddenly in the situation where you have to trust the user (=the developer) of your components to define all policies that you use in your code or else your code will break. This requires that you document it very well and probably also make some recommended configuration for the components that you deliver as part of the component. Generally the documentation of the entire EntLib has a lot more to wish and I hope that they start publishing better examples and documentation (hopefully before their next release, that will target.NET Framework 2.0). Exception Handling Application Block
45
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Logging Application Block
46
◦ Brief The logging application block simplifies the implementation of common logging functions. It allows the developer to write information to a variety of locations based on the configuration used. These include ◦ The event log ◦ An e-mail message ◦ A database ◦ A message queue ◦ A text file ◦ A WMI event Custom locations using application block extension points
47
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Logging Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Logging.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Logging Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Logging; using Microsoft.Practices.EnterpriseLibrary.Logging.ExtraInformation; using Microsoft.Practices.EnterpriseLibrary.Logging.Filters; Logging Application Block
48
Example Application Code The following code shows how to populate and raise an event in your application. The LogEntry object has a priority of 2 and belongs to both the Trace and UI Events categories. LogEntry logEntry = new LogEntry(); logEntry.EventId = 100; logEntry.Priority = 2; logEntry.Message = "Informational message"; logEntry.Categories.Add("Trace"); logEntry.Categories.Add("UI Events"); Logger.Write(logEntry); Logging Application Block
49
◦ Scenarios The Logging Application Block should be used when: ◦ There is a requirement to log information to the event log, e- mail, a database, a message queue, windows management instrumentation (WMI), or a file. ◦ There is a need to filter logging messages based on category or priority. ◦ The messages need to be formatted. ◦ There is a requirement to change the destination of the message without changing the application code. ◦ A simple, consistent interface for logging within the application and across the enterprise is required
50
◦ Issues/Disadvantages ◦ The Logging Application Block formatters do not encrypt logging information ◦ LAB places quite a bit of logic into configuration files. Logging Application Block
51
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Policy Injection Application Block Policy Injection Application Block
52
Policy Injection Application Block ◦ Brief ◦ The Enterprise Library Policy Injection Application Block provides a mechanism for automatically applying policies to object instances. This helps developers to better manage crosscutting concerns, maximize separation of concerns, and encapsulate behavior. Developers define the set of policies for the target classes and their members through configuration of the Policy Injection Application Block or by applying attributes to individual members of the target class. ◦ The Policy Injection Application Block ships with pre-built handlers for these application blocks that speed up development when using Enterprise Library to help manage crosscutting concerns. Developers can also create custom handlers and policies that carry out almost any required interception processing for methods and properties of their objects.
53
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Policy Injection Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.PolicyInjection.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Policy Injection Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.PolicyInjection; using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers; Policy Injection Application Block
54
Example code Let's say we have a policy for our store that whenever an order is returned it must be logged along with the reason it was returned. For simplification, here are my classes: public class Order : IOrder { public void Return(string reason) { // Do something... } } public interface IOrder { void Return(string reason); } Policy Injection Application Block
55
One option is to use the Enterprise Library Logging Application Block directly and somewhere during the Return Process just make a call that logs the information:Logging Application Block LogEntry logEntry = new LogEntry(); logEntry.Message = "...“ logEntry.Categories.Add("..."); //... Logger.Write(logEntry); With the Policy Injection Application Block, however, we can avoid this extra code by defining a policy that whenever a call is made to the Return Method on IOrder we should log a message.
56
Policy Injection Application Block There are numerous ways to match policies to methods, but in this case I am going to use the TagMatchingRule which means I just have to add a tag attribute to the Return Method on the Interface. Hence rather than writing all the LogEntry code above and explicitly calling the logging application block, I will just add the following to the IOrder Interface: public interface IOrder { [Tag("Log")] void Return(string reason); }
57
Policy Injection Application Block ◦ Scenarios ◦ The Policy Injection Application Block provides a ready-built solution that is easy to implement in new and existing applications, particularly in applications that already take advantage of the features of the Enterprise Library. ◦ The Policy Injection Application Block provides a solution that allows developers, operators, and administrators to create, modify, remove, and fine-tune interception policies though configuration, generally without requiring any changes to the code or recompilation of the application. This limits the chances of introducing errors into the code, simplifies versioning, and reduces downtime.
58
◦ Issues/Disadvantages Some functional limitations arise when using the Policy Injection Application Block to call routines that implement crosscutting concerns (such as validation, logging, and authorization) from handlers instead of directly from custom application code: Threaded applications where multiple calls passing through the same handler could corrupt the internal state. The application block caches policies as it initializes; the matching rules are not re-evaluated on each call. Therefore, matching rules cannot contain dynamic logic that changes over time. Policy Injection Application Block
59
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Security Application Block
60
Brief Developers frequently write applications that must authorize users using one or more security providers such as Microsoft Active Directory directory service, Authorization Manager, Active Directory Application Mode (ADAM), and custom authorization providers. These applications may also need to cache authentication or authorization data for the duration of a logon session. The Security Application Block simplifies these tasks by handling them in a consistent manner, abstracting the application code from the specific security providers. You can even change underlying providers through configuration without changing the underlying application code. The Security Application Block provides code that will help you with the following scenarios: ◦ Authorization ◦ Caching security-related credentials
61
Security Application Block ◦ The Security Application Block has the following features: ◦ It reduces the requirement to write boilerplate code to perform standard tasks. ◦ It helps maintain consistent security practices, both within an application and across the enterprise. ◦ It eases the learning curve for developers by using a consistent architectural model across the various areas of functionality provided. ◦ It provides implementations that you can use to solve common application security problems. ◦ It is extensible; it supports custom implementations of security providers.
62
Security Application Block The security application block has been designed to address the following areas. ◦ Authorisation ◦ Role management ◦ Profile management ◦ Caching principals ( Caching profile information and security-related credentials ) The security application block focuses on a role-based security model. The Security Application Block simplifies these tasks by handling them in a consistent manner and abstracting the application code from the specific security providers. Authentication, Authorisation, Roles and Profile Providers can work completely independently of one another but they are closely related
63
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Security Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Security.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Security Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Security; Security Application Block
64
Example Application Code The following code shows how to determine if a user is authorized to perform a task. IPrincipal principal = new GenericPrincipal(new GenericIdentity("Username"), new string[]{"Manager"}); IAuthorizationProvider ruleProvider = AuthorizationFactory.GetAuthorizationProvider("RuleProvider"); // Determine whether user is authorized for the rule defined as "Print Document". bool authorized = ruleProvider.Authorize(principal, "Print Document"); Security Application Block
65
◦ Scenarios The Security Application Block includes implementations of the following functions: ◦ Authorization ◦ Security-related caching and session management If your applications require the provided implementations, you can use the application block to provide this functionality. However, the application block is also designed to be extensible and includes generic providers for each function. You can adapt the providers to meet your own security requirements.
66
◦ Issues/Disadvantages ◦ SAB places quite a bit of logic into configuration files. Security Application Block
67
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Unity Application Block
68
◦ Brief The Unity Application Block (Unity) is a lightweight, extensible dependency Injection container with support for constructor, property and method call injection. It provides developers with the following advantages: It provides simplified object creation, especially for hierarchical object structures and dependencies, which simplifies application code. It supports abstraction of requirements, this allows developers to specify dependencies at run time or in configuration and simplify management of crosscutting concerns. It increases flexibility by deferring component configuration to the container. It has a service location capability, this allows clients to store or cache the container. This is especially useful in ASP.NET Web applications where the developers can persist the container in the ASP.NET session or application.
69
Security Crypto Configuration Data Access LoggingCaching Exception Handling Config Tools Core/Design Enterprise Library Components Policy Injection Validation Dependency Plug-in
70
Enterprise Library Components
71
◦ Prerequisite For all application blocks except for the Unity Application Block, the Enterprise Library core features, and the configuration tools, the minimum requirements are: ◦ Microsoft Windows XP Professional, Windows Server 2003, Windows Server 2008, or Windows Vista operating system ◦ Microsoft.NET Framework 2.0, 3.0, or 3.5 Microsoft Visual Studio 2005 or Visual Studio 2008 development system (any of the following editions): Standard Edition Professional Edition Team Edition for Software Developers Team Edition for Software Testers Team Edition for Software Architects Team Suite Unity Application Block
72
◦ Sample Code ◦ First prepare application Add a reference to the Unity Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.Unity.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Unity Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using Microsoft.Practices.Unity.StaticFactory;
73
Example Code Unity Application Block
74
◦ Scenarios Dependency injection provides opportunities to simplify code, abstract dependencies between objects, and generate dependent object instances automatically. However, the process may have a minor impact on performance, and it can increase complexity where only simple dependencies exist. In general, you should use the Unity Application Block in the following situations: ◦ Your objects and classes may have dependencies on other objects or classes. ◦ Your dependencies are complex or require abstraction. ◦ You want to take advantage of constructor, method, or property call injection features. ◦ You want to manage the lifetime of object instances. ◦ You want to be able to configure and change dependencies at run time. ◦ You want to be able to cache or persist the dependencies across postbacks in a Web application.
75
◦ Issues/Disadvantages ◦ Complex to understand ◦ Places a little bit coding logic in configuration files. ◦ Before use this you should be have great knowledge about your object there dependencies and relations. In general scenario it’s not possible. Unity Application Block
76
◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Scenarios ◦ Issues Validation Application Block Validation Application Block
77
Validation Application Block ◦ Brief The Enterprise Library Validation Application Block provides useful features that allow developers to implement structured and easy-to- maintain validation scenarios in their applications. Any application that accepts input either from users or from other systems must ensure that the information is valid in terms of some set of rules that you specify. For example phone number You can also group validators together in a rule set. A rule set allows you to validate a complex object or graph by composing different validators of different types and applying them to elements in the object graph. Examples of these elements include fields, properties, and nested objects.
78
Validation Application Block Brief (Continue) By using the Validation Application Block, you can perform validation and create rule sets in the following three ways: Using configuration Using attributes Using code In addition, the Validation Application Block includes adapters that allow you to use the application block with the following technologies: ASP.NET Windows Forms Windows Communications Framework (WCF)
79
◦ Prerequisite No Specific Requirements ◦ Sample Code ◦ First prepare application Add a reference to the Validation Application Block assembly. In Visual Studio, right-click your project node in Solution Explorer, and then click Add References. Click the Browse tab and find the location of the Microsoft.Practices.EnterpriseLibrary.Validation.dll Microsoft.Practices.EnterpriseLibrary.Common.dll Microsoft.Practices.ObjectBuilder2.dll Select the assemblies and then click OK to add the reference. To use elements from the Unity Application Block without fully qualifying the element reference, add the following using Microsoft.Practices.EnterpriseLibrary.Validation; using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; Validation Application Block
80
Example Application Code This example shows how to associate a validator with an object and then validate that object. public class Customer { [StringLengthValidator(0, 20)] public string CustomerName; public Customer(string customerName) {this.CustomerName = customerName; } } public class MyExample { public static void Main() { Customer myCustomer = new Customer("A name that is toolong"); ValidationResults r = Validation.Validate (myCustomer); If (!r.IsValid) {throw new InvalidOperationException("Validation error found."); }}} Validation Application Block
81
◦ Scenarios o The Validation Application Block allows you to encapsulate validation best practices into easily maintainable code that you can reuse. Encapsulation also allows you to separate the application code from the validation logic. o In some situations, you may be able to update the validation logic without redeploying the application. o where the application block works well is when your validation code must work across multiple layers of the application's architecture. o In very simple cases, when you only need to validate a few objects, you may not want to incur the overhead of adding the application block.
82
◦ Issues/Disadvantages ◦ when you need only to validate a few objects then you have to include all VAB references. Validation Application Block
83
Agenda What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details ◦ Brief ◦ Prerequisite ◦ Sample Code ◦ Issues ◦ Key Scenarios References
84
Book covering Enterprise Library: ◦ "Effective Use of Microsoft Enterprise Library: Building Blocks for Creating Enterprise Applications and Services" (Addison-Wesley ISBN 0-321-33421- 3). All about Enterprise Library: ◦ http://msdn.microsoft.com/hi-in/library/cc467894(en- us).aspx http://msdn.microsoft.com/hi-in/library/cc467894(en- us).aspx Set up for Enterprise Library: ◦ 192.168.2.7\Software\Utility\Enterprise Library Contact: Pawas Goyal
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.