Anybody?

2009/12/2 Dmitiry Nagirnyak <[email protected]>

> Hi,
>
> I need to map 2 classes: Blog and Post.
> There is an association (blog.Posts-post.BelongsTo) with custom collection
> (that supports notification if items were added/removed).
>
> Now the questions:
>
>    - What modification should be done to map the classes?
>    - What XML mapping should look like?
>
> The only condition is that the tests below should not be touched and
> passing.
>
> Thanks a lot.
> Dmitriy.
>
> The code:
>
>     public class Post {
>         private Blog belongsTo;
>         public virtual Blog BelongsTo {
>             get {
>                 return belongsTo;
>             }
>             set {
>                 if (value == null) {
>                     if (belongsTo != null)
>                         belongsTo.Posts.Remove(this);
>                 }  else
>                     value.Posts.Add(this);
>                 belongsTo = value;
>             }
>         }
>     }
>
>     public class Blog {
>         public Blog() {
>             posts = new SubscribableCollection<Post>();
>             posts.OnAdded += item => {
>                 if (item.BelongsTo != this && item.BelongsTo != null)
>                     item.BelongsTo.Posts.Remove(item);
>                 item.BelongsTo = this;
>             };
>             posts.OnRemoved += item => item.BelongsTo = null;
>         }
>
>         private SubscribableCollection<Post> posts;
>         public virtual ICollection<Post> Posts {
>             get {
>                 return posts;
>             }
>         }
>     }
>
>
>     public class SubscribableCollection<T> : ICollection<T> {
>         public event OnItemDelegate<T> OnAdded;
>         public event OnItemDelegate<T> OnRemoved;
>         // The implementation is stripped, you get what it does
>     }
>
>
>
>
>     [TestFixture]
>     public class OneToMany {
>
>         [Test]
>         public void Assign() {
>             var b = new Blog();
>             var p = new Post();
>
>             p.BelongsTo = b;
>
>             Assert.That(b.Posts.First(), Is.SameAs(p));
>         }
>
>         [Test]
>         public void ReAssign() {
>             var b1 = new Blog();
>             var b2 = new Blog();
>             var p = new Post();
>
>             p.BelongsTo = b1;
>             p.BelongsTo = b2;
>
>             Assert.That(b2.Posts.First(), Is.SameAs(p));
>             Assert.That(b1.Posts, Is.Empty);
>         }
>
>         [Test]
>         public void UnAssign() {
>             var b = new Blog();
>             var p = new Post();
>
>             p.BelongsTo = b;
>             p.BelongsTo = null;
>
>             Assert.That(b.Posts, Is.Empty);
>         }
>
>         [Test]
>         public void AddToList() {
>             var b = new Blog();
>             var p = new Post();
>
>             b.Posts.Add(p);
>             Assert.That(p.BelongsTo, Is.SameAs(b));
>         }
>
>         [Test]
>         public void AddDuplicateToList() {
>             var b = new Blog();
>             var p = new Post();
>
>             b.Posts.Add(p);
>             b.Posts.Add(p);
>             Assert.That(b.Posts.Count, Is.EqualTo(1));
>         }
>
>         [Test]
>         public void MoveToOtherList() {
>             var b1 = new Blog();
>             var b2 = new Blog();
>             var p = new Post();
>
>             b1.Posts.Add(p);
>             b2.Posts.Add(p);
>
>             Assert.That(p.BelongsTo, Is.SameAs(b2));
>             Assert.That(b1.Posts, Is.Empty);
>             Assert.That(b2.Posts, Is.Not.Empty);
>         }
>
>         [Test]
>         public void RemoveFomList() {
>             var b = new Blog();
>             var p = new Post();
>
>             b.Posts.Add(p);
>             b.Posts.Remove(p);
>             Assert.That(p.BelongsTo, Is.Null);
>         }
>     }
>

--

You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.


Reply via email to