adapter 2.0
sql server
asp.net 2.0
Summary:
I receive a TargetOfInvocation exception if the concrete types are heterogenious
IList<IEquipmentSummaryDTO> results = new List<IEquipmentSummaryDTO>();
GridView.DataSource = results;
GridView.DataBind();
Details:
I have an entity hierachy
EquipmentStatus
+-- PrepareStatus
+-- DeployStatus
+-- DisposeStatus
my interface which summarizes a status
public interface IEquipmentSummaryDTO
{
int AssetInstanceId { get; }
int AssetTag { get; }
string Description { get; }
string EquipmentType { get; }
string Location { get; }
string Status { get; }
DateTime? DateToReview { get; }
IEnumerable<ICustodianDTO> Custodians { get; }
}
Implement interface on supertype
public partial class EquipmentStatusEntity : IEquipmentSummaryDTO
{
#region IEquipmentSummaryDTO Members
public int AssetInstanceId { get { return this.Id; } }
public int AssetTag { get { return this.Equipment.UserDefinedId; } }
public string Description { get { return this.Equipment.Description; } }
public string EquipmentType { get { return this.Equipment.EquipmentType.Description; } }
public virtual string Location { get { return string.Empty; } }
public virtual string Status { get { return string.Empty; } }
public virtual DateTime? DateToReview { get { return null; } }
public virtual IEnumerable<ICustodianDTO> Custodians { get { return new List<ICustodianDTO>().AsReadOnly(); } }
#endregion
}
Subtypes override virtual properties.
Within my BLL/Managers I have
public interface IEquipmentStatusManager
{
IEnumerable<IEquipmentStatusDTO> FetchSummaryStatuses(DateTime asOf);
}
Each subtype has a manager which implements this interface.
In my PAL I have the following code to populate my gridview
List<IEquipmentSummaryDTO> results = new List<IEquipmentSummaryDTO>();
foreach (EntityType type in types)
{
IEquipmentStatusManager manager = EquipmentStatusManagerFactory.Create(type);
manager.EquipmentTypes = this.view.SelectedEquipmentTypes;
results.AddRange(manager.FetchAllStatusSummary(this.date));
}
results.Sort(this.sorterToUse);
this.view.Grid.BindTo(results), results.Count;//results and virtual item count
For testing purposes my list returns 3 results (one of each status)
On the last line a get a traget of invocation error when binding my 2nd dto in the list.
I found this post
[http://www.winterdom.com/weblog/2006/07/27/ITypedListAndPropertyDescriptors.aspx]
on creating a TypedBindingList<T>, but that does not resolve my issue either. I implmemented it by changing my last line of code above to
this.view.Grid.BindTo(new TypedBindingList<IEquipmentSummaryDTO>(results), results.Count);
I have read that I need to implement ITypedList (which the post above outlines), but I'm not sure if I'm doing it correctly (using the TypedBindingList<T> object).
2 quick test I did which removed the error, but don't fulfill my requirements:
1. remove the virtual keyword from the interfaced properties on EquipmentStatusEntity
2. return a collection of 1 subtype.
I could:
remove the interface from the supertype.
create an EquipmentSummaryDTO object which implments the interface
add a constructor for each subtype, but that seems to defeat the purpose of the interface.
I also found this post by Frans [http://weblogs.asp.net/fbouma/articles/115837.aspx] I'm not quite sure how to apply this to my scenario.