Luka
Luka

Reputation: 4211

Nhibernate Set how to intercept Add() method

I need a solution for this: How to intercept ISet.Add method so I don't need to do children.Parent=parent, but jus parent.Children.Add(children);

public class MyClass
{
public MyClass Parent;
public ISet<MyClass> Childrens;
}

I want to do this:

var mc = new MyClass();
var mc2 = new MyClass();
mc.Childrens.Add(mc2);

and not

var mc = new MyClass();
var mc2 = new MyClass();
mc.Childrens.Add(mc2);
mc2.Parent=mc;

Upvotes: 0

Views: 205

Answers (2)

dotjoe
dotjoe

Reputation: 26940

I simply create an AddChild method which does that.

You could also expose the public property as readonly collection as Frederik demonstrates...

What is the best practice for readonly lists in NHibernate

Upvotes: 3

ma7moud
ma7moud

Reputation: 423

public IList<MyClass> Childrens

Upvotes: 0

Related Questions