PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/DomainDriver_v2_0_0_0/code/AutomotivePricingModel/Vehicles/Vehicle.Storage.cs

#
C# | 191 lines | 159 code | 30 blank | 2 comment | 12 complexity | 82cf29603f59ed4b5efe756a26f0b1e7 MD5 | raw file
Possible License(s): LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Data;
  5. using System.Data.Entity;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Text;
  9. using DomainDriver.DomainModeling.DomainObjects;
  10. using DomainDriver.DomainModeling.Queries;
  11. using DomainDriver.DomainModeling.StorageManagers.DataSetStorage;
  12. using DomainDriver.DomainModeling.StorageManagers.EntityFrameworkStorage;
  13. using AutomotivePricingModel.ValueObjects.YearlyFinancialAssessments;
  14. using ObjectRow = AutomotivePricingModel.AutomotivePricingDataSet.VehiclesRow;
  15. namespace AutomotivePricingModel.Vehicles
  16. {
  17. public partial class Vehicle : IEfEntity<Guid>, IEfAggregate<Vehicle>, IDataSetSerializable<AutomotivePricingDataSet, DataRow>
  18. {
  19. #region ADO .NET Entities Framework Storage
  20. [Key]
  21. [DatabaseGenerated(DatabaseGeneratedOption.None)]
  22. public Guid EF_Id
  23. {
  24. get { return m_Key; }
  25. set { m_Key = value; }
  26. }
  27. [Required]
  28. public OptionsConfiguration EF_OptionsConfiguration
  29. {
  30. get { return m_OptionsConfiguration; }
  31. set { m_OptionsConfiguration = value; }
  32. }
  33. [Required]
  34. //[Association("Assc_Vehicle_YearlyFinancialAssessment", "EF_PriceHistoryId", "EF_YearlyFinancialAssessmentId")]
  35. //[ForeignKey("EF_PriceHistory")]
  36. public Nullable<Guid> EF_PriceHistoryId
  37. {
  38. get { return m_PriceHistoryId; }
  39. set { m_PriceHistoryId = value; }
  40. }
  41. [NotMapped]
  42. public YearlyFinancialAssessment EF_PriceHistory
  43. {
  44. get
  45. {
  46. if (!m_PriceHistoryId.HasValue)
  47. { return null; }
  48. if (!m_PriceHistory.Key.Equals(m_PriceHistoryId.Value))
  49. { return null; }
  50. return m_PriceHistory;
  51. }
  52. set
  53. {
  54. if (!m_PriceHistoryId.HasValue)
  55. { throw new ApplicationException(); }
  56. if (m_PriceHistoryId.Value != value.Key)
  57. { throw new ApplicationException(); }
  58. m_PriceHistory = value;
  59. }
  60. }
  61. ICollection<QuerySpecification> IEfAggregate<Vehicle>.OverriddenQueries
  62. {
  63. get { return new List<QuerySpecification>(); }
  64. }
  65. bool IEfAggregate<Vehicle>.SearchNestedAggregateValues(DbContext context, IParameterizedQuery<Vehicle> query)
  66. {
  67. throw new NotImplementedException();
  68. }
  69. protected static readonly string YearlyFinancialAssessmentName = (typeof(YearlyFinancialAssessment)).Name;
  70. void IEfAggregate<Vehicle>.ReadNestedAggregateValues(DbContext context, object rootQueryPredicate, Dictionary<string, object> batchReadState)
  71. {
  72. LoadNestedBatchReadState(context, rootQueryPredicate, batchReadState);
  73. IQueryable<YearlyFinancialAssessment> yearlyFinancialAssessmentResults = (batchReadState[YearlyFinancialAssessmentName] as IQueryable<YearlyFinancialAssessment>).Where<YearlyFinancialAssessment>((YearlyFinancialAssessment yfa) => (yfa.EF_Id == this.m_PriceHistoryId));
  74. List<YearlyFinancialAssessment> yearlyFinancialAssessmentList = new List<YearlyFinancialAssessment>(yearlyFinancialAssessmentResults);
  75. this.EF_PriceHistory = yearlyFinancialAssessmentList.FirstOrDefault();
  76. if (this.EF_PriceHistory != null)
  77. { this.EF_PriceHistory.ReadNestedAggregateValues(context, rootQueryPredicate, batchReadState); }
  78. }
  79. private void LoadNestedBatchReadState(DbContext context, object rootQueryPredicate, Dictionary<string, object> batchReadState)
  80. {
  81. if (batchReadState.ContainsKey(YearlyFinancialAssessmentName))
  82. { return; }
  83. string vehicleName = (typeof(Vehicle)).Name;
  84. IEnumerable<Vehicle> vehicles = batchReadState[vehicleName] as IEnumerable<Vehicle>;
  85. IEnumerable<Guid> vehicleGuids = vehicles.Select((Vehicle veh) => veh.EF_Id);
  86. List<Guid> yearlyFinancialAssessmentGuids = new List<Guid>();
  87. List<Vehicle> vehicleList = new List<Vehicle>(vehicles);
  88. foreach (Vehicle vehicle in vehicleList)
  89. {
  90. if (!vehicle.EF_PriceHistoryId.HasValue)
  91. { continue; }
  92. yearlyFinancialAssessmentGuids.Add(vehicle.EF_PriceHistoryId.Value);
  93. }
  94. Expression<Func<YearlyFinancialAssessment, bool>> yearlyFinancialAssessmentPredicate = null;
  95. yearlyFinancialAssessmentPredicate = ((YearlyFinancialAssessment yfa) => yearlyFinancialAssessmentGuids.Contains(yfa.EF_Id));
  96. IQueryable<YearlyFinancialAssessment> yearlyFinancialAssessmentResults = context.Set<YearlyFinancialAssessment>().Where(yearlyFinancialAssessmentPredicate);
  97. batchReadState.Add(YearlyFinancialAssessmentName, yearlyFinancialAssessmentResults);
  98. }
  99. void IEfAggregate<Vehicle>.AddNestedAggregateValues(DbContext context)
  100. {
  101. Expression<Func<ICollection<YearlyFinancialAssessment>>> yearlyFinancialAssessmentsGetterExpression = () => (EfAggregateUtilities.ConvertSingleNestedObjectToList(this.EF_PriceHistory));
  102. EfAggregateUtilities.AddNestedValues<YearlyFinancialAssessment>(context, yearlyFinancialAssessmentsGetterExpression);
  103. }
  104. void IEfAggregate<Vehicle>.UpdateNestedAggregateValues(DbContext context, Vehicle originalAggregateRoot)
  105. {
  106. Expression<Func<YearlyFinancialAssessment, Guid>> yearlyFinancialAssessmentKeyGetterExpression = ((YearlyFinancialAssessment nestedObj) => (nestedObj.EF_Id));
  107. Expression<Func<YearlyFinancialAssessment, YearlyFinancialAssessment, bool>> yearlyFinancialAssessmentMatchExpression = ((YearlyFinancialAssessment obj1, YearlyFinancialAssessment obj2) => ((obj1.EF_Id == obj2.EF_Id)));
  108. Expression<Func<Vehicle, IDictionary<Guid, YearlyFinancialAssessment>>> yearlyFinancialAssessmentDictionaryGetterExpression = ((Vehicle agg) => (EfAggregateUtilities.ConvertSingleNestedObjectToDictionary(agg.EF_PriceHistory, yearlyFinancialAssessmentKeyGetterExpression)));
  109. EfAggregateUtilities.UpdateNestedValues<Vehicle, Guid, YearlyFinancialAssessment>(context, this, originalAggregateRoot, yearlyFinancialAssessmentKeyGetterExpression, yearlyFinancialAssessmentMatchExpression, yearlyFinancialAssessmentDictionaryGetterExpression);
  110. }
  111. void IEfAggregate<Vehicle>.DeleteNestedAggregateValues(DbContext context)
  112. {
  113. Expression<Func<YearlyFinancialAssessment, bool>> yearlyFinancialAssessmentDeleteQueryExpression = ((YearlyFinancialAssessment yfa) => (yfa.EF_Id == this.EF_PriceHistoryId));
  114. EfAggregateUtilities.DeleteNestedValues(context, yearlyFinancialAssessmentDeleteQueryExpression);
  115. }
  116. #endregion
  117. #region AutomotivePricingDataSet Serialization
  118. string IDataSetSerializable<AutomotivePricingDataSet, DataRow>.ObjectTableName
  119. {
  120. get { return AutomotivePricingDataSet.SchemaHelper.Vehicles.TableName; }
  121. }
  122. string IDataSetSerializable<AutomotivePricingDataSet, DataRow>.KeyColumnName
  123. {
  124. get { return AutomotivePricingDataSet.SchemaHelper.Vehicles.EF_VehicleIdColumn.ColumnName; }
  125. }
  126. void IDataSetSerializable<AutomotivePricingDataSet, DataRow>.Serialize(AutomotivePricingDataSet datasource, DataRow dataRow)
  127. {
  128. ObjectRow objectRecord = dataRow as ObjectRow;
  129. objectRecord.EF_VehicleId = m_Key;
  130. objectRecord.SerializeNullableField<int>(m_ModelYear, AutomotivePricingDataSet.SchemaHelper.Vehicles.ModelYearColumn);
  131. objectRecord.SerializeNullableField<Guid>(m_ParentProductionModel, AutomotivePricingDataSet.SchemaHelper.Vehicles.ParentProductionModelColumn);
  132. objectRecord.SerializeNullableField<Guid>(m_PriceHistoryId, AutomotivePricingDataSet.SchemaHelper.Vehicles.EF_PriceHistoryIdColumn);
  133. objectRecord.EF_OptionsConfiguration_EF_BodyStyle = m_OptionsConfiguration.EF_BodyStyle;
  134. objectRecord.EF_OptionsConfiguration_EF_DriveTrainType = m_OptionsConfiguration.EF_DriveTrainType;
  135. objectRecord.EF_OptionsConfiguration_EF_TransmissionType = m_OptionsConfiguration.EF_TransmissionType;
  136. objectRecord.EF_OptionsConfiguration_EngineSizeInLiters = m_OptionsConfiguration.EngineSizeInLiters;
  137. objectRecord.EF_OptionsConfiguration_HasNavigationSystem = m_OptionsConfiguration.HasNavigationSystem;
  138. objectRecord.EF_OptionsConfiguration_NumberOfCylinders = m_OptionsConfiguration.NumberOfCylinders;
  139. objectRecord.EF_OptionsConfiguration_NumberOfSpeeds = m_OptionsConfiguration.NumberOfSpeeds;
  140. DataRow nestedObjectRow = datasource.Tables[m_PriceHistory.ObjectTableName].NewRow();
  141. m_PriceHistory.Serialize(datasource, nestedObjectRow);
  142. }
  143. void IDataSetSerializable<AutomotivePricingDataSet, DataRow>.Deserialize(AutomotivePricingDataSet datasource, DataRow dataRow)
  144. {
  145. ObjectRow objectRecord = dataRow as ObjectRow;
  146. m_Key = objectRecord.EF_VehicleId;
  147. objectRecord.DeserializeNullableField<int>(out m_ModelYear, AutomotivePricingDataSet.SchemaHelper.Vehicles.ModelYearColumn);
  148. objectRecord.DeserializeNullableField<Guid>(out m_ParentProductionModel, AutomotivePricingDataSet.SchemaHelper.Vehicles.ParentProductionModelColumn);
  149. objectRecord.DeserializeNullableField<Guid>(out m_PriceHistoryId, AutomotivePricingDataSet.SchemaHelper.Vehicles.EF_PriceHistoryIdColumn);
  150. m_OptionsConfiguration.EF_BodyStyle = objectRecord.EF_OptionsConfiguration_EF_BodyStyle;
  151. m_OptionsConfiguration.EF_DriveTrainType = objectRecord.EF_OptionsConfiguration_EF_DriveTrainType;
  152. m_OptionsConfiguration.EF_TransmissionType = objectRecord.EF_OptionsConfiguration_EF_TransmissionType;
  153. m_OptionsConfiguration.EngineSizeInLiters = objectRecord.EF_OptionsConfiguration_EngineSizeInLiters;
  154. m_OptionsConfiguration.HasNavigationSystem = objectRecord.EF_OptionsConfiguration_HasNavigationSystem;
  155. m_OptionsConfiguration.NumberOfCylinders = objectRecord.EF_OptionsConfiguration_NumberOfCylinders;
  156. m_OptionsConfiguration.NumberOfSpeeds = objectRecord.EF_OptionsConfiguration_NumberOfSpeeds;
  157. m_PriceHistory.Deserialize(datasource, objectRecord.YearlyFinancialAssessmentsRow);
  158. }
  159. #endregion
  160. }
  161. }