How to add mappings by namespace in Fluent NHibernate?

It's odd that FluentNbernate supports this type of filtering for automapping, but not for ClassMap s. It shouldn't be too hard to add this feature yourself, though, via the magic of extension methods. Try this.

It's odd that FluentNbernate supports this type of filtering for automapping, but not for ClassMaps. It shouldn't be too hard to add this feature yourself, though, via the magic of extension methods. Try this: public static FluentMappingsContainer AddFromAssemblyOf( this FluentMappingsContainer mappings, Predicate where) { if (where == null) return mappings.

AddFromAssemblyOf(); var mappingClasses = typeof(T).Assembly. GetExportedTypes() . Where(x => typeof(IMappingProvider).

IsAssignableFrom(x) && where(x)); foreach (var type in mappingClasses) { mappings. Add(type); } return mappings; } ... and use it like this: m.FluentMappings. AddFromAssemblyOf(t => t.Namespace.

StartsWith("One.Of.The.Two. Namespaces")).

1 I discovered something similar, but this approach is more general. – Gabe Moothart Jun 1 at 20:20.

I wound up writing an extension method that does this for me. Basically it uses reflection to iterate over all the types I'm interested in, and add them one-by-one. It is based on the implementation of AddFromAssemblyOf.

Usage: . Mappings(m => m.FluentMappings. AddFromNamespaceOf()) Implementation: public static class FluentNbernateExtensions { public static FluentMappingsContainer AddFromNamespaceOf( this FluentMappingsContainer fmc) { string ns = typeof(T).

Namespace; IEnumerable types = typeof(T).Assembly. GetExportedTypes() . Where(t => t.

Namespace == ns) . Where(x => IsMappingOf(x) || IsMappingOf(x) || IsMappingOf(x) || IsMappingOf(x)); foreach(Type t in types) { fmc. Add(t); } return fmc; } /// /// Private helper method cribbed from FNH source (PersistenModel.Cs:151) /// private static bool IsMappingOf(Type type) { return!type.

IsGenericType && typeof(T). IsAssignableFrom(type); } } Caveats: The name is a little misleading, since it only searches one assembly.It should perhaps be called AddFromAssemblyAndNamespaceOf, but that's a little verbose. It is not entirely future-proof.

If future versions of FNH add or remove some of the mappable interfaces, it wouldn't include them. But it works for my purposes.

1 Thanks, very helpful! – Jonathan Moffatt Sep 28 at 5:22.

I'm not using automappings, I'm specifying my mappings explicitly with ClassMap mappings. – Gabe Moothart Jun 1 at 20:09.

There is no way to do this. I recommend separating the namespaces out into separate projects. Remember: Separate namespaces, same project when logical separation makes sense.

Separate namespaces, separate projects when physical separation makes sense. In this case, since you can't separate by namespace in nhibernate mappings, physical separation makes sense. You can, however, get around this with with fluent automaps that use a .

Where or a ShouldMap configuration. Look up fluent automaps and see if that can get you where you want to be.

It's odd that FluentNHibernate supports this type of filtering for automapping, but not for ClassMap s. It shouldn't be too hard to add this feature yourself, though, via the magic of extension methods. Try this.

I wound up writing an extension method that does this for me. Basically it uses reflection to iterate over all the types I'm interested in, and add them one-by-one. It is based on the implementation of AddFromAssemblyOf.

Usage.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions