Closed manuelfsixtour closed 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; }
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.
What database are you using?
Hi WayneHiller, I'm using SQL Server 2008 R2.
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.
What version of Simple.Data do you have?
This is a screenshot of the record of my database.
I'm using version 0.16.2.1 of Simple.Data.
I am using 0.18.3.1, maybe a bug was fixed dealing with nulls.
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...
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.
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.
Hi everybody, given the following class:
When I try to call
Db.Contacts.All()
, everyNULL
CategoryId in the database is set toGuid.Empty
. Is it possible to have that property set tonull
?