Hello to everydoby, I've been reading many threads from this newsgroup and I find it very interesting because it's one of the few that speaks about design and architecture… I'm learning to design good OOP application (I've been working with Java), but it's
not easy, I need to make some experience. I'd like people to help me to improve my design. I tried to use the 3 tier approach, since I realized it's one of the most common.. I use some Managers to deal with the DATA level. I use an interface to make my Cart
able to save different kind of products. I'm still working on it, I have to add methods and new features.. Please give my some inputs to improve it, so I can learn! This is part of my code, I'm working on it: public class Cart { private int mUserID; private
int mID; private ArrayList list; public int ID public Cart(int IDcart) public Cart() : this(-1) public void AddItem(CartItem item) public void AddItem(CartItem item, int quantity) public void RemoveItem(CartItem item) public void RemoveItem(int ID) public
void setItemQuantity(CartItem item, int newQuantity) public Order GetOrder(CartItem item) public bool Contains(int ID) public ArrayList GetOrders() } Q: Should I add CartItem objects to my Cart, or just the ID of the CartItem? public abstract class CartItem
{ public abstract float Price {get; set;} public abstract int ID {get; set;} public bool Equals(CartItem item) { return (item.ID == this.ID); } public override bool Equals(object item) { if (!(item is CartItem) ) { return false; } else { return Equals(item);
} } } Q: what about the implementation of Equals is it ok? I used the abstract class CartItem because I had to override Equals. Before overriding this method I called if IcartItem and it was an interface. public class CartManager : Manager { MySqlConnection
connection; ICartItemManager itemManager; HttpRequest request; HttpResponse response; HttpSessionState session; string mIDcart; public string IDcart { get { if (mIDcart == null) { mIDcart = (string)session["IDcart"]; } return mIDcart; } } public CartManager(ICartItemManager
manager, HttpSessionState session, HttpRequest request, HttpResponse response) { connection = Connect(); itemManager = manager; this.request = request; this.response = response; this.session = session; if (this.IsSetSession() == false) { DateTime dtNow = DateTime.Now;
mIDcart = session.SessionID.ToString() + dtNow.ToString(); //Debug.Print("la sessione non c'è"); this.SetSession(); } } private MySqlDataReader selectItems(string tableName, int IDuser) public void Save(Cart cart) public bool IsSetSession() { return !((string)session["IDcart"]==null);
} public void SetSession() { session["IDcart"] = mIDcart; } public bool IsSetCookie() { HttpCookie cookie; cookie = request.Cookies["IDcart"]; return !(cookie == null); } public void SetCookie() { HttpCookie cookie; cookie = new HttpCookie("IDcart"); cookie.Value
= "aaa"; DateTime dtNow = DateTime.Now; TimeSpan tsMinute = new TimeSpan(30, 0, 0, 0); cookie.Expires = dtNow + tsMinute; response.Cookies.Add(cookie); } public Cart Load(int IDcart) { } Q: I have to pass a reference of the ItemManager to my CartManager cause
when I want to load a Cart I have to fill it with Items, but I cant only load the Items if I have the ItemManager. Am I messing up the 3 levels? Q: In this class I have the biggest problem. I need to save on a cookie the ID of the Cart and it must be on a
Session too, because I'm not sure all the users accept cookies. So I need to pass a reference to my CartManager of Session, Request and Response to deals with Session and Cookies when I save it for the first time... I'm not sure this is good, cause seems to
me that I'm not separating the 3 levels… When I load the Cart with the CartManager I have a Cart with an IDcart. When I create the Cart for the first time I don't know the ID, I know it only when I save it for the first time. I don't think it's much nice,
but how I can do? Any pattern to solve this problem? Q: in all my Managers I have method to Load, Save and so on Classes. Do I have to set them static? public interface ICartItemManager { CartItem getItem(int ID); } I public abstract class Manager { public
MySqlConnection Connect() { return new MySqlConnection(ConfigurationSettings.AppSettings["connectionString"]); } } Q: Good thing to have a base class with a method to make connections? I read in some threads I should not do it (?) public class Order { CartItem
mItem; int mQuantity; public Order(CartItem item, int quantity) public CartItem Item public int Quantity } public class Poster : CartItem { private string mCode; private string mAuthor; private int mID; private float mPrice; public Poster() public override
int ID { get { return mID;} set { if (mID == 0) { mID = value; } else { throw new WriteOnceException(); } } } public string Code public override float Price public string Author } public class PosterManager : Manager, ICartItemManager { private MySqlConnection
connection; public PosterManager() public DataView getPosters() private MySqlDataReader selectPoster(int ID) public CartItem getItem(int ID) } I know that is a very long post, hope you have some time to read it all..
I've been thinkin all day about my design.. I realized that the ID of the Cart doesnt have to stay inside the CartManager. I have to get it out from the CartManager, this is a problem of the UserManager, i had to create the class User. the User Manager has
to provide me a User and get his SessionID. Since the user ID is unknown until i had the user to the DB, but i have to store the Cart even if i dont know the user ID, i thought to save the Cart in the DB with a SessionID of the User. Once the user decides
to register, i can register and the i can buy what he has in his Cart, i can save the Items in the Cart using a new ID, the UserID. It that a better solution? Your help is really appreciate. Luigi this is the code of the UserManager public class UserManager
{ public static IUser UserInit(HttpSessionState session, HttpRequest request, HttpResponse response) { string userID; HttpCookie cookie; DateTime dtNow; cookie = request.Cookies["userID"]; // check whether a cookie exists or not if (cookie == null || cookie.Value.ToString().Length<35)
{ /* This is a new user or an existing user with no cookies */ Debug.Print("there isnt a cookie"); // check whether the session exists or not if ((string)session["userID"] == null) { /* The session doesn't exists, i have to create it and then add it to * the
IUser objetc, this is the first time the user enters the site in this session */ Debug.Print("there isnt a session"); dtNow = DateTime.Now; userID = session.SessionID.ToString() + dtNow.ToString("G", DateTimeFormatInfo.InvariantInfo); userID = userID.Replace(".","");
userID = userID.Replace(" ",""); userID = userID.Replace("/",""); userID = userID.Replace(":",""); // set the session session["userID"] = userID; } else { /* The use has already a SessionID, i load it. */ Debug.Print("there is a session"); userID = session["userID"].ToString();
} /* * Until the user logs in I consider it a new user, so i give him * a new SessionID * * The cookie doesnt exists I try to create it, i'm not sure * the client accepts cookies */ cookie = new HttpCookie("userID"); cookie.Value = userID; dtNow = DateTime.Now;
TimeSpan tsMinute = new TimeSpan(30, 0, 0, 0); cookie.Expires = dtNow + tsMinute; response.Cookies.Add(cookie); } else { /* I know that the user has a cookie, it contais a SessionID, i create new user with existing SessionID * * I check wheter the SessionID
exists, if not i create it */ Debug.Print("there is a cookie"); userID = cookie.Value.ToString(); if ((string)session["userID"]==null) { /* The session doesn't exists, i have to create it and then add it to * the IUser objetc, this is the first time the user
enters the site in this session */ Debug.Print("there isnt a session"); // set the session session["userID"] = userID; } else { /* The use has already a SessionID, i load it, it should be the same as the Cookie, * if not i consider the SessionID the right
one */ Debug.Print("there is a session"); // check whether the session and the Cookie are different if (userID != session["userID"].ToString()) { Debug.Print("cookie and session are different!! use session"); userID = session["userID"].ToString(); } } } Debug.Print("create
the use with SessionID " + userID); return new User(userID); } }
Why are you using session in storing items of your cart? Its actually ok but there are better ways of doing that. See the source project of IBuy store of the this site.
I save the items of the cart in the database. i have two table tbl_cart (IDcart, dateLastUpdate) tbl_cart_item (UDcart,IDitem,quantity) i use the session and cookie just to store the ID of the cart, what i called UserIDSession that becomes the IDcart. i did
that because i want to save the cart of the user even if he leaves the site and then after 10 days he come back. He can still find his cart. Is that a bad solution to use session to pass the IDcart from one page to another and the cookie ti save the IDcart
in the user client? Consider that the user can add items to his cart even if he's not registered yet.. Thank you very much for you input. WHat about the 3 tier approach, is that ok? Luigi
Cookies are only good for storing small objects like Clients Fullname and ID. Since you are doing a shopping cart (?), store items in the cache. Of course, a three tier approach is always the best way. :p Hope this helps.
>> Cookies are only good for storing small objects like Clients Fullname and ID. i perfectly agree >> Since you are doing a shopping cart (?), store items in the cache. sorry i didnt understand. i thought you suggested NOT to store items in the cache, but just
store the ID of cart! >> Of course, a three tier approach is always the best way the main reason of this post is to understand whether i'm implenting the 3 tier approach in the best way or if i can improve it my design.. Can you give me some advice about my
architecture? thanks Luigi Malagò
luigi_malago
Member
70 Points
14 Posts
Design of an e-commerce site
Dec 03, 2003 06:53 AM|LINK
luigi_malago
Member
70 Points
14 Posts
Re: Design of an e-commerce site
Dec 03, 2003 06:23 PM|LINK
cao
Member
712 Points
149 Posts
Re: Design of an e-commerce site
Dec 04, 2003 07:42 AM|LINK
luigi_malago
Member
70 Points
14 Posts
Re: Design of an e-commerce site
Dec 04, 2003 09:11 AM|LINK
cao
Member
712 Points
149 Posts
Re: Design of an e-commerce site
Dec 05, 2003 12:49 AM|LINK
luigi_malago
Member
70 Points
14 Posts
Re: Design of an e-commerce site
Dec 05, 2003 07:02 PM|LINK