Data transfer objects in C#

N

Navaneeth.K.N

Hi

I am learning DTO and wrote a dto for my "Product" class. I want to know
whether I am following the right method ? Here is my code

class Product
{
internal Product(ProductItem item)
{
this.Name = item.Name;
}

public string Name
{
get;set;
}

public static Product FromId(int productId)
{
ProductDAL data = new ProductDAL();
ProductItem item = data.GetProductFromId(productId);
return ProductFactory.CreateProduct(item);
}

public void Save()
{
ProductDAL data = new ProductDAL();
ProductItem item = new ProductItem(this.Name);
data.SaveDetails(item);
}

}

static class ProductFactory
{
public static CreateProduct(ProductItem item)
{
Product product = new Product(item);
return product;
}
}

// DTO class
class ProductItem
{
public ProductItem(string name)
{
this.Name = name;
}

public string Name
{
get;set;
}
}

Do you write any methods in the DTO class ? Or only some getters and
setters ?

Thanks
 
J

jj

Hello Navaneet, I'll put in my 2 cents..

DTOs are strictly used for TRANSFERRING data. Think of it as a data
structure. The code for fetching or saving data should not be in a
DTO. It should be in DAL objects, and invoked from BLL objects.

Regards,
Jim
 
N

Navaneeth.K.N

Hi Jim,

Thanks for replying me. I don't have Save functionality in DTO class.
"ProductItem" is the DTO class which contains only the data structure. So is
this correct approach ?
 
J

jj

Hello Navaneet,

Your ProductItem class is a DTO. there is nothing wrong with it. Just
remember to mark your DTO classes as serializable if you want to send
them using web services or remoting. I know you know about this, but
just a reminder.

Regards,
Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top