Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dataset question

Status
Not open for further replies.

dinger2121

Programmer
Sep 11, 2007
439
US
Hello,
I added a dataset class to my project in order to create a strongly typed dataset. I am getting a null value error and realize that I have to change the NullValue property of column to something other than (Throw Exception).

My question is whether I should set it to (Null) or (Empty).

Thanks for any thoughts.

carl
MCSD, MCTS:MOSS
 
i didn't think datasets allows null values. but it's been a long time since I worked with them.

also what is the data type. Empty makes me think of string, not int or Datetime. if that's the case then choosing null or empty is a choice you need to make as null is nothing and empty is a blank value. if your system interprets a null differently than an empty string you need to consider that when configuring the dataset.

if you system doesn't distinguish between null and empty string then I would use an empty string. you can still preform operations on an empty string where as a null string you need to check for null before using the string. this is an example of the Null Object pattern.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
That is what I had been thinking. they are string datatypes, and the properties allow both, so I think I will stick with the empty string.

Thanks

carl
MCSD, MCTS:MOSS
 
why mess with datasets at all? why not use POCOs to explicitly define the domain?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I am not familiar with POCO - I will look into it.

Thanks

carl
MCSD, MCTS:MOSS
 
POCO = plain old compiled/c# object. the most common example is Customer > Order > OrderLines model. here is just one way to implement that.
Code:
class Customer
{
   public long Id {get; private set;}
   public string Name  {get; set;}
   public Address ShippingAddress {get; private set;}

   public void SetId(long id)
   {
      Id = id;
   }

   public void Change(Address shippingAddress)
   {
       ShippingAddress = shippingAddress;
   }
}

class Order
{
    private IList<OrderLine> lines;
    public Order(Customer customer)
    {
       Customer = customer;
       lines = new List<OrderLine>();
    }

    public long OrderNumber {get;set;}
    public Customer Customer {get; private set;}
    public DateTime OrderPlacedOn {get;set;}
    public IEnumerable<OrderLine> Lines {get {return lines; }}

    public void Add(Product product, int quantity)
    {
        if(!lines.Any(l=>l.Product == Product)
        {
           lines.Add(new OrderLine(this, Product);
        }
        lines.First(l=>l.Product == Product).IncrementQuantityBy(quantity);
    }

public void Remove(Product product)
    {
        if(!lines.Any(l=>l.Product == Product) return;
        var line = lines.First(l=>l.Product == Product);
        lines.Remove(line);
    }
}

class OrderLine
{
    public OrderLine(Order order, Product product)
    {
       Order = order;
       Product = product;
    }

    public Order Order {get; private set;}
    public Product Product {get; private set;}
    public int Quantity {get; private set;}

    public void IncrementQuantityBy(int amount)
    {
         Quantity += amount;
    }
}
this creates a very explicity model, encapsulates logic within the entities themselves, and removes all the overhead of DataSet/DataTable operations.

frameworks like Nhibernate and ActiveRecord make it easy to map these objects to a database for persistence.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top