0

I've start wondering what's the best practice when handling objects in web development.

Oversimplified the process when there is a page request often looks like:

  1. Page request.
  2. Fetch object data from database.
  3. Create object.
  4. Return object to page.

And on POST or save:

  1. Get data from page.
  2. (Create object) could be done in 1.
  3. Save object data to database.

I've written a sample code below:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }

    public bool SaveNew()
    {
        SaveToDatabase(this.Id, this.Name, this.Category);
    }
    public void SetElementsById()
    {
         var datarows = GetProductDataByIdFromDB(this.Id);
         Name = datarows[1].ToString();
         Category = datarows[2].ToString();
    }
}

public class ProductHandler
{
    public bool SaveNew(Product product)
    {
        SaveToDatabase(product.Id, product.Name, product.Category);
    }
    public void GetProductById(int id)
    {
        Product product = new Product();
        product.Id = id;

        var datarows = GetProductDataByIdFromDB(id);
        product.Name = datarows[1].ToString();
        product.Category = datarows[2].ToString();
    }
} 

What is the best practice, to handle the object data within the object or in a handler type of class?

1

I think that you've stumbled across well know issue of repository (what you've named 'handler') vs active object.

Some say that repository pattern is better because it promotes single responsibility principle while active object bundles together data model with data persistence logic. Just to make it more complicated, some solutions actually use repository pattern inside active object to allow testability.

On the other hand active objects seems simpler to manage and some would argue that the repositories break layer separation between business object and data access layer.

Personally, I prefer repository pattern in C#, main reasons being: 1. Having 'dumb' objects allows in many cases to save on number of classes, because I can use same classes for DTO, database entities and sometimes client side view models (although sparsely). I don't like having several versions of basically same class just to represten same data but within different context and having to make copies. 2. C# extension methods allow to add any logic to any object without polluting it with the code. 3. Using active objects, in most of the cases I've came upon, does not actually really hide the data layer - consider transactions, some times you just want to commit a data change instantly, some times you want to make several updates on a single transaction. With repositories it's explicit, with active objects, hmmm in my case I've just added commits on the context, which pretty much kills the whole idea of hiding persistence layer within objects.

So that's just my 5c, there is tons of material on the net about it, google repository vs active record.

  • Ok, thanks for the response. So there is no "best practice" between handling object data "within or outside the object", only more what you prefer? – AsusT9 Oct 12 '16 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.