0

I'm new in NHibernate and I have some doubts. I'll put my code bellow and then I'll explain.

My tables: https://dl.dropbox.com/u/423844/map%20db.png

My classes:

public class IntegratorModel : PGIBaseModel<int>, IIntegratorModel
{
    public virtual string Identifier { get; set; }

    public virtual string Name { get; set; }

    public virtual string Description { get; set; }
}

public class ClientModel : PGIBaseModel<int>, IClientModel
{
    public virtual string Name { get; set; }

    public virtual string TokenPGI { get; set; }

    public virtual IIntegratorModel Integrator { get; set; }
}

My mappings using Fluent:

public class IntegratorMappingOverride : IAutoMappingOverride<IntegratorModel>
{
    public virtual void Override(AutoMapping<IntegratorModel> mapping)
    {
        mapping.Map(model => model.Identifier).Column("Identifier").Length(4);
        mapping.Map(model => model.Name).Column("Name").Length(200);
        mapping.Map(model => model.Description).Column("Description").Length(2000);
    }
}

    public class ClientMappingOverride : IAutoMappingOverride<ClientModel>
{
    public virtual void Override(AutoMapping<ClientModel> mapping)
    {
        mapping.Map(model => model.Name).Column("Name").Length(200);
        mapping.Map(model => model.TokenPGI).Column("TokenPGI").Length(50);

        mapping.References<IntegratorModel>(model => model.Integrator);
    }
}

I'm saving IntegratorModel previously in another moment, so when I'm inserting a new ClientModel I already have the IntegratorID, so it's ok.

My question is when I'm updating ClientModel and I want to change IntegratorID in the database, I'm doing something like this:

    IClientModel model = this.GetModelFromPost();
    model.Integrator = integratorBLL.LoadByID(context.Request.Form["IntegratorID"]));
    clientBLL.Save(model);

integratorBLL.LoadByID just will call ISession.Load from NHibernate. Maybe I'm wrong, but Load doesn't hit the database in this moment.

My question is if what I'm doing is correct. In this case, won't be easier if I had some property IntegratorID directly in ClientModel e just updated it?

Cause, in my architeture, I have to call my BLL of Integrator, call Load and then set the return in ClientModel property.

This code works, but cause I'm new at Nhibernate, I think that probably there's another way to do it better.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193
japoneizo
  • 508
  • 1
  • 5
  • 15

2 Answers2

1

session.Load() is meant for this scenario and has the advantage that the objectproxy you get behaves like the real object so you don't have a broken model when set only the id and you want to use the clientmodels integrator later on. It also does not break change tracking since NHib can not seperate between null > not initialised and null > references nothing

To sum up: this is the right way to handle this with (N)Hibernate.

Firo
  • 30,626
  • 4
  • 55
  • 94
0

The code is ok. You don't need to call Save after changing the entity. NHibernate finds the changes itself and propagates all changes to the database when committing the session.

Stefan Steinegger
  • 63,782
  • 15
  • 129
  • 193