PC Review


Reply
Thread Tools Rate Thread

A design question

 
 
Tony Johansson
Guest
Posts: n/a
 
      27th Apr 2009
Hello!

If I want to store a product and remove a product on a file will it be
suitable to use two classes ?

One class for the actual product and
one class which is accessing the file and storing the passed object into the
file.


//Tony


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      27th Apr 2009
IMHO, yes.

public class Product

public class ProductController
//Or ProductManager


You can see an example of this here:


"Tony Johansson" <(E-Mail Removed)> wrote in message
news:0WoJl.8246$(E-Mail Removed)...
> Hello!
>
> If I want to store a product and remove a product on a file will it be
> suitable to use two classes ?
>
> One class for the actual product and
> one class which is accessing the file and storing the passed object into
> the file.
>
>
> //Tony
>



 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      27th Apr 2009
IMHO, yes.

public class Product

public class ProductController
//Or ProductManager


You can see an example of this here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!176.entry

"Tony Johansson" <(E-Mail Removed)> wrote in message
news:0WoJl.8246$(E-Mail Removed)...
> Hello!
>
> If I want to store a product and remove a product on a file will it be
> suitable to use two classes ?
>
> One class for the actual product and
> one class which is accessing the file and storing the passed object into
> the file.
>
>
> //Tony
>




 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      28th Apr 2009
Tony,

Imo not, a product is an entity, that entity has methods, but it still stays
the same entity.

Cor

"Tony Johansson" <(E-Mail Removed)> wrote in message
news:0WoJl.8246$(E-Mail Removed)...
> Hello!
>
> If I want to store a product and remove a product on a file will it be
> suitable to use two classes ?
>
> One class for the actual product and
> one class which is accessing the file and storing the passed object into
> the file.
>
>
> //Tony
>


 
Reply With Quote
 
Peter Morris
Guest
Posts: n/a
 
      28th Apr 2009
Unless I misunderstood he just asked

"Shall I have a Product class, and a class for loading/storing its state in
a file"

Did you read the question differently, or are you saying he should put
persistence into the Product class?



--
Pete
====
http://mrpmorris.blogspot.com
http://www.AlterEgos.com - Who do you want to be?

 
Reply With Quote
 
Cor Ligthert[MVP]
Guest
Posts: n/a
 
      28th Apr 2009
Peter,

When I was reading it the first time, I probably have read a comma on a
different place (it is exactly there broken in my reader), now you point me
on it and I read it again, then I see it in another way. However I do not
see a reason for two classes in this. The product class can have its methods
doing the reading and writing. But you are right , as you tell that the
entity has nothing to do with it.

Cor



"Peter Morris" <(E-Mail Removed)> wrote in message
news:%23rPAIN%(E-Mail Removed)...
> Unless I misunderstood he just asked
>
> "Shall I have a Product class, and a class for loading/storing its state
> in a file"
>
> Did you read the question differently, or are you saying he should put
> persistence into the Product class?
>
>
>
> --
> Pete
> ====
> http://mrpmorris.blogspot.com
> http://www.AlterEgos.com - Who do you want to be?


 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      28th Apr 2009
On Apr 27, 4:52*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> Hello!
>
> If I want to store a product and remove a product on a file will it be
> suitable to use two classes ?
>
> One class for the actual product and
> one class which is accessing the file and storing the passed object into the
> file.
>
> //Tony


Hi
It depends of the design you are using. You can have a design where
the class Product just hold its basic data and you have another
classes (like Service) which take care of saving/Load operations. or
you can have method in the same class that takes care of that
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      28th Apr 2009
"Tony Johansson" <(E-Mail Removed)> wrote in message
news:0WoJl.8246$(E-Mail Removed)...

> If I want to store a product and remove a product on a file will it be
> suitable to use two classes ?
>
> One class for the actual product and
> one class which is accessing the file and storing the passed object into
> the file.


I think this is basically a good idea, but unless you may need to store the
product in different formats, I say let the product be in control of its own
on-disc representation. In other words, let the product serialize itself.
Then all your "product writer" class worries about is opening, seeking, and
writing to the file. It will just ask the product to serialize itself and
then write the received bytes out.

All that said, I recently wrote something where there was a base object
which could be saved in either 1.0 or 2.0 format. In this case, I had a
formatter class for each output type which was responsible for creating the
byte stream to represent the object in that particular format.

So, as always, "it depends."


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      30th Apr 2009
This is how I would see it and the choice comes down to your Project Brief.

Designing Objects with responsibilities has some advantages especially
around information expert and low coupling, but it becomes clumsy and when
it eventualy ends up doing everything has poor Cohesion. However creating a
Pure Fabrication for say storage will increase Cohesion and in my opinion
Coupling can be kept low by good use of interfaces / facade.

Below shows a layered approach with the model just a set of properties.
Interfaces have been left out its just to give an idea.

UI

M Business Logic Layer (Order
Handler)

O STORAGE STRATEGY

D Database Storage Strategy(DAL) File Storage Strategy
(FAL)

E ADO.NET Oracle.NET System.IO
System.XML System.Serialization

L


Alternative Model does the work.

UI

MODEL

ADO.NET Oracle.NET System.IO System.XML
System.Serialization


You can see the advantage of the Layer Structure immediately. Adding say a
product to a basket goes from UI code of (lets face it a user clicking on a
button rarely in USE CASE/ BLL terms just relates to storing an object for
the purpose of saving an object)

-load basket from storage
-add product to basket
-basket.Calculate Total
-basket.Calculate discounts
-basket .back to storage

and whetever else you would do

to a call to the BLL of

AddProductToBasket which then calls each seperate piece of code that
specializes in a funtion.

Not quite MVC but IMHO not that far off and it also reflects USE CASES so
testing the BLL can be done without the UI as in MVC.

The rule I tend to follow is the more complex you make any object the more
likely it is to fail, so small pockets of highly cohesive
objects/applications once unit tested are more likely to stand the test of
time. But obviously there is some development overhead, and very differing
opinions on this trust me. ;-)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Design question... Nick Z. Microsoft C# .NET 18 6th Oct 2005 05:35 PM
Design Question epigram Microsoft ASP .NET 1 13th Jan 2005 02:19 PM
Design Question apu Microsoft Access Database Table Design 3 29th Dec 2004 05:27 PM
OOP design question John Wood Microsoft C# .NET 6 22nd Apr 2004 03:44 PM
Re: Basic table design - design question John Vinson Microsoft Access Getting Started 1 26th Jun 2003 05:22 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:43 PM.