views:

67

answers:

1

Hi, This is a bit of conceptual questions but I think it’s the right place to ask it.

i am working with NHibernate for some time but I find it difficult to use the collections within the POCO objects 1. I think the proxy concept is nice but when you want o use it in multi tier architecture it become a challenge. 2. I want to add a business logic on any type of request from the DB. for example authorization restrictions etc.. 3. i would like to add some business logic when i add or remove entity from a collection.

and there are some more reasons why i have problems with the collections and proxies.

i thought maybe there is a way to use the POCO object without any collections and let the service layer to handle sub queries and association etc... i understand that the collection are needed for the Nhibernate to work but once i am out of the service layer i want to flat the objects and take out all the collections. this way i can implement my own operations and business logic.

is there any standard way to do it? do i need to attach and detach the objects? how do i create DTO object from the POCO object and copy the fields etc...

anyone have some best practices for that and did the same?

I which I could just create the POCO objects without any relationship and only let nhibernat know about those relationships via the mapping. then using the session i can control the associations between the objects.

Thanks, Noam

A: 

If you want to have a full control under your collection I can suggest you this approach:

  1. You are using a readonly collection:

    public virtual ReadOnlyCollection ChildActions { get { return new ReadOnlyCollection(new List(childActions)); } }

  2. For mapping you can use backing field

    private ISet childActions;

  3. To ensure your collection will never be null (avoid unnessesary checks) you can to next thing in contructor:

    childActions = new HashedSet();

  4. For adding and removing items from collection you can use methods:

    public virtual void AddChildAction(ActionOnClient actionToAdd)
    {
        //some logic here
    
    
    
    childActions.Add(actionToAdd);
    
    } public virtual void AddChildAction(ActionOnClient actionToAdd) { //some logic here
    childActions.Remove(actionToAdd);
    
    }

    This approach will give you full control for your collections. Hope it helps

Sly
This looks nice i will give it a try. and will let you know what i find.
Noam