Modelling Question - Main Attributes and Language Specific Attributes

Modelling Question - Main Attributes and Language Specific Attributes

Old forum URL: forums.lhotka.net/forums/t/698.aspx


tsouki posted on Friday, July 21, 2006

Hi, all.

I have a class (let's say Locations) which has some main attributes like Id, ParentId, Code etc. and some language specific attributes  like Description, History, Abstract (String attributes).

My question is how should I model my class. I would like to have something like this:

myLocation.CurrentLang.Description = "asdf"
myLocation.CurrentLang.Abstract = "asdf"            for the current language loaded

and

myLocation.Lang("en").Description = "asdf"
myLocation.Lang("en").Abstract = "asdf"            for a different Language

The approach that I have in mind is to have a different class with the language  specific attributes which I get through the properties CurrentLang or Lang("en"). However what I'd really like to avoid is to allow the direct instantanation of the new class from outside my Location class (even if the new one is nested).

Any implementation ideas or another approach?




RockfordLhotka replied on Friday, July 21, 2006

Do you have a different Location per language, or are you just trying to represent a single Location in different languages?

tsouki replied on Thursday, July 27, 2006

Hi, I'm sorry for the delay but I'm on vacations and no internet access is available.

I'm trying to represent a single location in different languages. I tried the following workaround.

I have one class with the language independent attibutes of a Location and one class with language dependent attributes. In the first class I have a property that returns the second class, according to a lang argument. Here's the implementation

Public Class Location

Private _Details As Dictionary(Of String, LocationDetails)

   .
   .
   .


   
Public ReadOnly Property Details(Optional ByVal locale As String = "") As LocationDetails
      
Get
     Dim mLocale As String
      
If locale = "" Then
         
mLocale = MyLanguage.CurrentLanguage
      
Else
         
mLocale = locale
      
End If

      Dim dets As LocationDetails
      
If Me._Details.ContainsKey(mLocale) Then
         
dets = Me._Details.Item(mLocale)
      
Else
         
If LocationDetails.Exists(Me._Id, mLocale) Then
            
dets = LocationDetails.GetLocationDetails(Me._Id, mLocale)
         
Else
            
dets = LocationDetails.NewLocationDetails
            dets.LocationId =
Me._Id
            dets.Locale = mLocale
         
End If
         
Me._Details.Add(mLocale, dets)
      
End If
      
Return dets
   End Get
   End Property

   .
   .
   .

        Public Overrides Function Save() As Location
            
Dim Saved As Location = MyBase.Save

            
Dim i As Dictionary(Of String, LocationDetails).Enumerator
            i =
Me._Details.GetEnumerator()

            While i.MoveNext
               i.Current.Value.LocationId =
Me._Id
               i.Current.Value.Save()
            
End While

            Return Saved

      End Function

      .
   .
   .

End Class

In the Save method first I save the Location object, because in case of a new Location a foreign key is required by the LocationDetails class and then I save any LocationDetails object in the _Details dictionary. Both Location and LocationDetails classes inherit from BusinessBase.

What do you think?

 

Copyright (c) Marimer LLC