本文介绍了NHibernate AliasToBean 转换器关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下语句来获取包含我想要的字段的实体:

I'm trying to use the following statement to get an entity with the fields I'm after:

retVal = session.CreateCriteria(typeof(MyEntity))
            .CreateAlias("MyEntityProperty", "MyEntityProperty")
            .Add(Restrictions.Eq("MyEntityProperty.Year", year))
            .SetProjection(
                Projections.Distinct(
                    Projections.ProjectionList()
                        .Add(Projections.Property("Property1"), "Property1")
                        .Add(Projections.Property("Property2"), "Property2")
                        .Add(Projections.Property("MyEntityProperty.RegisteredUser"), "MyEntityProperty.RegisteredUser")
                        .Add(Projections.Property("MyEntityProperty.CompanyInfo"), "MyEntityProperty.CompanyInfo")
                                                )
            )
            .SetResultTransformer(Transformers.AliasToBean(typeof(MyEntity)))
            .List<MyEntity>()
            .Cast<BaseMyEntity>();

MyEntity 是我想要返回的实体,而 MyEntityProperty 是 MyEntity 的一个属性,它是另一个实体(MyEntityProperty 类型).

MyEntity is the entity I want to return, and MyEntityProperty is a property of MyEntity that is another entity (of type MyEntityProperty).

我得到的错误是在MyEntity"类中找不到属性MyEntityProperty.RegisteredUser"的setter

AliasToBean 转换器是否无法处理子实体?或者我还需要做些什么才能让它发挥作用?

Is the AliasToBean transformer not able to handle sub entities? Or is there something more I need to do to make it work?

推荐答案

这是我的大师作品...我正在使用它来转换任何级别的投影深度.拿它并像这样使用它:

There is my master piece... which I'm using to transform any level of projections depth. Take it and use it like this:

.SetResultTransformer(new DeepTransformer<MyEntity>())

它可以用于任何 ValueType 属性、多对一 引用以及 动态对象...

It could be used for any ValueType properties, many-to-one references and also for dynamic objects...

public class DeepTransformer<TEntity> : IResultTransformer
    where TEntity : class
{
    // rows iterator
    public object TransformTuple(object[] tuple, string[] aliases)
    {
        var list = new List<string>(aliases);

        var propertyAliases = new List<string>(list);
        var complexAliases = new List<string>();

        for(var i = 0; i < list.Count; i++)
        {
            var aliase = list[i];
            // Aliase with the '.' represents complex IPersistentEntity chain
            if (aliase.Contains('.'))
            {
                complexAliases.Add(aliase);
                propertyAliases[i] = null;
            }
        }

        // be smart use what is already available
        // the standard properties string, valueTypes
        var result = Transformers
             .AliasToBean<TEntity>()
             .TransformTuple(tuple, propertyAliases.ToArray());

        TransformPersistentChain(tuple, complexAliases, result, list);

        return result;
    }

    /// <summary>Iterates the Path Client.Address.City.Code </summary>
    protected virtual void TransformPersistentChain(object[] tuple
          , List<string> complexAliases, object result, List<string> list)
    {
        var entity = result as TEntity;

        foreach (var aliase in complexAliases)
        {
            // the value in a tuple by index of current Aliase
            var index = list.IndexOf(aliase);
            var value = tuple[index];
            if (value.IsNull())
            {
                continue;
            }

            // split the Path into separated parts
            var parts = aliase.Split('.');
            var name = parts[0];

            var propertyInfo = entity.GetType()
                  .GetProperty(name, BindingFlags.NonPublic
                                   | BindingFlags.Instance
                                   | BindingFlags.Public);

            object currentObject = entity;

            var current = 1;
            while (current < parts.Length)
            {
                name = parts[current];
                object instance = propertyInfo.GetValue(currentObject);
                if (instance.IsNull())
                {
                    instance = Activator.CreateInstance(propertyInfo.PropertyType);
                    propertyInfo.SetValue(currentObject, instance);
                }

                propertyInfo = propertyInfo.PropertyType.GetProperty(name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
                currentObject = instance;
                current++;
            }

            // even dynamic objects could be injected this way
            var dictionary = currentObject as IDictionary;
            if (dictionary.Is())
            {
                dictionary[name] = value;
            }
            else
            {
                propertyInfo.SetValue(currentObject, value);
            }
        }
    }

    // convert to DISTINCT list with populated Fields
    public System.Collections.IList TransformList(System.Collections.IList collection)
    {
        var results = Transformers.AliasToBean<TEntity>().TransformList(collection);
        return results;
    }
}

这篇关于NHibernate AliasToBean 转换器关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:56