I need to make a mapping One by One, and I have some doubts. I have this classes:
public class DocumentType {
public virtual int Id { get; set; }
If the relationship is really one-to-one... then use HasOne :-)
See http://nhibernate.info/doc/nhibernate-reference/mapping.html#mapping-declaration-onetoone. It has all you need to know.
Even though you have a one-to-one in your domain, your relational model is probably one-to-many. I doubt you share same PK on both tables, more likely you have FK on DocumentConfiguration for DocumentType. In that case you would map it as such, because what you are mapping is your relational model. So on DocumentType it would be HasOne.Inverse.AllDeleteOrphan ... and on DocumentConfiguration it would be "References".
Now you domain should expose it as you are describing it.
public class DocumentConfiguration
{
public DocumentConfiguration()
{
_internalDocumentConfigurations = new List<DocumentConfiguration>(1);
}
private IList<DocumentConfiguration> _internalDocumentConfigurations
public virtual DocumentType Type
{
get
{
return _internalDocumentConfigurations.FirstOrDefault();
}
/**/WARNING - no setter here**
}
public virtual SetDocumentConfiguration(DocumentConfiguration config)
{
//add your asserts here
Add(config);
}
private virtual Add (DocumentConfiguration config)
{
//add your asserts here
_internalDocumentConfigurations.Add(config)
config.DocumentType = this;
}
public virtual Remove (DocumentConfiguration config)
{
_internalDocumentConfigurations.Remove(config)
config.DocumentType = null;
}
}
public class DocumentConfiguration {
public virtual int Id { get; set; }
/* some other properties for configuration */
public virtual DocumentType Type { get; protected internal set; }