jeudi 30 octobre 2014

Should I use static or instance functions to populate my object from a database?


Vote count:

0




I know this may be purely a design preference, but from your perspective: Should my functions that retrieve items from the database be static or instance based? What is generally the most preferred method? (And most common) What are the Pros/Cons of each method?


I have an class which has 3 properties: (No constructor for the object)



  • ObjectId - string

  • Name - string

  • Count - int


Instance Based Example



public async void Get(string objectId) {
// Gets specific item from "Tag" table
ParseQuery<ParseObject> query = ParseObject.GetQuery("Tag");
ParseObject tagObject = await query.GetAsync(objectId);
this.ObjectId = tagObject.ObjectId;
this.Name = tagObject.Get<string>("name");
this.Count = tagObject.Get<int>("count");
}


Setting my object would be done like so:



Tag myTag = new Tag();
await myTag.Get("123456");
// Properties are set and ready to work with


Static Example



public static async Task<Tag> Get(string objectId) {
Tag toReturnTag = new Tag();
// Gets specific item from "Tag" table
ParseQuery<ParseObject> query = ParseObject.GetQuery("Tag");
ParseObject tagObject = await query.GetAsync(objectId);
toReturnTag.ObjectId = tagObject.ObjectId;
toReturnTag.Name = tagObject.Get<string>("name");
toReturnTag.Count = tagObject.Get<int>("count");
return toReturnTag;
}


And would be set as such:



Tag myTag = await Tag.Get("123456");


asked 34 secs ago







Should I use static or instance functions to populate my object from a database?

Aucun commentaire:

Enregistrer un commentaire