locked
Upgrading to Enterprise Library 5.0 -- Need help! RRS feed

  • Question

  • User-1681489650 posted

    Hi, I am attempting to upgrade from Enterprise Library 3.1 to 5.0 and have read hundreds of articles and looked at lots of examples on the Internet.  I've also looked at the migration papers but I'm still confused.

    Could someone PLEASE show me what the converted code would look like to convert the following code below to support Enterprise Library 5.0 and if there's anything else additionally I'd need to do.

    My code is very similar to this file (which I found by searching the internet) and I have several enterprise library blocks which are similar to this and use the [Assembler] attribute and the IAssembler interface.  So if I can see the actual conversion for this then I should be able to do it for the others.

    Here's the example code file similar to mine:

    //===============================================================================
    // Microsoft patterns & practices Enterprise Library
    // Logging Application Block
    //===============================================================================
    // Copyright © Microsoft Corporation.  All rights reserved.
    // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
    // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
    // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    // FITNESS FOR A PARTICULAR PURPOSE.
    //===============================================================================
    
    using System;
    using System.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder; // Not used in Enterprise Library 5.0
    using Microsoft.Practices.EnterpriseLibrary.Logging.Formatters;
    using Microsoft.Practices.ObjectBuilder; // Not used in Enterprise Library 5.0
    
    namespace Microsoft.Practices.EnterpriseLibrary.Logging.Configuration
    {
    	/// <summary>
    	/// Represents the configuration settings for a <see cref="TextFormatter"/>.
    	/// </summary>
    	[Assembler(typeof(TextFormatterAssembler))] // Assembler attribute not used in Enterprise Library 5.0
    	public class TextFormatterData : FormatterData
    	{
    		private const string templateProperty = "template";
    
    		/// <summary>
    		/// Initializes a new instance of the <see cref="TextFormatterData"/> class with default values.
    		/// </summary>
    		public TextFormatterData()
    		{
    		}
    
    		/// <summary>
    		/// Initialize a new instance of the <see cref="TextFormatterData"/> class with a template.
    		/// </summary>
    		/// <param name="templateData">
    		/// Template containing tokens to replace.
    		/// </param>
    		public TextFormatterData(string templateData)
    			: this("unnamed", templateData)
    		{
    		}
    
    		/// <summary>
    		/// Initialize a new instance of the <see cref="TextFormatterData"/> class with a name and template.
    		/// </summary>
    		/// <param name="name">
    		/// The name of the formatter.
    		/// </param>
    		/// <param name="templateData">
    		/// Template containing tokens to replace.
    		/// </param>
    		public TextFormatterData(string name, string templateData)
    			: this(name, typeof(TextFormatter), templateData)
    		{
    		}
    
    		/// <summary>
    		/// Initialize a new instance of the <see cref="TextFormatterData"/> class with a name and template.
    		/// </summary>
    		/// <param name="name">
    		/// The name of the formatter.
    		/// </param>
    		/// <param name="formatterType">
    		/// The type of the formatter.
    		/// </param>
    		/// <param name="templateData">
    		/// Template containing tokens to replace.
    		/// </param>
    		public TextFormatterData(string name, Type formatterType, string templateData)
    			: base(name, formatterType)
    		{
    			this.Template = templateData;
    		}
    
    
    		/// <summary>
    		/// Gets or sets the template containing tokens to replace.
    		/// </summary>
    		[ConfigurationProperty(templateProperty, IsRequired = true)]
    		public string Template
    		{
    			get
    			{
    				return (string)this[templateProperty];
    			}
    			set
    			{
    				this[templateProperty] = value;
    			}
    		}
    	}
    
    	/// <summary>
    	/// This type supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
    	/// Represents the process to build a <see cref="TextFormatter"/> described by a <see cref="TextFormatterData"/> configuration object.
    	/// </summary>
    	/// <remarks>This type is linked to the <see cref="TextFormatterData"/> type and it is used by the <see cref="LogFormatterCustomFactory"/> 
    	/// to build the specific <see cref="ILogFormatter"/> object represented by the configuration object.
    	/// </remarks>
    	public class TextFormatterAssembler : IAssembler<ILogFormatter, FormatterData> // IAssembler not used in Enterprise Library 5.0
    	{
    		/// <summary>
    		/// This method supports the Enterprise Library infrastructure and is not intended to be used directly from your code.
    		/// Builds a <see cref="TextFormatter"/> based on an instance of <see cref="TextFormatterData"/>.
    		/// </summary>
    		/// <seealso cref="LogFormatterCustomFactory"/>
    		/// <param name="context">The <see cref="IBuilderContext"/> that represents the current building process.</param>
    		/// <param name="objectConfiguration">The configuration object that describes the object to build. Must be an instance of <see cref="TextFormatterData"/>.</param>
    		/// <param name="configurationSource">The source for configuration objects.</param>
    		/// <param name="reflectionCache">The cache to use retrieving reflection information.</param>
    		/// <returns>A fully initialized instance of <see cref="TextFormatter"/>.</returns>
    		public ILogFormatter Assemble(IBuilderContext context, FormatterData objectConfiguration, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
    		{
    			TextFormatterData castedObjectConfiguration = (TextFormatterData)objectConfiguration;
    
    			ILogFormatter createdObject = new TextFormatter(castedObjectConfiguration.Template);
    
    			return createdObject;
    		}
    	}
    }


    Thanks in advance.

    Wednesday, October 17, 2012 2:24 PM

Answers

  • User-201938348 posted

    Instead of using the AssemblerAttribute and creating an Assembler you would now use GetRegistrations in the class itself:

            /// <summary>
            /// Returns the <see cref="TypeRegistration"/> entry for this data section.
            /// </summary>
            /// <returns>The type registration for this data section</returns>
            public override IEnumerable<TypeRegistration> GetRegistrations()
            {
                yield return new TypeRegistration<ILogFormatter>(
                   () => new TextFormatter(this.Template))
                {
                    Name = this.Name,
                    Lifetime = TypeRegistrationLifetime.Transient
                };
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2012 8:54 AM

All replies

  • User-201938348 posted

    Instead of using the AssemblerAttribute and creating an Assembler you would now use GetRegistrations in the class itself:

            /// <summary>
            /// Returns the <see cref="TypeRegistration"/> entry for this data section.
            /// </summary>
            /// <returns>The type registration for this data section</returns>
            public override IEnumerable<TypeRegistration> GetRegistrations()
            {
                yield return new TypeRegistration<ILogFormatter>(
                   () => new TextFormatter(this.Template))
                {
                    Name = this.Name,
                    Lifetime = TypeRegistrationLifetime.Transient
                };
            }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 23, 2012 8:54 AM
  • User-1681489650 posted

    I'm a little confused.  So are you saying that your example above would replace all of the code I posted in my original email?  If not, what should my code look in order to work in Enterprise Library 5?

    Sunday, October 28, 2012 4:49 AM