Fluent NHibernate Ignores Property Inside ClassMap Using FluentMappings

I am using NHibernate 3.1 and Fluent NHibernate as ORM in my project. I need to have a POCO property that is ignored by Fluent NHibernate. At first, my post may look like an exact duplicate of this question , but it is not.

My complications are due to the fact that POCOs are defined in a different assembly than the mapping, and I use smooth mappings for my POCOs. I have an additional requirement not to write the ingore-property code where the factory session configuration takes place (this happens in a centralized place outside the modules), but as part of the module that defines the mappings. Ideally, I believe that the specific implementation will be the right place ClassMap, since he knows exactly how to describe POCO for ORM.

However, I am stuck with this, mainly because this is my first influence on NHibernate and its free API. So far, I have a very good impression of its capabilities and extensibility, and I hope that there is a way to achieve my requirement in such a way that the code associated with the display is encapsulated in its corresponding module.

Here is my configuration, from a central location:

List<Assembly> assemblies = GetModules().Select(x => x.GetType().Assembly).ToList();

ISessionFactory nhibernateSessionFactory = Fluently
    .Configure()
    .Mappings(m => assemblies.ForEach(asm => m.FluentMappings.AddFromAssembly(asm)))
    .Database(
        MsSqlConfiguration.MsSql2005
            .ShowSql()
            .ConnectionString(DatabaseConfig.Instance.ConnectionString))
    .ExposeConfiguration(c => new SchemaUpdate(c).Execute(true, true))
    .BuildSessionFactory();

I use standard class mappings that inherit from ClassMap:

public class User
{
    public virtual int ID { get; set; }
    public virtual String Username { get; set; }
    public virtual String Password { get; set; }
    public virtual DateTime DateCreated { get; set; }
    public virtual DateTime DateModified { get; set; }

    // Must ignore
    public string ComputedProperty  { get { ... } }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Table("User");
        Id(x => x.ID).GeneratedBy.Identity();
        Map(m => m.Username).Not.Nullable().Length(255).UniqueKey("User_Username_Unique_Key");
        Map(m => m.Password).Not.Nullable().Length(255);
        Map(m => m.DateCreated).Not.Nullable();
        Map(m => m.DateModified).Not.Nullable();
    }
}
+3
source share
3 answers

I think you're right that ClassMapis the best place to ignore this property.

Example:

.Override<Shelf>(map =>  
{  
  map.IgnoreProperty(x => x.YourProperty);
});

Documentation: https://github.com/jagregory/fluent-nhibernate/wiki/Auto-mapping#ignoring-properties

, , - ( ):

.Mappings(m =>
              {
                  m.FluentMappings.AddFromAssemblyOf<ProvideClassFromYourOtherAssembly>();
              });
+5

, , , . , - , . , , , . [NoEntity] attitute.

/// <summary>
/// Tells a single Property to not be persisted to table.
/// </summary>
public class NoEntity : Attribute { }


    /// <summary>
/// Extension to ignore attributes
/// </summary>
public static class FluentIgnore
{
    /// <summary>
    /// Ignore a single property.
    /// Property marked with this attributes will no be persisted to table.
    /// </summary>
    /// <param name="p">IPropertyIgnorer</param>
    /// <param name="propertyType">The type to ignore.</param>
    /// <returns>The property to ignore.</returns>
    public static IPropertyIgnorer SkipProperty(this IPropertyIgnorer p, Type propertyType)
    {
        return p.IgnoreProperties(x => x.MemberInfo.GetCustomAttributes(propertyType, false).Length > 0);
    }
}

:

            return Fluently.Configure()
            .Database(DatabaseConfig)
            .Mappings(m => m.AutoMappings.Add(AutoMap.Assembly(typeof(IDependency).Assembly)
            .OverrideAll(p => {
                p.SkipProperty(typeof(NoEntity)); 
            }).Where(IsEntity)))
            .ExposeConfiguration(ValidateSchema)
            .ExposeConfiguration(BuildSchema)
            .BuildConfiguration();
+5

. . , , .

public class Person : IEntity{ 
 public virtual string Name{..}
 public virtual string Lastname{..}

 [NoProperty]
 public virtual string FullName{ // Not created property
  get { return Name +  " " + Lastname;  }
 }
}

public class Group : IEntity{ 
 public virtual string FullName{..} //Created property
}
0

All Articles