tags:

views:

100

answers:

3

I'm filling PDF applications using classes that represent the pdf. I have a property per textbox on the pdf and am using attributes to specify the pdf textbox that the propery maps to. All of my PDF's have the same properties, but the pdf's use different names for the textboxes. So, the only solution I could think of was to create a base class and have each application type extend my base class and override each of the properties just to throw the new attribute value on it. Is there a simpler way?

Example (Notice the only difference between Application1 and Application2 is the ITextField value changes from "TextBox1" to "TextBox2":

public class Application
{
    private string accountNumber;
    public virtual string AccountNumber
    {
        get { return this.accountNumber; }
        set { this.accountNumber = value; }
    }
}

public class Application1 : Application
{
    [ITextField("TextBox1")]
    public override string AccountNumber
    {
        get
        {
            return base.AccountNumber;
        }
        set
        {
            base.AccountNumber = value;
        }
    } 
}

public class Application2 : Application
{
    [ITextField("TextBox2")]
    public override string AccountNumber
    {
        get
        {
            return base.AccountNumber;
        }
        set
        {
            base.AccountNumber = value;
        }
    }
}

Thanks

A: 

I know this is a poor answer, but that seems like a pretty good method. It also should work well for changes in the future.

JasonRShaver
A: 

If the value is truly part of the system, then perhaps have it as a property:

public string AccountNumber {get;set;}
public string AccountNumberTextField {get;set;}

Then you can have two Application instances (same type) simply with different properties. It really depends on the context.

Eric Lippert has a good blog on when to (and not to) use attributes: Properties vs. Attributes

Marc Gravell
I think this is also a good answer... just depends on how he is using it.
Brian ONeil
A: 

If you are going to use attributes, I would say that the base class was not the right choice. To define a scheme like this I would choose Interfaces over the base class. There is less code to maintain and it nets you the same effect.

public interface IApplication
{
    string AccountNumber { get; set; }        
}

public class Application1 : IApplication
{
    [ITextField("TextBox1")]
    public string AccountNumber { get; set; }
}

public class Application2 : IApplication
{
    [ITextField("TextBox2")]
    public override string AccountNumber { get; set; }
}

Also, you could use the base class idea to contain a dictionary that maps property to text field and just set it up in the derived class... This would only really be efficient if you have a large number of properties in each form that you need to do this for.

public abstract class Application
{
    public Application() { }

    private Dictionary<string, string> mappings = new Dictionary<string, string>();

    public Dictionary<string, string> Mappings
    {
        get { return mappings; }
    }

    public string AccountNumber { get; set; }

    protected abstract void ProvideMappings();        
}


public class Application1 : Application
{
    protected override void ProvideMappings()
    {
        Mappings.Add("AccountNumber", "TextBox1");
    }
}

public class Application2 : Application
{
    protected override void ProvideMappings()
    {
        Mappings.Add("AccountNumber", "TextBox2");
    }
}

Really need more context to say which if any of these are good ideas. The use case would drive the specifics. For instance, the dictionary may help you a lot more if it maps PropertyInfo to the string name of the PDF field, it just depends on how you are using it.

Brian ONeil
I have 6 different applications with about 120 fields each. I think I'm going to go the base class route. I like the idea of only having to provide mappings for each. Also, I will only need to write the code to define and set the properties once in the base class.
Striker