0

I am using Entity Framework mapped to my database. I have a Basket model which can have many BasketItem models, and I have Promotions and Coupons models. This is for eCommerce checkout functionality and I just don't understand how this will work, here goes:

Because my BasketItems have a foreign key relationship to the Basket if I want to sum up the Subtotal for my basket items in a partial class, I can do this:

        public decimal Subtotal {
        get {
            return this.BasketItems.Sum(pb => pb.Subtotal);
            }
        }

This is helpful because I can use this inside a view, there's no mucking around with passing a DB context through and it's DRY, etc. etc.

Now I want to apply promotions or coupons to my Subtotal ideally I want it to look like this:

        public decimal DiscountedSubtotal {
        get { 
            decimal promotions_discount = 0;
            decimal coupons_discount = 0;
            return Subtotal - promotions_discount - coupons_discount;
            }
        }

But there is no access to Promotions or Coupons without either creating some crazy and unnecessary relationships in my database or some light hacking to get this functionality to work. I don't know what I should do to overcome this problem.

Solution 1:

        public decimal DiscountedSubtotal(DatabaseEntities db) {
        decimal promotions_discount = from p in db.Promotions
                                  select p.Amount;
        decimal coupons_discount = from c in db.Coupons
                               select c.Amount;
        return Subtotal - promotions_discount - coupons_discount;
        }

I don't want to use this in my View pages, plus I have to send through my context every time I want to use it.

Solution 2: (untested)

    public List<Promotion> Promotions { get; set; }
    public List<Coupon> Coupons { get; set; }

    public Basket()
        : base() {
            DatabaseEntities db = new DatabaseEntities();
            Promotions = db.Promotions.ToList();
            Coupons = db.Coupons.ToList();
    }

A bit of light hacking could provide me with references to promotions and coupons but i've had problems with creating new contexts before and I don't know if there is a better way to get me to the DiscountedSubtotal property I would ideally like.

So to sum up my question, I would like to know the best way to get a DiscountedSubtotal property.

Many thanks and apologies for such a long read :)

6
  • Can you provide the code for Promotions and Coupons, as well as your view and model? Also, what do you mean that you've had problems with creating new contexts? Commented Oct 24, 2012 at 17:42
  • Promotions and Coupons are just your plain old entity framework mapped objects. If I create a new instance of my context inside of an object that has a reference to the context it causes a runtime error stating both cannot be used at the same time or something. Commented Oct 24, 2012 at 18:04
  • It sounds like your connection string is missing MultipleActiveResultSets=True Commented Oct 24, 2012 at 18:22
  • oo I'll have to give that a go, thanks :) Doesn't sound like it'll be too friendly in shared hosting environment though :/ Commented Oct 24, 2012 at 19:10
  • Not sure what a shared hosting environment has to do with anything.. Commented Oct 24, 2012 at 19:13

1 Answer 1

1

I think the problem here is that you're not really using a coherent architecture.

In most cases, you should have a business layer to handle this kind of logic. Then that business layer would have functions like CalculateDiscountForProduct() or CalculateNetPrice() that would go out to the database and retrieve the data you need to complete the business rule.

The business class would talk to a data layer that returns data objects. Your view only needs it's view model, which you populate from the business objects returned by your businesss layer.

A typical method might be:

public ActionResult Cart() {
    var model = _cartService.GetCurrentCart(userid);
    return View(model);
}

So when you apply a discount or coupon, you would call a method like _cartService.ApplyDiscount(model.DiscountCode); then return the new model back to the view.

You might do well to study the Mvc Music Store sample project, as it includes cart functionality and promo codes.

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1

Sign up to request clarification or add additional context in comments.

3 Comments

I see, I keep bumping into this problem. I need to separate my View models from my Data models. Would you say I still need a business layer/wrapper between them?
Also, are you saying that MVC and 3-tier fit well together and are not opposing methodologies? thanks for taking the time :)
@Smithy - I know all the examples show data access in your controller, but that's just not a correct way to go about it long term. Yes, MVC and 3-tier fit well together and are complementary. MVC is a UI layer (yes, even the model) so it fits in the UI tier. You may find this article useful codeproject.com/Articles/70061/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.