Should i do the same thing twice? [closed]
up vote
-3
down vote
favorite
I work with c# mvc and entity framework.
I write code for an e shop and i'm trying to write code for the cart.
The cart has a list of cartItems and a cartitem has a product and a quantity variable.
When i want to remove a cartItem from the cart i call the cart.remove(int productid) function i write the appropriate entity framework code to remove it from the database but i also removethe cartitem from the cart class that exists in memory.
I am doing two times the almost the same thing in order to my object to be synchronized with the changes i make to the database. But this does not look very efficient to me.
One time to update the database and one time to update the object that resides in memory.
And i'm doing that almost for every function in my cart class.
here is my classes
public class UserCart
{
private EshopContext db;
[ForeignKey("user")]
public string id { get; set; }
public virtual User.User user { get; set; }
public virtual List<CartItem> cartItems { get; set; }
[ForeignKey("voucher")]
public int voucherId;
public Voucher voucher { get; set; }
public double SubTotalCost { get; set; }
public UserCart(string userId, EshopContext db)
{
this.id = userId;
this.db = db;
db.UserCarts.Add(this);
db.SaveChanges();
}
public List<CartItem> getCartItems()
public void add(int productId)
public void remove(int productId)
{
//remove from the database
CartItem item = db.CartItems.Where(p => p.serialnum == productId && p.cartId == this.id).FirstOrDefault();
if (item != null)
{
if(item.quantity > 1)
{
item.quantity -= 1;
db.SaveChanges();
}
else
{
db.CartItems.Remove(item);
db.SaveChanges();
}
}
//remove from the class
CartItem item2 = cartItems.Where(p => p.serialnum == productId).FirstOrDefault();
if (item2 != null)
{
if (item2.quantity > 1)
{
item2.quantity -= 1;
}
else
{
cartItems.Remove(item2);
}
}
}
public double calculateCost()
public void addVoucher(int voucherId)
public void removeVoucher()
public Voucher getVoucher()
public void clearCart()
}
c# oop entity-framework-6
closed as unclear what you're asking by Dour High Arch, Daniel Mann, Brian Rogers, Gert Arnold, Servy Nov 12 at 18:45
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
-3
down vote
favorite
I work with c# mvc and entity framework.
I write code for an e shop and i'm trying to write code for the cart.
The cart has a list of cartItems and a cartitem has a product and a quantity variable.
When i want to remove a cartItem from the cart i call the cart.remove(int productid) function i write the appropriate entity framework code to remove it from the database but i also removethe cartitem from the cart class that exists in memory.
I am doing two times the almost the same thing in order to my object to be synchronized with the changes i make to the database. But this does not look very efficient to me.
One time to update the database and one time to update the object that resides in memory.
And i'm doing that almost for every function in my cart class.
here is my classes
public class UserCart
{
private EshopContext db;
[ForeignKey("user")]
public string id { get; set; }
public virtual User.User user { get; set; }
public virtual List<CartItem> cartItems { get; set; }
[ForeignKey("voucher")]
public int voucherId;
public Voucher voucher { get; set; }
public double SubTotalCost { get; set; }
public UserCart(string userId, EshopContext db)
{
this.id = userId;
this.db = db;
db.UserCarts.Add(this);
db.SaveChanges();
}
public List<CartItem> getCartItems()
public void add(int productId)
public void remove(int productId)
{
//remove from the database
CartItem item = db.CartItems.Where(p => p.serialnum == productId && p.cartId == this.id).FirstOrDefault();
if (item != null)
{
if(item.quantity > 1)
{
item.quantity -= 1;
db.SaveChanges();
}
else
{
db.CartItems.Remove(item);
db.SaveChanges();
}
}
//remove from the class
CartItem item2 = cartItems.Where(p => p.serialnum == productId).FirstOrDefault();
if (item2 != null)
{
if (item2.quantity > 1)
{
item2.quantity -= 1;
}
else
{
cartItems.Remove(item2);
}
}
}
public double calculateCost()
public void addVoucher(int voucherId)
public void removeVoucher()
public Voucher getVoucher()
public void clearCart()
}
c# oop entity-framework-6
closed as unclear what you're asking by Dour High Arch, Daniel Mann, Brian Rogers, Gert Arnold, Servy Nov 12 at 18:45
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
I work with c# mvc and entity framework.
I write code for an e shop and i'm trying to write code for the cart.
The cart has a list of cartItems and a cartitem has a product and a quantity variable.
When i want to remove a cartItem from the cart i call the cart.remove(int productid) function i write the appropriate entity framework code to remove it from the database but i also removethe cartitem from the cart class that exists in memory.
I am doing two times the almost the same thing in order to my object to be synchronized with the changes i make to the database. But this does not look very efficient to me.
One time to update the database and one time to update the object that resides in memory.
And i'm doing that almost for every function in my cart class.
here is my classes
public class UserCart
{
private EshopContext db;
[ForeignKey("user")]
public string id { get; set; }
public virtual User.User user { get; set; }
public virtual List<CartItem> cartItems { get; set; }
[ForeignKey("voucher")]
public int voucherId;
public Voucher voucher { get; set; }
public double SubTotalCost { get; set; }
public UserCart(string userId, EshopContext db)
{
this.id = userId;
this.db = db;
db.UserCarts.Add(this);
db.SaveChanges();
}
public List<CartItem> getCartItems()
public void add(int productId)
public void remove(int productId)
{
//remove from the database
CartItem item = db.CartItems.Where(p => p.serialnum == productId && p.cartId == this.id).FirstOrDefault();
if (item != null)
{
if(item.quantity > 1)
{
item.quantity -= 1;
db.SaveChanges();
}
else
{
db.CartItems.Remove(item);
db.SaveChanges();
}
}
//remove from the class
CartItem item2 = cartItems.Where(p => p.serialnum == productId).FirstOrDefault();
if (item2 != null)
{
if (item2.quantity > 1)
{
item2.quantity -= 1;
}
else
{
cartItems.Remove(item2);
}
}
}
public double calculateCost()
public void addVoucher(int voucherId)
public void removeVoucher()
public Voucher getVoucher()
public void clearCart()
}
c# oop entity-framework-6
I work with c# mvc and entity framework.
I write code for an e shop and i'm trying to write code for the cart.
The cart has a list of cartItems and a cartitem has a product and a quantity variable.
When i want to remove a cartItem from the cart i call the cart.remove(int productid) function i write the appropriate entity framework code to remove it from the database but i also removethe cartitem from the cart class that exists in memory.
I am doing two times the almost the same thing in order to my object to be synchronized with the changes i make to the database. But this does not look very efficient to me.
One time to update the database and one time to update the object that resides in memory.
And i'm doing that almost for every function in my cart class.
here is my classes
public class UserCart
{
private EshopContext db;
[ForeignKey("user")]
public string id { get; set; }
public virtual User.User user { get; set; }
public virtual List<CartItem> cartItems { get; set; }
[ForeignKey("voucher")]
public int voucherId;
public Voucher voucher { get; set; }
public double SubTotalCost { get; set; }
public UserCart(string userId, EshopContext db)
{
this.id = userId;
this.db = db;
db.UserCarts.Add(this);
db.SaveChanges();
}
public List<CartItem> getCartItems()
public void add(int productId)
public void remove(int productId)
{
//remove from the database
CartItem item = db.CartItems.Where(p => p.serialnum == productId && p.cartId == this.id).FirstOrDefault();
if (item != null)
{
if(item.quantity > 1)
{
item.quantity -= 1;
db.SaveChanges();
}
else
{
db.CartItems.Remove(item);
db.SaveChanges();
}
}
//remove from the class
CartItem item2 = cartItems.Where(p => p.serialnum == productId).FirstOrDefault();
if (item2 != null)
{
if (item2.quantity > 1)
{
item2.quantity -= 1;
}
else
{
cartItems.Remove(item2);
}
}
}
public double calculateCost()
public void addVoucher(int voucherId)
public void removeVoucher()
public Voucher getVoucher()
public void clearCart()
}
c# oop entity-framework-6
c# oop entity-framework-6
edited Nov 12 at 18:42
asked Nov 11 at 18:12
Vasilis Dellas
36
36
closed as unclear what you're asking by Dour High Arch, Daniel Mann, Brian Rogers, Gert Arnold, Servy Nov 12 at 18:45
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
closed as unclear what you're asking by Dour High Arch, Daniel Mann, Brian Rogers, Gert Arnold, Servy Nov 12 at 18:45
Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42
add a comment |
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Whilst @GertArnold's comment is right, it sounds to me like you are approaching this wrong. In my experience, you would update your in-memory object and then save that to the database. Doing it twice is a bad idea.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Whilst @GertArnold's comment is right, it sounds to me like you are approaching this wrong. In my experience, you would update your in-memory object and then save that to the database. Doing it twice is a bad idea.
add a comment |
up vote
0
down vote
accepted
Whilst @GertArnold's comment is right, it sounds to me like you are approaching this wrong. In my experience, you would update your in-memory object and then save that to the database. Doing it twice is a bad idea.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Whilst @GertArnold's comment is right, it sounds to me like you are approaching this wrong. In my experience, you would update your in-memory object and then save that to the database. Doing it twice is a bad idea.
Whilst @GertArnold's comment is right, it sounds to me like you are approaching this wrong. In my experience, you would update your in-memory object and then save that to the database. Doing it twice is a bad idea.
answered Nov 11 at 22:50
Avrohom Yisroel
3,33132753
3,33132753
add a comment |
add a comment |
a will try to refactor this according to your comments,thank you
– Vasilis Dellas
Nov 11 at 19:42