开发者

Which of these work in nhibernate?

I'm just about to start digging into nHibernate and I'm hoping you can give me a little head start here.

I believe the POCO's must have a parameterless public constructor (?). And for child related collections, use IList (although I've seen IBag in some examples) (?)

I'm also interested in what access I can give or deny publicly on objects. Here's an example:

public class Poco
{
    private IList<ChildPoco> _childPocos;

    //1) can use IEnumerable? Or have to use IList? Or third way?
    public IEnumerable<ChildPoco> ChildPocosEnumerable
    {
        get { return _childPocos.AsEnumerable(); }
    }

}

public class ChildPoco
{
    private string _name;
    public string Name
    {
        get { return _name; }
        //2) protected/public/private set?
    }
}

So, above you can see //1) and //2) can you just explain what my options here are that nhibernate will support?

A little extra info if it helps. The desire at 1) to use IEnumerable is to make the public face of the collection read only.

The same is true of 2) with name. I'd like开发者_开发百科 it to be read only here.

The reason for asking is I've got the facilities to build out some pocos at the moment, but won't be able to start on the nhibernate stuff until later so I'm trying to plan ahead a little.

Thanks.


1) The interfaces you can use depend on the collection type.

For bag you can use IEnumerable<T>, ICollection<T> or IList<T>.

For set you can use IEnumerable<T>, ICollection<T> or Iesi.Collections.Generic.ISet<T>.

For list you must use IList.

Now, AsEnumerable is a noop. If you want to prevent client code from modifying the collection, use an identity select instead:

public IEnumerable<ChildPoco> ChildPocos
{
    get { return _childPocos.Select(x => x); }
}

And to map it, use access="field.camelcase-underscore". This will cause NHibernate to use the _childPocos field for ChildPocos persistence

2) The setter can have any accesibility you want. And you don't need to use a backing field:

public string Name { get; private set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜