与Nhibernate和流利Nhibernate只读策略 [英] Readonly Strategies with Nhibernate and Fluent Nhibernate

查看:122
本文介绍了与Nhibernate和流利Nhibernate只读策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读nhibernate的初学者3.0,并一直在阅读常见的错误(其中一些我一直在做)

我想知道制定一个什么样的策略或只读更多的记录。现在我得到所有的行,并通过它们循环,使他们通过session.Readonly()只读。



我喜欢在书中,他们用流利的

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


code $ $ $ $ $ $ $ $ $ $ $ $我不确定是什么情况,如果我需要这些记录不是只读?对我来说,这意味着我必须做这个完全相同的映射减去这两行代码。

所以我想这样做,并有ReadonlyEntityMap和EntityMap,但我宁可不必须重复所有的设置两次。

任何人都有想法如何做到这一点?或更好的想法readonly?

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

如果您想应用某种ReadOnly和Writeable策略,我会建议使用 ReadOnly() $ c>如果你想禁止只写一个属性的属性。我使用这种技术来避免写回SQL Server中的计算列。

类似这样:

<$ p $使用此接口标记实体
$ b public class ReadOnlyConvention
:IClassConvention,IClassConventionAcceptance< IClassInspector>
{
public void Accept(IAcceptanceCriteria< IClassInspector> criteria)
{
criteria.Expect(x => x.EntityType is IReadOnly);

$ b $ public void Application(IClassInstance instance)
{
instance.ReadOnly();






如果你想做你的时间转换的事情,我会建议创建一个IUserType为您的日期时间对象转换为用户时间,而不会修改底层的数据。




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?

解决方案

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.

这篇关于与Nhibernate和流利Nhibernate只读策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆