markrendle / Simple.Data

A light-weight, dynamic data access component for C# 4.0
MIT License
1.33k stars 303 forks source link

Nullable<Guid> into Guid.Empty #348

Closed manuelfsixtour closed 10 years ago

manuelfsixtour commented 10 years ago

Hi everybody, given the following class:

public class Contact
{
    public Contact()
    {
        ContactId = Guid.NewGuid();
    }
    public Guid ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Guid? CategoryId { get; set; }
}

When I try to call Db.Contacts.All(), every NULL CategoryId in the database is set to Guid.Empty. Is it possible to have that property set to null?

skironDotNet commented 10 years ago

I'm not participating into this project, but per null object pattern is better to have a value Guid.Empty than null, you can always compare if your property is default value / Guid.Empty and roll BL accordingly . Of course you may have reasons to have it null then I would advise to handle it yourself like this

    private Guid? contactId;  //  use underscore _contactId if you like it :)
    public Guid? ContactId
    {
        get
        {
            if (contactId == Guid.Empty)
                return null;

            return contactId;
        }
        set
        {
            contactId = value;
        }
    }

maybe (because i don't know Simple.Data 100%) if you just mark your property as nullable it will give you null if DB has null:

public Guid? ContactId { get; set; }

manuelfsixtour commented 10 years ago

Hi skironDotNet, the issue is not with ContactId property but with CategoryId property, which is actually nullable and returned by Simple.Data as Guid.Empty.

However, I think I'll end up following a solution like the one you posted, though it is not my first choice.

WayneHiller commented 10 years ago

What database are you using?

manuelfsixtour commented 10 years ago

Hi WayneHiller, I'm using SQL Server 2008 R2.

WayneHiller commented 10 years ago

That is very strange, I have been using SQL Server with Simple.Data for a long time and I have never had any problem with null values. Are you very sure the table actually contains nulls in the CategoryId field for the row(s) you are fetching.

WayneHiller commented 10 years ago

What version of Simple.Data do you have?

manuelfsixtour commented 10 years ago

This is a screenshot of the record of my database.

image

I'm using version 0.16.2.1 of Simple.Data.

WayneHiller commented 10 years ago

I am using 0.18.3.1, maybe a bug was fixed dealing with nulls.

skironDotNet commented 10 years ago

Lol, I'm really reading catching keywords, once I saw Guid CategoryId, I didn't read the rest, yet still my solution apply. I can add, and I'm assuming only, that at the time when Simple.Data was designed there were either no nullable types (old .NET) and the author was returning default value of the type (structures can't be null) or Simple.Data would take Guid.Empy and put null into DB if a column is marked null.

The other thought, there is plenty of articles about using int vs guid for IDs, and due to Guid fragmentation, I would use bigint for my column IDs, either with autoincrement or implement thread safe counter and generate IDs in BL (which I have done in the past).

At the and, it's your design, do what you think is best. Since Simple.Data is open source you could also locate the code responsible for that, and adjust to give null if nullable propery Guid is found...

WayneHiller commented 10 years ago

I believe Simple.Data has supported Nullable types from the beginning. Something strange is going on because like I said I have never had a problem with nulls. I also use Guid's for keys as I am doing Replication.

manuelfsixtour commented 10 years ago

skironDotNet, you're right about int vs guid for IDs, however it's a constraint I cannot remove. Said that, I'll try to update my Simple.Data version and try again. Since then, your solution apply perfectly! Thanks.