开发者

dynamic-component fluent automapping

Does anyone know how can we automatically map dynamic components using Fluent Automapping in NHibernate?

I know that we can map normal classes as components, but couldn't figure out how to map dictionaries as dynamic-components using开发者_运维百科 fluent automapping.

Thanks


We've used the following approach successfully (with FluentNH 1.2.0.712):

public class SomeClass
{
    public int Id { get; set; }
    public IDictionary Properties { get; set; }
}

public class SomeClassMapping : ClassMap<SomeClass>
{
    public SomeClassMapping()
    {
        Id(x => x.Id);

        // Maps the MyEnum members to separate int columns.
        DynamicComponent(x => x.Properties,
                         c =>
                            {
                                foreach (var name in Enum.GetNames(typeof(MyEnum)))
                                    c.Map<int>(name);
                            });
    }
}

Here we've mapped all members of some Enum to separate columns where all of them are of type int. Right now I'm working on a scenario where we use different types for the dynamic columns which looks like this instead:

// ExtendedProperties contains custom objects with Name and Type members
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Map(prop.Name).CustomType(prop.Type);
}

This also works very well.

What I'm still about to figure out is how to use References instead of Map for referencing other types that have their own mapping...

UPDATE: The case with References is unfortunately more complicated, please refer to this Google Groups thread. In short:

// This won't work
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Reference(dict => dict[part.Name]);
}

// This works but is not very dynamic
foreach (var property in ExtendedProperties)
{
    var prop = property;
    part.Reference<PropertyType>(dict => dict["MyProperty"]);
}

That's all for now.


I got struggle with exactly the same problem. With fluent nHibernate we cannot map this but on my own I somehow was able to solve this. My solution is to build lambda expression on the fly and the assign this into object. For instance, lets say that:

Let my copy part of the site that Oliver refer:

DynamicComponent(
    x => x.Properties,
    part =>
    {
        // Works
        part.Map("Size").CustomType(typeof(string));
        // Works
        var keySize = "Size";
        part.Map(keySize).CustomType(typeof(string));
        // Does not work
        part.Map(d => d[keySize]).CustomType(typeof(string));
 
        // Works
        part.References<Picture>(d => d["Picture"]);
        // Does not work
        var key = "Picture";
        part.References<Picture>(d => d[key]);
    });

And we have this problem that we need to hardcode "Picture" in mapping. But somehow after some research I created following solution:

var someExternalColumnNames = GetFromSomewhereDynamicColumns();

'x' is a DynamicComponent callback in fluent Nhibernate e.g. (DynamicColumns): DynamicComponent(a => a.DynamicColumns, x => (...content of method below...))

foreach(var x in someExternalColumnNames)
{
    if (x.IsReferenceToPerson == true)
    {
        var param = Expression.Parameter(typeof(IDictionary), "paramFirst");
        var key = Expression.Constant(x.Name);
        
        var me = MemberExpression.Call(param, typeof(IDictionary).GetMethod("get_Item"), new[] { key });
        var r = Expression.Lambda<Func<IDictionary, object>>(me, param);
            
        m.References<Person>(r, x.Name);    
    }
    else
    {
        m.Map(x.Name)
    }
}
//

// Some class that we want to reference, just an example of Fluent Nhibernate mapping
    
public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Table("Person");
            Id(x => x.PersonId, "PersonId");
            Map(x => x.Name);
        }
    }

    public class Person
    {
        public virtual Guid PersonId { get; set; }

        public virtual string Name { get; set; }

        public Person()
        { }
    }

Maybe it would be helpful

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜