huysentruitw / SapNwRfc

SAP NetWeaver RFC library for .NET 5, .NET Core and .NET Framework
MIT License
148 stars 43 forks source link

Providing Dynamic Property Name #3

Closed ongun23 closed 4 years ago

ongun23 commented 4 years ago

Can the developer provide a dynamic SAP field name like below for the Contract class?


using System;
using System.Configuration;
using System.Reflection;

namespace SapNwRfc
{
    /// <summary>
    /// Attribute used to set the real SAP RFC property name of a property.
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public sealed class SapNameAttribute : Attribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SapNameAttribute"/> class.
        /// </summary>
        /// <param name="name">The SAP RFC property name.</param>
        /// <param name="config">Look in to the config file for this property value.</param>
        public SapNameAttribute(string name, bool config = false)
        {
            Name = name;
            if (config)
            {
                Name = ConfigurationManager.AppSettings[name];
            }
        }

        /// <summary>
        /// Gets the SAP RFC property name.
        /// </summary>
        public string Name { get; }
    }
}
huysentruitw commented 4 years ago

Do you mean specifying a field name at-runtime?

huysentruitw commented 4 years ago

If yes, I'd be glad to work out a way to make this work, so any suggestion on how you'd like it to work is more than welcome.

ongun23 commented 4 years ago

Hey there, Using an app config file would be much easier for the property field change for many cases. Also, the class is sealed so inheritance is a bit tight.

huysentruitw commented 4 years ago

I had some time to think about this and I don't think it should be part of this library as feels like an opinionated way to store fieldnames in appsettings.json (we cannot support every single use-case of different ways of storing fieldnames). Additionally, it would mean an additional dependency on System.Configuration.

Anyway, I believe you should be able to do what you want to do and so I have removed the sealed modifier from the SapNameAttribute class, which enables you to do:

[AttributeUsage(AttributeTargets.Property)]
public class SapNameFromAppSettings : SapNameAttribute
{
    public SapNameFromAppSettings (string path)
        : base(ConfigurationManager.AppSettings[path])
    {
    }
}

Please look for version 1.0.0.56 or higher of this package.

Thank you for bringing this up!

ongun23 commented 4 years ago

@huysentruitw Great job! I've been using this amazing library for one of a project and it was taking so much time to change property names because of the production and development environment. Because the property field mapping was limited to hand coded. However, your recent development just nailed it! Great job, thanks!

huysentruitw commented 4 years ago

@ongun23 glad this small change helped you out. Feel free to star this project here on github, always appreciated ⭐