Nhibernate 和 Fluent Nhibernate 的只读策略

发布于 2024-12-04 11:30:30 字数 511 浏览 1 评论 0原文

我一直在阅读《nhibernate for Beginners 3.0》,并阅读了一些常见错误(其中一些是我犯过的),

我想知道使一条或多条记录只读的策略是什么。现在,我取回所有行并循环遍历它们,通过 session.Readonly() 将它们设置为只读。

我喜欢书中他们用流畅的方式做的

class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        SchemaAction.None();
        ReadOnly();

        // Mappings
    }
 }

事情我不确定的是,如果我需要这些记录不是只读的,会发生什么?对我来说,这意味着我必须制作完全相同的映射减去这两行代码。

所以我想这样做并拥有 ReadonlyEntityMap 和 EntityMap,但我不想将所有设置重复两次。

有人对如何做到这一点有想法吗?或者更好的只读想法?

I been reading nhibernate for beginners 3.0 and been reading about common mistakes(a few of them I been making)

I am wondering what are some strategies for making one or more records readonly. Right now I get all the rows back and loop through them making them readonly through session.Readonly().

I like in the book that they do with fluent

class EntityMap : ClassMap<Entity>
{
    public EntityMap()
    {
        SchemaAction.None();
        ReadOnly();

        // Mappings
    }
 }

What I am not sure is what happens if I need these records to not be Readonly? To me this means I have to make this exact same mapping minus those 2 lines of code.

So I would like to do this and have ReadonlyEntityMap and EntityMap but I rather not have to duplicate all the settings twice.

Anyone have ideas on how do to this? Or better ideas for readonly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰雪梦之恋 2024-12-11 11:30:30

当您修改对象的属性时,在映射上使用 ReadOnly() 将禁用对数据库的任何更新。

如果您想应用某种只读和可写策略,我建议使用 Fluent Convention 和标记界面或类似的。

如果您只想禁用对某个属性的写入,您还可以将 ReadOnly() 应用于该属性。我使用该技术来避免写回 SQL Server 中的计算列。

像这样的事情:

public interface IReadOnly{} //Mark entities with this interface

public class ReadOnlyConvention
     : IClassConvention, IClassConventionAcceptance<IClassInspector>
{
  public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
  {
    criteria.Expect(x => x.EntityType Is IReadOnly);
  }

  public void Apply(IClassInstance instance)
  {
    instance.ReadOnly();
  }
}

更新:
如果您想做时间转换,我建议您为 DateTime 对象创建一个 IUserType ,它可以在不修改基础数据的情况下转换为用户时间。

Using ReadOnly() on your mapping will disable any updates back to the database when you modify the properties of the object.

If you want to apply some sort of ReadOnly and Writeable strategy I would suggest using a Fluent Convention and a marker interface or similar.

You can also apply ReadOnly() to a property if you want to disable writes on just that one property. I use that technique to avoid writing back to Computed Columns in SQL Server.

Something like this:

public interface IReadOnly{} //Mark entities with this interface

public class ReadOnlyConvention
     : IClassConvention, IClassConventionAcceptance<IClassInspector>
{
  public void Accept(IAcceptanceCriteria<IClassInspector> criteria)
  {
    criteria.Expect(x => x.EntityType Is IReadOnly);
  }

  public void Apply(IClassInstance instance)
  {
    instance.ReadOnly();
  }
}

Update:
If you want to do you time conversion thing I would suggest creating an IUserType for your DateTime object which does the conversion to the user time without modifying the underlying data.

摘星┃星的人 2024-12-11 11:30:30

这取决于您想要实现的目标。如果您正在尝试优化内存消耗,您应该考虑改进会话管理策略或使用 其他性能改进技术。您可以例如:

  • 清除加载对象的会话,它将释放一级缓存分配的所有内存。如果需要,稍后可以将对象重新附加(合并)到新会话。这样,您首先不需要将对象设置为只读。

  • 当您不再需要某些对象时,将其逐出。这会将其他对象保留在一级缓存中。

  • 使用 IStatelessSession 根本不使用一级缓存。

同样,答案取决于您的应用程序架构。

It depends on what you are trying to achieve. If you are trying to optimize memory consumption you should consider improving your session management strategy or using other performance improvement techniques. You can for example:

  • Clear the session that loaded your objects and it will release all the memory allocated by first level cache. The objects can later be reattached (Merged) to a new session if needed. This way you don't need objects to be readonly in the first place.

  • Evict some of the objects when you no longer need them. This will keep the other objects in the first level cache.

  • Use IStatelessSession which does not use 1st level cache at all.

Again, the answer depends on your application architecture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文