Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Articles » General »
Enterprise Library Application Blocks
|
Enterprise Library is a software component, developed by an organization "Microsoft Patterns and Practices". Following are the types of Application Blocks found in Enterprise Library v3.1
1. Caching Application Block 2. Data Access Application Block 3. Cryptography Application Block 4. Exception Handling Application Block 5. Logging Application Block 6. Policy Injection Application Block 7. Security Application Block 8. Validation Application Block
Caching Application Block
ASP.NET Provides Object Caching functionality to place any Objects in cache. Objects can be of any type: data type, class, dataset etc. you can add and retrieve values from Object cache using key/value pairs.
Besides using Object caching functionality of the ASP.NET, User can use the Caching Application Block of Enterprise Library for better performance. The Enterprise Library Caching Application Block lets developers to introduce a local cache (a local memory) in their applications.
This Local Cache will be used to store the item locally and can be used to perform various operations like addition, extraction of Cached data etc. User can load the item in two ways into Cache Manager provided by Enterprise library. 1) Proactively – The Entire data (e.g. the entire XML specific data) is loaded into cache with the specific key. 2) Reactively – The user specific data (as requested by the Application) is loaded into Cache with the specific key.
Explanation of Config Section with example Application Code: C#
CacheManager tempCache= CacheFactory.GetCacheManager("Load Cache"); string name = "John"; int id = 1; string address = "New York"; CacheAdd Adde = new CacheAdd(); tempCache.Add(name,id,address); //addtion of item in a cache Adde = (CacheAdd) tempCache.GetData(id); //Retrieve item from a cache using specific key //(here id is taken specific key tempCache.Flush(); //remove item from a cache
Data Access Application Block
1) Developers can use the Enterprise Library Data Access Application Block which simplifies common data access functionality. Applications can use this application block in a variety of situations, such as reading data from database, displaying the data, passing data through application layers, and submitting changed data back to the database system.
2) The Data Access Application Block uses the classes provided by ADO.NET2.0 needed to abstract the Data provider. Because of this, applications can be ported from one database type to another without modifying the client code. Using this Data Access Block user can retrieve multiple rows using data reader, dataset, can perform transaction, can also perform insert and update operation.
3) Application Code can refer to particular database by Connection string name. Each named database has its connection information in a Web.config file in case of web Application or App.Config file incase of Windows Application. Hence by changing the settings in the configuration file, developers can point to different database without touching the source code. It is useful when any application wants to change its database servers in future.
Explanation of Config Section with example Application Code: C#
public bool sampletransaction(int id, int name) { bool result = false; Database db = DatabaseFactory.CreateDatabase(); // DataBase factory object is created //with respect to database service provided
using (DbConnection connection = db.CreateConnection()) { connection.Open(); DbTransaction transaction = connection.BeginTransaction();
try {
OracleParameter[] attributeparam = { // In Parameters for SP new OracleParameter("In_nid",OracleDbType.Int32,ParameterDirection.Input), new OracleParameter("In_aname",OracleDbType.Varchar2, ParameterDirection.Input)
};
attributeparam [0].Value = id; attributeparam [1].Value = name; //dbcmd1.ExecuteNonQuery(); db.ExecuteNonQuery(CommandType.StoredProcedure,””,attributeparam); OracleParameter[] attributeparameter = { // In Parameters for SP new OracleParameter("In_nid",OracleDbType.Int32,ParameterDirection.Input),
};
attributeparam [0].Value =id;
db.ExecuteNonQuery(CommandType.StoredProcedure,”< Stored procedure name>”,attributeparameter); // Commit the transaction transaction.Commit(); result = true; } catch { // Rollback transaction transaction.Rollback(); } connection.Close(); return result; } }
Benefits: 1) Data Access Functionality is achieved using this block. This minimizes the developer to write excessive Data Access code in their Application. 2) It reduces the amount of code that developers must write when they port applications to different types of databases. 3) You can customize this block to access data from different data Providers.
Exception Handling Application Block
When you Program encounters an exceptional circumstances, it throws exception and execution of the current method halts and the calling method will get a chance to handle the exception. If none of the calling methods handles it, the exception will ultimately be handled by CLR, which will abruptly terminate your program.
The Enterprise Library Exception Handling Application Block lets developers to process such type of exceptions that occur in all architectural layers of an application.
This block 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. User can also create their own exception handlers.
The Exception Handling Application Block includes four exception handlers: 1) Wrap handler. This exception handler wraps one exception around another. 2) Replace handler. This exception handler replaces one exception with another. 3) Logging handler. This exception handler formats exception information, such as the message and the stack trace. Then the logging handler gives this information to the Enterprise Library Logging Application Block so that it can be published. The below code describes how exception is handled and propagated.
catch (NullReferenceException ex) { db.RollBackTransaction(); ExceptionPolicy.HandleException(ex, " ComponentException"); } catch (ArgumentException ex) { db.RollBackTransaction(); ExceptionPolicy.HandleException(ex, "ComponentException"); } catch (OracleException ex) { db.RollBackTransaction(); ExceptionPolicy.HandleException(ex, "ComponentException"); } catch (ArithmeticException ex) { db.RollBackTransaction(); ExceptionPolicy.HandleException(ex, "ComponentException"); } catch (InvalidCastException ex) { db.RollBackTransaction(); ExceptionPolicy.HandleException(ex, "ComponentException"); }
The below code describes how an exception can be wrapped with another exception:
try { WrapException(); } catch (Exception ex) { wrappedException(ex); }
public bool WrapException () { try { } catch(Exception ex) { ExceptionPolicy.HandleException(ex, "Exception Wrap"); } return true; }
private void wrappedException (Exception ex) { try { bool rethrow = ExceptionPolicy.HandleException(ex, "Wrapped"); if (rethrow) { Application.Exit(); } } catch { Application.Exit(); } }
Benefits 1) 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. 2) It lets developers create their own exception handlers. 3) The Logging Application Block uses the appropriate Exception handlers to log Exception information and display Exception policy accordingly.
Logging Application Block
The Enterprise Library Logging Application Block lets developers incorporate standard logging functionality in their applications. Applications can use the Logging Application Block to log events to a variety of locations:
The event log E-mail messages A database A message queue A file
Using Logging block, developer can log an event information, can populate a log message with additional information, can trace the log information. The below code describes how the user can create and log the information to Logging Block
public void sampleLog() { try { LogEntry logEntry = new LogEntry(); logEntry.EventId = 100; logEntry.Priority =0; logEntry.Message = " A new Log Events will be captured "; logEntry.Categories.Clear(); logEntry.Categories.Add("trace"); }
Logger.Write(logEntry); // Writes the log entry. catch (Exception ex) { ExceptionPolicy.HandleException(ex, "ComponentException"); } }
The below code describes how the developer trace the method.
public void SampleTrace() { try { using (new Tracer("Trace")) { SampleLog(); // the above code will get executed. } } }
The Call Stack results: SampleTrace SampleLog
This trace file can be displayed in text format. The trace file will be useful when the developer get stuck in finding in which method the exception is unhandled.
Benefits 1) It helps maintain consistent logging practices, both within an application and across the enterprise. 2) It provides implementations that you can use to solve common application logging problems.
AttachmentsEnterprise Library v3.1 Application Blocks (22607-19823-Enterprise Library V3.1 Application Blocks.doc)
For more details, visit http://msdn.microsoft.com/en-us/library/cc511823.aspx
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|