nhibernate-core icon indicating copy to clipboard operation
nhibernate-core copied to clipboard

Provide a way for proxy to tell that initialization is not required for certain properties

Open bahusoid opened this issue 6 years ago • 4 comments

It would be really helpful for composite keys when key properties are also exposed in Entity for convenience. Something like:

public class CompositeId
{
	public int Key1 {get;set;}
	public int Key2 {get;set;}
}

public class Entity
{
	public virtual CompositeId Id { get; set; } = new CompositeId();

	// This property doesn't require entity initialization.
	// But there is no way to tell NHibernate that
	public virtual int Key1
	{
		get => Id.Key1;
		set => Id.Key1 = value;
	}

	public virtual int Key2
	{
		get => Id.Key2;
		set => Id.Key2 = value;
	}
}

Or when Id property is accessed via interface with different name (see #2184)

bahusoid avatar Oct 16 '19 13:10 bahusoid

Are you thinking about adding a dedicated attribute in NHibernate which would be used as a marker for such properties?

fredericDelaporte avatar Oct 17 '19 19:10 fredericDelaporte

Yes that's one possibility. Alternatively to keep NHibernate out of entity I'm thinking of some static class with user provided generator settings for type members (for now with single SkipInitialization flag). Configuration could look something like:

ProxySettings
	.Add<Entity>(settings => settings.SkipInitialization = true, x => x.Member1, x => x.Member2,... )
	.Add<Entity2>(...);

In case mapping by code it seems such configuration can be placed along with entity mapping.

Also I realized implementation shouldn't be restricted to properties - methods also should be supported.

bahusoid avatar Oct 18 '19 05:10 bahusoid

Could it be inferred from generated attribute on the property mapping?

hazzik avatar Feb 03 '23 00:02 hazzik

Are you about this mapping? https://nhibernate.info/doc/nh/en/index.html#mapping-generated

No I don't think it's related to this feature. Proxy initialization is required on property access of generated properties (if it's not ID property)

And this feature is useful for properties/methods that are safe to call for un-initialized proxy (so they only access identifier or some static or not persistent data).

bahusoid avatar Feb 03 '23 11:02 bahusoid