471,348 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Data transfer objects in C#

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
Jun 27 '08 #1
3 4350
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
Jun 27 '08 #2
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 ?

"jj" wrote:
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
Jun 27 '08 #3
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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ads | last post: by
12 posts views Thread by joaotsetsemoita | last post: by
reply views Thread by Jean-Paul Calderone | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.