- Timestamp:
- 06/19/13 15:25:31 (12 years ago)
- Location:
- branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs
r9641 r9645 2 2 using System.Collections.Generic; 3 3 using System.Data.Entity; 4 using System.Data.Entity.Infrastructure; 4 5 using System.Data.Entity.ModelConfiguration; 5 6 using System.Data.Entity.ModelConfiguration.Conventions; 7 using HeuristicLab.Services.Optimization.Billing.Interfaces; 6 8 using HeuristicLab.Services.Optimization.Billing.Model; 7 9 … … 28 30 public BillingContext(string connectionString) 29 31 : base(connectionString) { 32 33 Database.SetInitializer(new HeuristicLab.Services.Optimization.Billing.DataAccess.BillingContext.BillingContextInitiliazer()); 34 35 // Hook up event to mark existing entities as Unchanged 36 ((IObjectContextAdapter)this).ObjectContext.ObjectMaterialized += (sender, args) => { 37 var entity = args.Entity as IObjectWithState; 38 if (entity != null) { 39 entity.EntityState = State.Unchanged; 40 } 41 }; 42 30 43 this.Configuration.LazyLoadingEnabled = false; 31 44 this.Configuration.ProxyCreationEnabled = false; … … 33 46 34 47 protected override void OnModelCreating(DbModelBuilder modelBuilder) { 48 modelBuilder.Configurations.Add(new ContactInformationConfiguration()); 49 modelBuilder.Configurations.Add(new PaymentInformationConfiguration()); 50 modelBuilder.Configurations.Add(new ProductConfiguration()); 35 51 modelBuilder.Configurations.Add(new OrderConfiguration()); 36 52 modelBuilder.Configurations.Add(new OrderLineConfiguration()); … … 47 63 #region EntityTypeConfigurations 48 64 65 private class ContactInformationConfiguration : EntityTypeConfiguration<ContactInformation> { 66 internal ContactInformationConfiguration() { 67 this.Ignore(ci => ci.EntityState); 68 } 69 } 70 71 private class PaymentInformationConfiguration : EntityTypeConfiguration<PaymentInformation> { 72 internal PaymentInformationConfiguration() { 73 this.Ignore(pi => pi.EntityState); 74 } 75 } 76 77 private class ProductConfiguration : EntityTypeConfiguration<Product> { 78 internal ProductConfiguration() { 79 this.Ignore(p => p.EntityState); 80 } 81 } 82 49 83 private class OrderConfiguration : EntityTypeConfiguration<Order> { 50 84 internal OrderConfiguration() { 85 this.Ignore(o => o.EntityState); 51 86 this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId); 52 87 // TODO: OrderState? … … 56 91 private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> { 57 92 internal OrderLineConfiguration() { 93 this.Ignore(ol => ol.EntityState); 58 94 this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true); 59 95 this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId); … … 63 99 private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> { 64 100 internal InvoiceConfiguration() { 101 this.Ignore(i => i.EntityState); 65 102 this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId); 66 103 this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId); … … 70 107 private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> { 71 108 internal InvoiceLineConfiguration() { 109 this.Ignore(i => i.EntityState); 72 110 this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true); 73 111 this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId); … … 77 115 private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> { 78 116 internal UsageRecordConfiguration() { 117 this.Ignore(ur => ur.EntityState); 79 118 this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId); 80 119 this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId); … … 84 123 private class UserConfiguration : EntityTypeConfiguration<User> { 85 124 internal UserConfiguration() { 125 this.Ignore(u => u.EntityState); 86 126 this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId); 87 127 this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId); … … 166 206 BillingPeriod = BillingPeriod.Monthly, 167 207 }; 208 168 209 OrderLine ol1 = new OrderLine() { 169 210 Order = o1, … … 172 213 Quantity = 1 173 214 }; 215 174 216 o1 = context.Orders.Add(o1); 175 217 ol1 = context.OrderLines.Add(ol1); -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingDao.cs
r9641 r9645 2 2 using System; 3 3 using System.Collections.Generic; 4 using System.Data; 4 5 using System.Data.Entity; 5 6 using System.Linq; 6 7 using System.Linq.Expressions; 7 8 using HeuristicLab.Services.Optimization.Billing.Model; 9 using HeuristicLab.Services.Optimization.Billing.Utils; 8 10 namespace HeuristicLab.Services.Optimization.Billing.DataAccess { 9 11 public class BillingDao { … … 16 18 } 17 19 18 private delegate T ExecuteWithContextDelegate<T>(BillingContext context);19 private delegate void ExecuteWithContextDelegate(BillingContext context);20 21 private void ExecuteWithContext(ExecuteWithContextDelegate call) {22 BillingContext context = null;23 try {24 using (context = CreateContext()) {25 call(context);26 }27 }28 finally {29 if (context.Database.Connection.State == System.Data.ConnectionState.Open) {30 context.Database.Connection.Close();31 }32 }33 }34 35 private T ExecuteWithContext<T>(ExecuteWithContextDelegate<T> call) {36 BillingContext context = null;37 try {38 using (context = CreateContext()) {39 T result = call(context);40 return result;41 }42 }43 finally {44 if (context.Database.Connection.State == System.Data.ConnectionState.Open) {45 context.Database.Connection.Close();46 }47 }48 }49 50 20 #endregion 51 21 … … 53 23 54 24 public Product FindProductById(long id) { 55 return ExecuteWithContext<Product>((context) =>{25 using (var context = CreateContext()) { 56 26 return context.Products.FirstOrDefault(prod => prod.ProductId == id); 57 } );27 } 58 28 } 59 29 … … 72 42 } 73 43 74 public void AddProduct(Product entity) { 75 ExecuteWithContext((context) => { 76 context.Products.Add(entity); 44 public void InsertOrUpdateProduct(Product entity) { 45 using (var context = CreateContext()) { 46 throw new NotImplementedException(); 47 } 48 } 49 50 void DeleteProduct(Product entity) { 51 using (var context = CreateContext()) { 52 throw new NotImplementedException(); 53 } 54 } 55 56 #endregion 57 58 #region Order Methods 59 60 public void InsertOrUpdateOrder(Order entity) { 61 using (var context = CreateContext()) { 62 context.Orders.Add(entity); 63 context.ApplyStateChanges(); 77 64 context.SaveChanges(); 78 }); 79 } 80 81 public void UpdateProduct(Product entity) { 82 ExecuteWithContext((context) => { 83 context.Products.Attach(entity); 84 context.Entry(entity).State = System.Data.EntityState.Modified; 85 context.SaveChanges(); 86 }); 87 } 88 89 void DeleteProduct(Product entity) { 90 ExecuteWithContext((context) => { 91 context.Products.Remove(entity); 92 }); 93 } 94 95 #endregion 96 97 #region Order Methods 98 99 public void AddOrder(Order order) { 100 ExecuteWithContext((context) => { 101 context.Orders.Add(order); 102 context.SaveChanges(); 103 }); 65 } 104 66 } 105 67 … … 122 84 123 85 public OrderLine FindOrderLineById(long id) { 124 return ExecuteWithContext<OrderLine>((context) =>{86 using (var context = CreateContext()) { 125 87 return context.OrderLines.FirstOrDefault(ol => ol.OrderLineId == id); 126 } );88 } 127 89 } 128 90 … … 141 103 } 142 104 143 public void AddOrderLine(OrderLine entity) { 144 ExecuteWithContext((context) => { 145 context.OrderLines.Add(entity); 146 context.SaveChanges(); 147 }); 148 } 149 150 public void UpdateOrderLine(OrderLine entity) { 151 ExecuteWithContext((context) => { 152 context.OrderLines.Attach(entity); 153 context.Entry(entity).State = System.Data.EntityState.Modified; 154 context.SaveChanges(); 155 }); 105 public void InsertOrUpdateOrderLine(OrderLine entity) { 106 using (var context = CreateContext()) { 107 throw new NotImplementedException(); 108 } 156 109 } 157 110 158 111 void DeleteOrderLine(OrderLine entity) { 159 ExecuteWithContext((context) =>{160 context.OrderLines.Remove(entity);161 } );112 using (var context = CreateContext()) { 113 throw new NotImplementedException(); 114 } 162 115 } 163 116 … … 167 120 168 121 public User FindUserById(long id) { 169 return ExecuteWithContext<User>((context) =>{122 using (var context = CreateContext()) { 170 123 return context.Users.FirstOrDefault(user => user.UserId == id); 171 } );124 } 172 125 } 173 126 … … 186 139 } 187 140 188 public void AddUser(User entity) { 189 ExecuteWithContext((context) => { 190 context.Users.Add(entity); 191 context.SaveChanges(); 192 }); 193 } 194 195 public void UpdateUser(User entity) { 196 ExecuteWithContext((context) => { 197 context.Users.Attach(entity); 198 context.Entry(entity).State = System.Data.EntityState.Modified; 199 context.SaveChanges(); 200 }); 141 public void InsertOrUpdateUser(User entity) { 142 using (var context = CreateContext()) { 143 throw new NotImplementedException(); 144 } 201 145 } 202 146 203 147 void DeleteUser(User entity) { 204 ExecuteWithContext((context) =>{205 context.Users.Remove(entity);206 } );148 using (var context = CreateContext()) { 149 throw new NotImplementedException(); 150 } 207 151 } 208 152 … … 212 156 213 157 public UsageRecord FindUsageRecordById(long id) { 214 return ExecuteWithContext<UsageRecord>((context) =>{158 using (var context = CreateContext()) { 215 159 return context.UsageRecords.FirstOrDefault(prod => prod.ProductId == id); 216 } );160 } 217 161 } 218 162 … … 231 175 } 232 176 233 public void AddUsageRecord(UsageRecord entity) { 234 ExecuteWithContext((context) => { 235 context.UsageRecords.Add(entity); 177 public void InsertOrUpdateUsageRecord(UsageRecord entity) { 178 using (var context = CreateContext()) { 179 throw new NotImplementedException(); 180 } 181 } 182 183 void DeleteUsageRecord(UsageRecord entity) { 184 using (var context = CreateContext()) { 185 throw new NotImplementedException(); 186 } 187 } 188 189 #endregion 190 191 #region Invoice Methods 192 193 public void InsertOrUpdateInvoice(Invoice entity) { 194 using (var context = CreateContext()) { 195 context.Invoices.Add(entity); 196 context.ApplyStateChanges(); 236 197 context.SaveChanges(); 237 }); 238 } 239 240 public void UpdateUsageRecord(UsageRecord entity) { 241 ExecuteWithContext((context) => { 242 context.UsageRecords.Attach(entity); 243 context.Entry(entity).State = System.Data.EntityState.Modified; 244 context.SaveChanges(); 245 }); 246 } 247 248 void DeleteUsageRecord(UsageRecord entity) { 249 ExecuteWithContext((context) => { 250 context.UsageRecords.Remove(entity); 251 }); 252 } 253 254 #endregion 255 256 #region Invoice Methods 257 258 public void AddInvoice(Invoice entity) { 259 ExecuteWithContext((context) => { 260 context.Invoices.Add(entity); 261 context.SaveChanges(); 262 }); 198 } 263 199 } 264 200
Note: See TracChangeset
for help on using the changeset viewer.