开发者

C# fluent nhibernate

Does Fluent NHibernate has a simple method for automapping entities?

Let's say I have some classes like the following one and corresponding classmaps:

public sealed class Hello
{
    public String Name { get; set; }
    public DateTime Timestamp { get; set; }
}

public class Hell开发者_运维问答oMapping : ClassMap<Hello>
{
    public HelloMapping()
    {
        Not.LazyLoad();
        // Some Id here
        Map(x => x.Name).Not.Nullable().Length(64);
        Map(x => x.Timestamp).Not.Nullable();
    }
}

So, does Fluent NHibernate has something like "add every mapped entity like Hello"?

If not, what's the easiest way to let the NHibernate use my mappings provided?


It depends on what you mean by "like"?

Do you mean all entities in the same namespace? Then you can do

public class MyConfiguration : DefaultAutomappingConfiguration {
    public override bool ShouldMap(Type type) {
        return type.Namespace == typeof(Hello).Namespace;
    }
}

Whatever you mean, you can probably set a convention to do what it is you are trying to achieve. See auto mapping in Fluent NHibernate.


Short answer: http://wiki.fluentnhibernate.org/Auto_mapping. You can use objects and basic conventions built into FluentNH to map objects that don't require much custom behavior.

You could also use inheritance to define mappings that have common elements across most or all classes. Say Hello is a base class that defines Id, Name and Timestamp. You can define the mapping for this base class, then either derive from it directly to produce mappings for other objects, or you could define JoinedSubclass mappings for objects that should be stored in a common table structure (usually because they are various flavors of a base class, like CheckingAccount, SavingsAccount and MoneyMarketAccount are all different types of BankAccounts with substantially similar data structures).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜