NHibernate 具有流畅的映射和 Syscache2 二级缓存
我有一个非常简单的国家/地区实体,我想缓存它。 这工作得很好,但我希望返回缓存实例的克隆版本或将其设置为只读,以防止开发人员更改其状态。
我将如何实现这一目标? 我认为 Fluent Readonly() 方法会强制执行此操作,但事实并非如此。
示例流畅映射:
Id(x => x.Id);
Map(x => x.Name).WithLengthOf(50).Not.Nullable().Unique();
Map(x => x.IsoCode).WithLengthOf(10).Not.Nullable().Unique();
HasMany(x => x.States).Cascade.None().LazyLoad();
Cache.AsReadOnly();
我的单元测试清楚地表明实体已被缓存,并且在后续获取中返回相同的实体,但我希望该对象在从持久存储加载后是不可变的。
谢谢!
I have a very simple Country entity which I want to cache. This works perfectly, but I want a clone version of the cached instance to be returned or be made read-only to prevent developers from changing the state of it.
How would I achieve this? I tought that the Fluent Readonly() method would enforce this, but it's not the case.
Sample Fluent Mapping:
Id(x => x.Id);
Map(x => x.Name).WithLengthOf(50).Not.Nullable().Unique();
Map(x => x.IsoCode).WithLengthOf(10).Not.Nullable().Unique();
HasMany(x => x.States).Cascade.None().LazyLoad();
Cache.AsReadOnly();
My unit tests clearly indicates that the entities are cached and that the same entity is returned on subsequent gets, but I want the object to be immutable once it's loaded from the persistent store.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望类的实例是不可变的(从某种意义上说,不可能修改对象实例),那么您需要根据该类来编写您的类 - 这与 NHib 或 Fluent NHib 无关。 例如,将 NHib 映射到私有字段或私有属性 setter,并且仅公开公开属性 getter。
使用 NHib 可以在类映射上指定“mutable=false”(抱歉,不知道如何使用 FNH 执行此操作)。 这不会使对象在运行时不可变,但它确实通知 NHib 不要检查这些实体的插入、更新和删除。
If you want instances of your class to be immutable (in the sense that it's impossible to modify object instances), then you'll need to write your class according - it's nothing to do with NHib or Fluent NHib. As an example, map NHib onto private fields or private property setters and only expose property getters publicly.
With NHib it is possible to specify "mutable=false" on your class mapping (not sure how to do this with FNH, sorry). This doesn't make the object immutable at runtime, but it does inform NHib not to check for inserts, updates and deletes against these entities.