This question is locked. New answers and comments are not allowed.
some time ago I realized there is a bug, but I found a workaround so it wasn't urgent.
however, bug is a bug (or maybe my knowledge is poor?)
we have two classes, order and modification. modification references order, order has list of modification and wants to be notified (trackedbindinglist) that list has changed. Nothing like this happens.
I tried to track the bug and it appeared that really deeply withing openaccess code when the class is "recreated" from its metadata, the type IList<Modification> is recreated as TrackedList<Modification> Modifications. the workaround is to implicitly declare modifications as TrackedBindingList<Modification> Modifications { get; set; }. The bad side is that then we can't have meny-to-many relationships of trackedbindinglist - withopposite takes just IList<T> as an argument.
I attach full (simplified) code of the classes.
however, bug is a bug (or maybe my knowledge is poor?)
we have two classes, order and modification. modification references order, order has list of modification and wants to be notified (trackedbindinglist) that list has changed. Nothing like this happens.
[Test]
public void Modification_gets_changed()
{
var orderRepository = Container.Resolve<
IRepository
<Order>>();
var order = new Order();
bool flag = false;
orderRepository.Add(order);
UnitOfWork.Commit();
(order.Modifications as IBindingList).ListChanged += (s, e) => flag = true;
order.Modifications.Add(new Modification() {Order = order});
Assert.IsTrue(flag);
}
I tried to track the bug and it appeared that really deeply withing openaccess code when the class is "recreated" from its metadata, the type IList<Modification> is recreated as TrackedList<Modification> Modifications. the workaround is to implicitly declare modifications as TrackedBindingList<Modification> Modifications { get; set; }. The bad side is that then we can't have meny-to-many relationships of trackedbindinglist - withopposite takes just IList<T> as an argument.
I attach full (simplified) code of the classes.
public class Order
{
public Order()
{
Modifications = new TrackedBindingList<
Modification
>();
}
[RaisePropertyChanged]
public int Id { get; set; }
//public TrackedBindingList<
Modification
> Modifications { get; set; }
public IList<
Modification
> Modifications { get; set; }
public Modification LastModification
{
get { return Modifications.LastOrDefault(); }
}
public static MappingConfiguration<
Order
> CreateConfiguration()
{
var mapping = new MappingConfiguration<
Order
>();
mapping.MapType(o => new
{
Id = o.Id,
}).ToTable("Orders");
mapping.HasProperty(o => o.Id).IsIdentity(KeyGenerator.Autoinc);
return mapping;
}
}
public class Modification
{
public Modification()
{
OrderId = null;
ModificationDate = DateTime.Now;
}
[RaisePropertyChanged]
public int Id { get; set; }
[RaisePropertyChanged]
public DateTime ModificationDate { get; set; }
[RaisePropertyChanged("Order")]
public int? OrderId { get; set; }
[RaisePropertyChanged("OrderId")]
public Order Order { get; set; }
public static MappingConfiguration<
Modification
> CreateConfiguration()
{
var orderConfiguration = new MappingConfiguration<
Modification
>();
orderConfiguration.MapType(o => new
{
Id = o.Id,
ModificationDate = o.ModificationDate,
OrderId = o.OrderId,
}).ToTable("Modifications");
orderConfiguration.HasProperty(m => m.Id).IsIdentity(KeyGenerator.Autoinc);
orderConfiguration.HasAssociation(m => m.Order)
.WithOpposite(o => o.Modifications)
.HasConstraint((m, o) => m.OrderId == o.Id);
return orderConfiguration;
}
}
[Test]
public void Modification_gets_changed()
{
var orderRepository = Container.Resolve<
IRepository
<Order>>();
var order = new Order();
bool flag = false;
orderRepository.Add(order);
UnitOfWork.Commit();
(order.Modifications as IBindingList).ListChanged += (s, e) => flag = true;
order.Modifications.Add(new Modification() {Order = order});
Assert.IsTrue(flag);
}