PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FluentNHibernate/Mapping/CompositeElementPart.cs

https://github.com/dotnetchris/fluent-nhibernate
C# | 106 lines | 76 code | 21 blank | 9 comment | 4 complexity | 50b7cec449820c42eae03d362ba20364 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. using System.Reflection;
  5. using FluentNHibernate.Mapping.Providers;
  6. using FluentNHibernate.MappingModel;
  7. using FluentNHibernate.MappingModel.Collections;
  8. using FluentNHibernate.Utils;
  9. namespace FluentNHibernate.Mapping
  10. {
  11. /// <summary>
  12. /// Component-element for component HasMany's.
  13. /// </summary>
  14. /// <typeparam name="T">Component type</typeparam>
  15. public class CompositeElementPart<T> : ICompositeElementMappingProvider
  16. {
  17. private readonly Type entity;
  18. private readonly IList<IPropertyMappingProvider> properties = new List<IPropertyMappingProvider>();
  19. private readonly IList<IManyToOneMappingProvider> references = new List<IManyToOneMappingProvider>();
  20. private readonly AttributeStore<CompositeElementMapping> attributes = new AttributeStore<CompositeElementMapping>();
  21. public CompositeElementPart(Type entity)
  22. {
  23. this.entity = entity;
  24. }
  25. public PropertyPart Map(Expression<Func<T, object>> expression)
  26. {
  27. return Map(expression, null);
  28. }
  29. public PropertyPart Map(Expression<Func<T, object>> expression, string columnName)
  30. {
  31. return Map(ReflectionHelper.GetProperty(expression).ToMember(), columnName);
  32. }
  33. protected virtual PropertyPart Map(Member property, string columnName)
  34. {
  35. var propertyMap = new PropertyPart(property, typeof(T));
  36. if (!string.IsNullOrEmpty(columnName))
  37. propertyMap.Column(columnName);
  38. properties.Add(propertyMap);
  39. return propertyMap;
  40. }
  41. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> expression)
  42. {
  43. return References(expression, null);
  44. }
  45. public ManyToOnePart<TOther> References<TOther>(Expression<Func<T, TOther>> expression, string columnName)
  46. {
  47. return References<TOther>(ReflectionHelper.GetProperty(expression).ToMember(), columnName);
  48. }
  49. protected virtual ManyToOnePart<TOther> References<TOther>(Member property, string columnName)
  50. {
  51. var part = new ManyToOnePart<TOther>(typeof(T), property);
  52. if (columnName != null)
  53. part.Column(columnName);
  54. references.Add(part);
  55. return part;
  56. }
  57. /// <summary>
  58. /// Maps a property of the component class as a reference back to the containing entity
  59. /// </summary>
  60. /// <param name="exp">Parent reference property</param>
  61. /// <returns>Component being mapped</returns>
  62. public CompositeElementPart<T> ParentReference(Expression<Func<T, object>> exp)
  63. {
  64. var property = ReflectionHelper.GetProperty(exp);
  65. attributes.Set(x => x.Parent, new ParentMapping
  66. {
  67. Name = property.Name,
  68. ContainingEntityType = entity
  69. });
  70. return this;
  71. }
  72. CompositeElementMapping ICompositeElementMappingProvider.GetCompositeElementMapping()
  73. {
  74. var mapping = new CompositeElementMapping(attributes.CloneInner());
  75. mapping.ContainingEntityType = entity;
  76. if (!mapping.IsSpecified("Class"))
  77. mapping.Class = new TypeReference(typeof(T));
  78. foreach (var property in properties)
  79. mapping.AddProperty(property.GetPropertyMapping());
  80. foreach (var reference in references)
  81. mapping.AddReference(reference.GetManyToOneMapping());
  82. return mapping;
  83. }
  84. }
  85. }