Inheritance and Attribute Values
I am populating PDF applications using classes that represent PDF. I have a property for a textbox in pdf, and I am using attributes to specify the pdf textbox to which the property is bound. All my PDFs have the same properties, but the pdf uses different names for the text fields. So the only solution I could think of was to create a base class, and each type of application extends my base class and overrides each of the properties only to throw a new attribute value on it. Is there an easier way?
Example (note that the only difference between Application1 and Application2 is that 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 source to share
If you are going to use attributes, I would say that the base class was not the right choice. To define such a schema, I would choose Interfaces over a base class. There is less code to maintain, and it gives 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 can use the idea of a base class to contain a dictionary that maps a property to a textbox and just sets it in a derived class ... This would be really really efficient if you have a large number of properties in each form for which you need to do this.
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");
}
}
It actually takes more context to say which are good ideas. The use case would lead to specifics. For example, a dictionary can help you a lot more if it maps PropertyInfo to a string name in a PDF field, it just depends on how you use it.
a source to share
If the value is indeed part of the system, it may have the property:
public string AccountNumber {get;set;}
public string AccountNumberTextField {get;set;}
Then you can have two instances Application
(same type) just with different properties. It really depends on the context.
Eric Lippert has a good blog on when to (rather than use) attributes: Properties vs. Attributes
a source to share