using System.Collections.Generic;
namespace System.ComponentModel.DataAnnotations {
///
/// Used to mark an Entity member as an association
///
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public sealed class AssociationAttribute : Attribute {
private string name;
private string thisKey;
private string otherKey;
private bool isForeignKey;
///
/// Full form of constructor
///
/// The name of the association. For bi-directional associations, the name must
/// be the same on both sides of the association
/// Comma separated list of the property names of the key values
/// on this side of the association
/// Comma separated list of the property names of the key values
/// on the other side of the association
public AssociationAttribute(string name, string thisKey, string otherKey) {
this.name = name;
this.thisKey = thisKey;
this.otherKey = otherKey;
}
///
/// Gets the name of the association. For bi-directional associations, the name must
/// be the same on both sides of the association
///
public string Name {
get { return this.name; }
}
///
/// Gets a comma separated list of the property names of the key values
/// on this side of the association
///
public string ThisKey {
get {
return this.thisKey;
}
}
///
/// Gets a comma separated list of the property names of the key values
/// on the other side of the association
///
public string OtherKey {
get {
return this.otherKey;
}
}
///
/// Gets or sets a value indicating whether this association member represents the foreign key
/// side of an association
///
public bool IsForeignKey {
get {
return this.isForeignKey;
}
set {
this.isForeignKey = value;
}
}
///
/// Gets the collection of individual key members specified in the ThisKey string.
///
public IEnumerable ThisKeyMembers {
get {
return GetKeyMembers(this.ThisKey);
}
}
///
/// Gets the collection of individual key members specified in the OtherKey string.
///
public IEnumerable OtherKeyMembers {
get {
return GetKeyMembers(this.OtherKey);
}
}
///
/// Parses the comma delimited key specified
///
/// The key to parse
/// Array of individual key members
private static string[] GetKeyMembers(string key) {
return key.Replace(" ", string.Empty).Split(',');
}
}
}