Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Services.Optimization.Billing/DataAccess/BillingContext.cs @ 9645

Last change on this file since 9645 was 9645, checked in by spimming, 11 years ago

#1888:

  • Model classes implement new interface to track current state of entity
  • Mark EntityState property as 'not mapped'
  • Set hook to initialize entity as unchanged
  • Extension method for DbContext to apply changes only on modified entity
File size: 8.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Data.Entity;
4using System.Data.Entity.Infrastructure;
5using System.Data.Entity.ModelConfiguration;
6using System.Data.Entity.ModelConfiguration.Conventions;
7using HeuristicLab.Services.Optimization.Billing.Interfaces;
8using HeuristicLab.Services.Optimization.Billing.Model;
9
10namespace HeuristicLab.Services.Optimization.Billing.DataAccess {
11  public class BillingContext : DbContext {
12    public DbSet<Product> Products { get; set; }
13    public DbSet<User> Users { get; set; }
14    public DbSet<Order> Orders { get; set; }
15    public DbSet<OrderLine> OrderLines { get; set; }
16    public DbSet<Invoice> Invoices { get; set; }
17    public DbSet<InvoiceLine> InvoiceLines { get; set; }
18    public DbSet<UsageRecord> UsageRecords { get; set; }
19    public DbSet<ContactInformation> ContactInformations { get; set; }
20    public DbSet<PaymentInformation> PaymentInformations { get; set; }
21
22    // enum PaymentMethod
23    // enum OrderState
24
25    public BillingContext()
26      : this("name=BillingContext") {
27
28    }
29
30    public BillingContext(string connectionString)
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
43      this.Configuration.LazyLoadingEnabled = false;
44      this.Configuration.ProxyCreationEnabled = false;
45    }
46
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());
51      modelBuilder.Configurations.Add(new OrderConfiguration());
52      modelBuilder.Configurations.Add(new OrderLineConfiguration());
53      modelBuilder.Configurations.Add(new InvoiceConfiguration());
54      modelBuilder.Configurations.Add(new InvoiceLineConfiguration());
55      modelBuilder.Configurations.Add(new UsageRecordConfiguration());
56      modelBuilder.Configurations.Add(new UserConfiguration());
57
58      modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
59
60      base.OnModelCreating(modelBuilder);
61    }
62
63    #region EntityTypeConfigurations
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
83    private class OrderConfiguration : EntityTypeConfiguration<Order> {
84      internal OrderConfiguration() {
85        this.Ignore(o => o.EntityState);
86        this.HasRequired(o => o.User).WithMany().HasForeignKey(o => o.UserId);
87        // TODO: OrderState?
88      }
89    }
90
91    private class OrderLineConfiguration : EntityTypeConfiguration<OrderLine> {
92      internal OrderLineConfiguration() {
93        this.Ignore(ol => ol.EntityState);
94        this.HasRequired(ol => ol.Order).WithMany(o => o.OrderLines).HasForeignKey(ol => ol.OrderId).WillCascadeOnDelete(true);
95        this.HasRequired(ol => ol.Product).WithMany().HasForeignKey(ol => ol.ProductId);
96      }
97    }
98
99    private class InvoiceConfiguration : EntityTypeConfiguration<Invoice> {
100      internal InvoiceConfiguration() {
101        this.Ignore(i => i.EntityState);
102        this.HasRequired(i => i.User).WithMany().HasForeignKey(i => i.UserId);
103        this.HasRequired(i => i.Order).WithMany(o => o.Invoices).HasForeignKey(i => i.OrderId);
104      }
105    }
106
107    private class InvoiceLineConfiguration : EntityTypeConfiguration<InvoiceLine> {
108      internal InvoiceLineConfiguration() {
109        this.Ignore(i => i.EntityState);
110        this.HasRequired(il => il.Invoice).WithMany(i => i.InvoiceLines).HasForeignKey(il => il.InvoiceId).WillCascadeOnDelete(true);
111        this.HasRequired(il => il.Product).WithMany().HasForeignKey(il => il.ProductId);
112      }
113    }
114
115    private class UsageRecordConfiguration : EntityTypeConfiguration<UsageRecord> {
116      internal UsageRecordConfiguration() {
117        this.Ignore(ur => ur.EntityState);
118        this.HasRequired(ur => ur.User).WithMany().HasForeignKey(ur => ur.UserId);
119        this.HasRequired(ur => ur.Product).WithMany().HasForeignKey(ur => ur.ProductId);
120      }
121    }
122
123    private class UserConfiguration : EntityTypeConfiguration<User> {
124      internal UserConfiguration() {
125        this.Ignore(u => u.EntityState);
126        this.HasMany(u => u.PaymentInformation).WithRequired(pi => pi.User).HasForeignKey(pi => pi.UserId);
127        this.HasRequired(u => u.ContactInformation).WithMany().HasForeignKey(u => u.ContactInformationId);
128      }
129    }
130
131    #endregion
132
133    #region DB Seed Methods
134
135    // - DropCreateDatabaseAlways<BillingContext>
136    // - DropCreateDatabaseIfModelChanges<BillingContext>
137    public class BillingContextInitiliazer : DropCreateDatabaseAlways<BillingContext> {
138      protected override void Seed(BillingContext context) {
139        Product p1 = new Product() {
140          Name = "OaaS Basic Service Charge",
141          Description = "Create and run your experiments within HeuristicLab Hive",
142          Price = 20.0,
143          ProductType = "Optimization Service"
144        };
145
146        Product p2 = new Product() {
147          Name = "Oaas Usage Charges",
148          Description = "Charges for OaaS based on usage data",
149          Price = 0.0,
150          ProductType = "Optimization Service"
151        };
152
153        Product p3 = new Product() {
154          Name = "Oaas Support Package",
155          Description = "Charges for OaaS based on usage data",
156          Price = 50.0,
157          ProductType = "Optimization Service"
158        };
159
160        p1 = context.Products.Add(p1);
161        p2 = context.Products.Add(p2);
162        p3 = context.Products.Add(p3);
163
164        User u1 = new Model.User() { Name = "spimming" };
165        User u2 = new Model.User() {
166          Name = "fschoeppl",
167          PaymentInformation = new List<PaymentInformation>() {
168            new PaymentInformation() {
169              PaymentMethod = PaymentMethod.Visa,
170              CardNumber = "123456789" } }
171        };
172
173        u1 = context.Users.Add(u1);
174        u2 = context.Users.Add(u2);
175
176        ContactInformation ci1 = new ContactInformation() {
177          FirstName = "Max",
178          LastName = "Mustermann",
179          Email = "max.mustermann@fh-hagenberg.at",
180          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
181          Street = "Softwarepark 11",
182          PostalCode = "4232",
183          City = "Hagenberg",
184        };
185
186        ci1 = context.ContactInformations.Add(ci1);
187        u1.ContactInformation = ci1;
188
189        ContactInformation ci2 = new ContactInformation() {
190          FirstName = "Max",
191          LastName = "Mustermann",
192          Email = "max.mustermann@fh-hagenberg.at",
193          OrganizationName = "University of Applied Sciences Upper Austria " + Environment.NewLine + "School of Informatics/Communications/Media",
194          Street = "Softwarepark 11",
195          PostalCode = "4232",
196          City = "Hagenberg",
197        };
198
199        ci2 = context.ContactInformations.Add(ci2);
200        u2.ContactInformation = ci2;
201
202        Order o1 = new Order() {
203          User = u2,
204          State = OrderState.Created,
205          BillingType = BillingType.Post,
206          BillingPeriod = BillingPeriod.Monthly,
207        };
208
209        OrderLine ol1 = new OrderLine() {
210          Order = o1,
211          Product = p1,
212          ProductPrice = p1.Price,
213          Quantity = 1
214        };
215
216        o1 = context.Orders.Add(o1);
217        ol1 = context.OrderLines.Add(ol1);
218
219        context.SaveChanges();
220
221        o1.State = OrderState.Active;
222        o1.ActiveSince = DateTime.Now;
223        context.Entry(o1).State = System.Data.EntityState.Modified;
224        context.SaveChanges();
225      }
226    }
227
228    #endregion
229  }
230}
Note: See TracBrowser for help on using the repository browser.