Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

.net - How to fine tune FluentNHibernate's auto mapper?

Okay, so yesterday I managed to get the latest trunk builds of NHibernate and FluentNHibernate to work with my latest little project. (I'm working on a bug tracking application.) I created a nice data access layer using the Repository pattern.

I decided that my entities are nothing special, and also that with the current maturity of ORMs, I don't want to hand-craft the database. So, I chose to use FluentNHibernate's auto mapping feature with NHibernate's "hbm2ddl.auto" property set to "create".

It really works like a charm. I put the NHibernate configuration in my app domain's config file, set it up, and started playing with it. (For the time being, I created some unit tests only.) It created all tables in the database, and everything I need for it. It even mapped my many-to-many relationships correctly.

However, there are a few small glitches:

  • All of the columns created in the DB allow null. I understand that it can't predict which properties should allow null and which shouldn't, but at least I'd like to tell it that it should allow null only for those types for which null makes sense in .NET (eg. non-nullable value types shouldn't allow null).
  • All of the nvarchar and varbinary columns it created, have a default length of 255. I would prefer to have them on max instead of that.

Is there a way to tell the auto mapper about the two simple rules above?

If the answer is no, will it work correctly if I modify the tables it created? (So, if I set some columns not to allow null, and change the allowed length for some other, will it correctly work with them?)

FINAL EDIT: Big Thanks to everyone who dropped by and helped out. All of my issues with Fluent are solved now.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Auto Mapping Overrides to change how the Auto Mapper work, and you can also define Conventions, that will be used instead by the auto mapper.

Here is an example on how to use both the conventions and the overrides:

var mappings = new AutoPersistenceModel();
mappings.Conventions.Setup(s => s.Add<ColumnNullabilityConvention>());
mappings.UseOverridesFromAssemblyOf<AssemblyName>();

// This convention will set all properties to be not nullable

public class ColumnNullabilityConvention: IPropertyConvention, IPropertyConventionAcceptance
{
   public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
   {
       criteria.Expect(x => x.Nullable, Is.Not.Set);
   }

   public void Apply(IPropertyInstance instance)
   {
       instance.Not.Nullable();
   }
}

// This override will change "string" to use "text" instead of "varchar(255)".
// Also set the property to be not nullable

public class SomeOverrideInTheSameAssembly : IAutoMappingOverride<TypeName>
{
   public void Override(AutoMapping<TypeName> mapping)
   {
       mapping.Map(x => x.Property).CustomType("StringClob").CustomSqlType("text");
       mapping.Map(x => x.Property).Not.Nullable();
   }
}    

Check these links for more examples:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...