Lazy Loading

1,070 views
Skip to first unread message

Oliver Castle

unread,
Dec 18, 2012, 9:23:05 AM12/18/12
to golan...@googlegroups.com
Hi

I am using a repository pattern to wrap my database logic. All the database logic will then be wrapped into a service struct but every time I create the service I dont want to create all of the repositories. Is there a good way to lazy load these objects?

type Person struct {
Id              int
UserName        string
EmailAddress    string
Password        string
IsActive        bool
DateJoined      string
LastLoggedOn    string
IsAdministrator bool
}
type PersonRepository struct{}
func (r *PersonRepository) OpenDB() *sql.DB {
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?charset=utf8&keepalive=1", DB_USER, DB_PASS, DB_HOST, DB_NAME))
if err != nil {
panic(err)
}
return db
}
func (r *PersonRepository) GetById(id int) *Person {
db := r.OpenDB()
defer db.Close()
row := db.QueryRow("SELECT `Id`, `UserName`, `EmailAddress`, `Password`, `IsActive`, `DateJoined`, `LastLoggedOn`, `IsAdministrator` FROM `Person` WHERE Id=?;", id)
var person Person
row.Scan(&person.Id, &person.UserName, &person.EmailAddress, &person.Password, &person.IsActive, &person.DateJoined, &person.LastLoggedOn, &person.IsAdministrator)
return &person
}

In the base service

personRepository = new(PersonRepository)
func GetPersonRepository()  *PersonRepository {
        return personRepository
}

Is there a way in Go to set the item to nil and then only create it once the function is called? or am I going about creating a service and repository layers the wrong way?




Rodrigo Moraes

unread,
Dec 18, 2012, 10:01:28 AM12/18/12
to golang-nuts
On Dec 18, 12:23 pm, Oliver Castle wrote:
> Is there a way in Go to set the item to nil and then only create it once
> the function is called? or am I going about creating a service and
> repository layers the wrong way?

Which item you want to set to nil?

I found this message very confusing (the funny format I see on Google
Groups didn't help :P).

-- rodrigo

Ollie Castle

unread,
Dec 18, 2012, 10:11:41 AM12/18/12
to golan...@googlegroups.com
The person personRepository so we check if it is nil before creating the object.
 
personRepository = new(PersonRepository)
func GetPersonRepository()  *PersonRepository {
        return personRepository
}

If I was to do it in C# it would be.

private PersonRepository personRepository;
public PersonRepository PersonRepository
{
  get 
  {
    if (personRepository == null)
    {
      personRepository = new PersonRepository();
    }
  }
}

I am just unsure how to achieve this in go 

chris dollin

unread,
Dec 18, 2012, 10:27:38 AM12/18/12
to Ollie Castle, golan...@googlegroups.com
On 18 December 2012 15:11, Ollie Castle <oliver...@tribeguru.com> wrote:
> The person personRepository so we check if it is nil before creating the
> object.
>
>>
>> personRepository = new(PersonRepository)
>> func GetPersonRepository() *PersonRepository {
>> return personRepository
>> }
>
>
> If I was to do it in C# it would be.
>
>> private PersonRepository personRepository;
>>
>> public PersonRepository PersonRepository
>> {
>> get
>> {
>> if (personRepository == null)
>> {
>> personRepository = new PersonRepository();
>> }
>> }
>> }
>
>
> I am just unsure how to achieve this in go

I'm not quite sure if this matches up to your question, but
something like:

type YourTypeName struct { pr personRepository }

func (t *YourTypeName) PersonRepository() {
if (t.pr == nil) { t.pr = MakeAPersonRepository }
return t.pr
}

Note that Go's new function is NOT like C#s; it returns a pointer
to a zero-value of the given type, not calling any implicit
constructor function (because Go doesn't have them).

Chris

--
Chris "allusive" Dollin

David DENG

unread,
Dec 18, 2012, 8:56:55 PM12/18/12
to golan...@googlegroups.com
var personRepository *PersonRepository = nil // set the item to nil
func GetPersonRepository()  *PersonRepository {
    if personRepository == nil {
        personRepository = new(PersonRepository) // and then only create it once the function is called
    } // if
    return personRepository
}

If GetPersonRepository() will be called by multi-threads, change it to this:

var once sync.Once
var personRepository *PersonRepository = nil // set the item to nil
func createPersonRepository() {
    personRepository = new(PersonRepository)
}
func GetPersonRepository()  *PersonRepository {
    if personRepository == nil {
        // and then only create it once the function is called
        once.Do(createPersonRepository)
    } // if
    return personRepository
}

Does this help?

David
Reply all
Reply to author
Forward
0 new messages