With nhibernate there is a way to reassign readonly property in interface

I have an interface named IEntity that still has one specific class called Entity, this interface has a read-only property. I would prefer to map the interface, but since the interface cannot have a private field, I cannot use the camel area field with the prefix option to map it, so what can I do?

public interface IEntity {public readonly string Name{get;} } public class Entity:IEntity {public readonly string Name{get;}} public class EntityMap:ClassMap<IEntityMap> { //how to map the readonly property } 
+6
source share
2 answers

Try:

 <property name="Name" type="string" access="readonly"/> 

Mapping NHibernate Read Only Properties

and if you use Fluent:

Displaying a read-only property without setting using Fluent NHibernate

I think this might be useful too:

How to map interface in nhibernate?

updated

I think the first step will be the correct code. Then try to publish the mapping file or free configuration. We cannot help you if it is not clear what you want to achieve.

+14
source

You map classes in NHibernate, not with interfaces. As others have pointed out, you mislead the readonly keyword using a read-only property: the readonly keyword means that the field can only be set in the constructor. The read-only property does not have either a private setter.

But I think you can achieve what you want using this:

 public interface IEntity { string Name { get; } } public class Entity : IEntity { public string Name { get; private set; } } public class EntityMap : ClassMap<Entity> { public EntityMap() { Map(x => x.Name); } } 

NHibernate uses reflection, so it can set the Name property, but it is read-only in your application.

+2
source

Source: https://habr.com/ru/post/890346/


All Articles