如何创建一个流畅的 NHibernate 约定来忽略没有设置器的属性

发布于 2024-09-15 05:23:42 字数 406 浏览 4 评论 0原文

我正在寻找 FluentNH (Fluent NHibernate)约定或配置,忽略所有没有设置器的属性:

它仍然会映射这些:

public class foo{
  public virtual int bar {get; private set;}
}

并省略这些:

public class foo{
  public virtual int fizz{get;private set;}
  public virtual int bar{get {return fizz;}} //<-------
}

I'm looking for a FluentNH (Fluent NHibernate) convention or configuration that ignores all properties that have no setter:

It would still map these:

public class foo{
  public virtual int bar {get; private set;}
}

And omit these:

public class foo{
  public virtual int fizz{get;private set;}
  public virtual int bar{get {return fizz;}} //<-------
}

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

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

发布评论

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

评论(4

荭秂 2024-09-22 05:23:52

另一种方法是使用属性。

public class MyEntity
{
    [NotMapped]
    public bool A => true;
}

public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.MemberInfo.GetCustomAttributes(typeof(NotMappedAttribute), true).Length > 0)
        {
            return false;
        }
        return base.ShouldMap(member);
    }
}

Another way is to use an attribute.

public class MyEntity
{
    [NotMapped]
    public bool A => true;
}

public class AutomappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.MemberInfo.GetCustomAttributes(typeof(NotMappedAttribute), true).Length > 0)
        {
            return false;
        }
        return base.ShouldMap(member);
    }
}
浅浅淡淡 2024-09-22 05:23:50

我知道这是一个老问题,但下面的代码对于私有设置器来说效果很好。

public override bool ShouldMap(Member member)
{
    var prop = member.DeclaringType.GetProperty(member.Name);
    bool isPropertyToMap = 
        prop != null &&
        prop.GetSetMethod(true) != null &&
        member.IsProperty;

    return
        base.ShouldMap(member) && isPropertyToMap;
}

I know this is old question but code below do well with private setters.

public override bool ShouldMap(Member member)
{
    var prop = member.DeclaringType.GetProperty(member.Name);
    bool isPropertyToMap = 
        prop != null &&
        prop.GetSetMethod(true) != null &&
        member.IsProperty;

    return
        base.ShouldMap(member) && isPropertyToMap;
}
dawn曙光 2024-09-22 05:23:48

使用这个:

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.IsProperty && !member.CanWrite)
        {
            return false;
        }

        return base.ShouldMap(member);
    }
}

应该处理没有 setter 和私有 setter 的情况。

Use this:

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        if (member.IsProperty && !member.CanWrite)
        {
            return false;
        }

        return base.ShouldMap(member);
    }
}

That should handle the case of no setter and private setter.

謸气贵蔟 2024-09-22 05:23:47

您应该使用自定义映射配置

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        return member.CanWrite;
    }
}

用法:

var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();

var.fluentConfiguration = Fluently.Configure(nhConfiguration );
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
    ));

var sessionFactory = this.fluentConfiguration.BuildSessionFactory();

但是,私有设置器不会被映射。你应该让它们受到保护

You should use a custom mapping configuration

public class DefaultMappingConfiguration : DefaultAutomappingConfiguration
{
    public override bool ShouldMap(Member member)
    {
        return member.CanWrite;
    }
}

Usage :

var nhConfiguration = new Configuration().Configure();
var mappingConfiguration = new DefaultMappingConfiguration();

var.fluentConfiguration = Fluently.Configure(nhConfiguration );
    .Mappings(m => m.AutoMappings.Add(
        AutoMap.AssemblyOf<MappedType>(mappingConfiguration)
    ));

var sessionFactory = this.fluentConfiguration.BuildSessionFactory();

However, private setters won't get mapped. You should get them as protected

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