1 / 85

Getting Started with Enterprise Library 4.x in ASP.NET

Getting Started with Enterprise Library 4.x in ASP.NET. Pawas Goyal Pawas_goyal@rediffmail.com +91 9967502429. Agenda. What is Enterprise Library? Enterprise Library Components /Architecture Common Scenarios Pre Requisite Enterprise Library Component Details Brief Prerequisite

Télécharger la présentation

Getting Started with Enterprise Library 4.x in ASP.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Getting Started withEnterprise 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? • 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. Enterprise Library Components Security Policy Injection Exception Handling Caching Data Access Logging Validation Crypto Plug-in Dependency Config Tools Configuration Core/Design

  9. Config Helpers & Design Instrumen-tation ObjectBuilder Enterprise Library Application Blocks Data Access Logging Caching Core Cryptography Exception Handling Security Policy Injection Validation

  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 practices

  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. Enterprise Library Components Details Caching Crypto Data Access Policy Injection Exception Handling Logging Validation Unity Security

  19. Caching Application Block • Brief • Prerequisite • Sample Code • Scenarios • Issues

  20. Caching Application Block • 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. Caching Application Block • 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;

  22. Caching Application Block 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)));

  23. Caching Application Block • 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. Caching Application Block • 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.

  25. Cryptography Application Block • Brief • Prerequisite • Sample Code • Scenarios • Issues

  26. Cryptography Application Block • 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. Cryptography Application Block • 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.dllMicrosoft.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 ;

  28. Cryptography Application Block 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);

  29. Cryptography Application Block • 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. Cryptography Application Block • 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.

  31. Data Access Application Block • Brief • Prerequisite • Sample Code • Scenarios • Issues

  32. Data Access Application Block • 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. Data Access Application Block • 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.dllMicrosoft.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;

  34. Data Access Application Block 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. Data Access Application Block 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);

  36. Data Access Application Block • 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. Data Access Application Block • 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.

  38. Exception Handling Application Block • Brief • Prerequisite • Sample Code • Scenarios • Issues

  39. Exception Handling Application Block • 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. Exception Handling Application Block • 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;

  42. Exception Handling Application Block 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; }

  43. Exception Handling Application Block • 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. Exception Handling Application Block • 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).

  45. Logging Application Block • Brief • Prerequisite • Sample Code • Scenarios • Issues

  46. Logging Application Block • 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. Logging Application Block • 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;

  48. Logging Application Block 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);

  49. Logging Application Block • 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. Logging Application Block • Issues/Disadvantages • The Logging Application Block formatters do not encrypt logging information • LAB places quite a bit of logic into configuration files.

More Related