Hi Dokania

I hope that you do not doubt that Pharo is a cool system :)
Hello,

I am creating an Object Relational Mapper in pharo using sqlite.

While creating database I am using a dictionary for columns of Database. This 
gives me Keys as column name and values as Data type of those.

So suppose Employee is the the table name, with ID->Integer, Name->Text, 
Salary->Float.

emp:= Employee new.
Now emp is a row of table, so for each row user has to create instance of 
Employee and then feed values to it. Later on he can pass message insert query 
to emp.

So I was creating this dictionary for column at the instance side 
initialization of Employee.

My instructor suggested to do that in class side initialization,

My Doubts are:

1. Why should we do class side initialization instead of instance side 
initialization.

Because class side initialization is executing once when the class is loaded in memory. And I imagine that you do not want to change and execute the mapping declaration for every single
instance you will create. isn't?

So

Object subclass: #Employee
    instanceVariableNames: ''
    classVariableNames: 'Mappings'
    category: 'Foo-Model'


Employee class>>initialize

Mappings := { #ID->Integer . #Name->Text. #Salary->Float } asDictionary

makes a lot of sense.

or

Object subclass: #Employee
    instanceVariableNames: ''
    classVariableNames: ''
    category: 'Foo-Model'

Employee class
    instanceVariableNames: 'mappings'

Employee class>>initialize

mappings := { #ID->Integer . #Name->Text. #Salary->Float } asDictionary

Read pharo by example to understand the difference between class variable (Shared Variable) and class instance variables

2. Could you please refer me to the link which explains how to do class side 
initialization. Because when I do Employee new, I see only instance side 
initialization is being called.

Employee initialize

you send a message to the CLASS itself to get **ITS** initialize method execute. Normally when you load the class its initialize method is automatically executed.

When you do Employee new
    => Employee basicNew initialize
    => anEmployee initialize
    => anInitializedEmployee



Thanks,
Harshit





Reply via email to