private static string GetDescriptionValue(GenericsModelMetadata metadata, object model)
        {
            if (model != null)
                return model.ToString();

            return null;
        }
Esempio n. 2
0
        private static Type GetIdMemberType(GenericsModelMetadata metadata)
        {
            if (metadata.IdMember.MemberType == MemberTypes.Property)
                return ((PropertyInfo)metadata.IdMember).PropertyType;

            if (metadata.IdMember.MemberType == MemberTypes.Field)
                return ((FieldInfo)metadata.IdMember).FieldType;

            throw new InvalidOperationException(
                String.Format("IdMember {0} configured for model {1} should be field or property.",
                              metadata.IdMember.Name, metadata.DisplayName));
        }
        private static object GetIdValue(GenericsModelMetadata metadata, object model)
        {
            if (model == null || metadata.IdMember == null)
                return null;

            if (metadata.IdMember.MemberType == MemberTypes.Property)
                return ((PropertyInfo)metadata.IdMember).GetValue(model, new object[] {});

            if (metadata.IdMember.MemberType == MemberTypes.Field)
                return ((FieldInfo)metadata.IdMember).GetValue(model);

            return null;
        }
        private SelectList BuildSelectList(GenericsModelMetadata metadata)
        {
            var provider = ConfigurationHolder.DomainAccessorAccessor.Invoke(metadata.ReferenceType);
            if (provider == null)
                throw new InvalidOperationException(String.Format("The domain provider for the type {0} is invalid.", metadata.ReferenceType));

            var referenceMetadata = GetMetadataForTypeImpl(() => null, metadata.ReferenceType);
            if (referenceMetadata.IdMember == null)
            {
                throw new InvalidOperationException(String.Format(
                        "{0} is configured as a reference type but it hasn't an Id property. " +
                        "Consider configuring the Id property or excluding the type via the " +
                        "ExcludeReferenceTypes property in the ReferencePatternApplier.",
                        metadata.ReferenceType));
            }

            var list = provider.GetList();

            var selectList = list.Select(
                referenceValue => new SelectListItem
                                  	{
                                  		Value = GetIdValue(referenceMetadata, referenceValue).ToString(),
                                  		Text = GetDescriptionValue(referenceMetadata, referenceValue)
                                  	}).ToList();

            // TODO: probar si podemos usar esta sobrecarga (viendo el Equals)
            // return new SelectList(selectList, GetIdValue(metadata, metadata.Model));
            return new SelectList(selectList, "Value", "Text", GetIdValue(referenceMetadata, metadata.Model));
        }