views:

24

answers:

2

I'm developing a web project with service and repository layers in ASP.NET MVC 2. I'm using Entity Framework.

I have two generated EF classes. Namely, Company and User. Company has two fields CompanyID {int} and CompanyName {string}. User has three fields UserID{int}, UserName{string}, BirthMonth{smallint,can be null} and CompanyID{int, this is a foreign key of Company}

I have a view for displaying UserName, Month and CompanyName. And also, this same view should include another model, namely Messages (MessageTitle, RecievedDate, ReceivedFrom) I would like to view BirthMonths as month names (January, February, etc.)

I'm stuck at converting short? to short and displaying months as names (ie. January, February) other than numbers.

A: 

Give this a try(I didn't test it, but found similiar in a few web searches).

string month = String.Empty;
if(BirthMonth.HasValue)
{
  DateTime date = new DateTime(1,(int)BirthMonth.Value,1);
  month = date.ToString("MMMM");
}
else
{
  // month = some default value here;
}

Edit to add: To convert short? to short specifically:

short myShort = BirthMonth.HasValue ? BirthMonth.Value : 0;
Chris L
Where to put those conventions? In repository function or in service layer? I think they should be in the service layer because they are in the business logic. But if I put them in service layer then I'll have to declare two different models, and traverse one set, make convnetions and get the intended set.
Afife Sevinç
Perhaps create an Extension method in a separate assembly, and then reference it in. http://msdn.microsoft.com/en-us/library/bb383977.aspx
Chris L
A: 

Hey,

short? to short can be done using GetValueOrDefault(); to get the non-nullable form, and to get the month name, write a quick converter to convert the number to month name... There may be something within DateTime.ToString to give this to you too.

Brian
Where to put those conventions? In repository function or in service layer? I think they should be in the service layer because they are in the business logic. But if I put them in service layer then I'll have to declare two different models, and traverse one set, make convnetions and get the intended set.
Afife Sevinç
Put them in a shared library shared across all your projects, since we are talking lower level helper methods and not enterprise wide features. You could go the extension method route, or create a static class depending on your needs. Plus these could go beyond business; they could be for your business objects but at some level too they will be helpful in the presentation layer. Some of these things actually seem more like a presentation layer type issue.
Brian