Changeset 9645 for branches/OaaS/HeuristicLab.Services.Optimization.Billing
- Timestamp:
- 06/19/13 15:25:31 (11 years ago)
- Location:
- branches/OaaS/HeuristicLab.Services.Optimization.Billing
- Files:
-
- 4 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS/HeuristicLab.Services.Optimization.Billing/BillingEngine/BillingEngine.cs
r9641 r9645 89 89 usageCharges.Invoice = invoice; 90 90 usageCharges.Product = billingService.GetProductByName("Oaas Usage Charges").First(); 91 usageCharges.ProductPrice = -999.99;91 usageCharges.ProductPrice = 999.99; 92 92 usageCharges.Quantity = 1; 93 93 invoice.InvoiceLines.Add(usageCharges); … … 102 102 UpdateBillingState(order); 103 103 billingService.UpdateOrder(order); 104 billingService.SaveInvoice(invoice); 104 105 } 105 106 } … … 120 121 } 121 122 123 order.LastBillableDay = currentDate; 122 124 DateTime nextBillingDay = CalculateNextBillingDate(order); 123 125 … … 130 132 order.NextBillableDay = nextBillingDay; 131 133 } 134 135 order.EntityState = State.Modified; 132 136 } 133 137 -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/BillingService.cs
r9619 r9645 42 42 //TODO: Authentication 43 43 //TODO: Enable Transaction 44 BillingDao. AddOrder(order);44 BillingDao.InsertOrUpdateOrder(order); 45 45 } 46 46 … … 59 59 60 60 public void UpdateOrder(Order order) { 61 throw new System.NotImplementedException(); 61 //TODO: Authentication 62 //TODO: Enable Transaction 63 BillingDao.InsertOrUpdateOrder(order); 62 64 } 63 65 … … 75 77 //TODO: Authentication 76 78 //TODO: Enable Transaction 77 BillingDao. AddUser(user);79 BillingDao.InsertOrUpdateUser(user); 78 80 } 79 81 … … 104 106 } 105 107 108 public void SaveInvoice(Invoice invoice) { 109 //TODO: Authentication 110 //TODO: Enable Transaction 111 BillingDao.InsertOrUpdateInvoice(invoice); 112 } 113 106 114 #endregion 107 115 } -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Business/MockupBillingService.cs
r9619 r9645 134 134 } 135 135 136 public void SaveInvoice(Model.Invoice invoice) { 137 throw new NotImplementedException(); 138 } 139 136 140 #endregion 137 141 } -
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 -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/HeuristicLab.Services.Optimization.Billing.csproj
r9619 r9645 59 59 <Compile Include="Interfaces\IGenericDao.cs" /> 60 60 <Compile Include="Interfaces\IInvoiceFormattingEngine.cs" /> 61 <Compile Include="Interfaces\IObjectWithState.cs" /> 61 62 <Compile Include="Interfaces\IOptimizationBilling.cs" /> 62 63 <Compile Include="Business\MockupBillingService.cs" /> 63 64 <Compile Include="Model\Model.cs" /> 64 65 <Compile Include="Properties\AssemblyInfo.cs" /> 66 <Compile Include="Utils\DbContextUtil.cs" /> 67 <Compile Include="Utils\StateUtil.cs" /> 65 68 </ItemGroup> 66 69 <ItemGroup> -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Interfaces/IOptimizationBilling.cs
r9619 r9645 23 23 24 24 IList<Invoice> GetInvoices(string userName); 25 void SaveInvoice(Invoice invoice); 25 26 26 27 User GetUser(string userName); -
branches/OaaS/HeuristicLab.Services.Optimization.Billing/Model/Model.cs
r9641 r9645 2 2 using System; 3 3 using System.Collections.Generic; 4 using HeuristicLab.Services.Optimization.Billing.Interfaces; 4 5 namespace HeuristicLab.Services.Optimization.Billing.Model { 5 6 … … 14 15 */ 15 16 16 public class ContactInformation {17 public class ContactInformation : IObjectWithState { 17 18 public long ContactInformationId { get; set; } 18 19 public string OrganizationName { get; set; } … … 24 25 public string FirstName { get; set; } 25 26 public string Email { get; set; } 27 28 // Property is not mapped 29 // It is used to track state (disconnected entities) 30 public State EntityState { get; set; } 26 31 } 27 32 28 public class PaymentInformation {33 public class PaymentInformation : IObjectWithState { 29 34 public long PaymentInformationId { get; set; } 30 35 public long UserId { get; set; } … … 39 44 set { PaymentMethodValue = (int)value; } 40 45 } 46 47 public State EntityState { get; set; } 41 48 } 42 49 … … 60 67 } 61 68 62 public class Product {69 public class Product : IObjectWithState { 63 70 public long ProductId { get; set; } 64 71 public string Name { get; set; } … … 66 73 public string ProductType { get; set; } 67 74 public double Price { get; set; } 75 76 public State EntityState { get; set; } 68 77 } 69 78 70 public class User {79 public class User : IObjectWithState { 71 80 public long UserId { get; set; } 72 81 public long ContactInformationId { get; set; } … … 75 84 public virtual IList<PaymentInformation> PaymentInformation { get; set; } 76 85 public virtual ContactInformation ContactInformation { get; set; } 86 87 public State EntityState { get; set; } 77 88 } 78 89 … … 85 96 } 86 97 87 public class Order {98 public class Order : IObjectWithState { 88 99 public long OrderId { get; set; } 89 100 public long UserId { get; set; } … … 112 123 set { BillingPeriodValue = (int)value; } 113 124 } 125 126 public State EntityState { get; set; } 114 127 } 115 128 116 public class OrderLine {129 public class OrderLine : IObjectWithState { 117 130 public long OrderLineId { get; set; } 118 131 public long OrderId { get; set; } … … 123 136 public virtual Order Order { get; set; } 124 137 public virtual Product Product { get; set; } 138 139 public State EntityState { get; set; } 125 140 } 126 141 … … 129 144 } 130 145 131 public class Invoice {146 public class Invoice : IObjectWithState { 132 147 public long InvoiceId { get; set; } 133 148 public long UserId { get; set; } … … 148 163 set { StatusValue = (int)value; } 149 164 } 165 166 public State EntityState { get; set; } 150 167 } 151 168 152 public class InvoiceLine {169 public class InvoiceLine : IObjectWithState { 153 170 public long InvoiceLineId { get; set; } 154 171 public long InvoiceId { get; set; } … … 159 176 public virtual Invoice Invoice { get; set; } 160 177 public virtual Product Product { get; set; } 178 179 public State EntityState { get; set; } 161 180 } 162 181 163 public class UsageRecord {182 public class UsageRecord : IObjectWithState { 164 183 public long UsageRecordId { get; set; } 165 184 public long UserId { get; set; } … … 172 191 public virtual User User { get; set; } 173 192 public virtual Product Product { get; set; } 193 194 public State EntityState { get; set; } 174 195 } 175 196 }
Note: See TracChangeset
for help on using the changeset viewer.