1

I have a multi-language system, and all over the place I have what I would call "wrapper properties" in every class, such as:

Public Property Name(Optional ByVal ForceLanguageCode As String = Nothing) As String
    Get
        Return Translations.GetByCode(Me.EntityID, "name", ForceLanguageCode)
    End Get
    Set(ByVal value As String)
        Translations.SetByCode(Me.EntityID, "name", value, ForceLanguageCode)
    End Set
End Property

I would then copy and paste that code for every translatable field.

I am hoping to be able to replace that with one line of code somehow, with a class. The only thing that changes each time is the name of the field, such as "name", "title", or "subject", etc. This seems a bit of a waste and I want my system to be as easy to use as possible for developers.

I did some research into attributes but I don't think that is my answer as:

1) I don't know how to automatically instantiate the objects, especially as this is extending a LINQ entity where the New() signature is already generated by the DBML. My current technique uses static functions.

2) The "EntityID" is realtime information, not compile-time.

Could anybody suggest how I could clean up this mess?

Steve McGill
  • 501
  • 2
  • 15

1 Answers1

1

Instead of strings you can use lamdas:
Retrieving Property name from lambda expression
That way you at least get compiletime checking.

You could use stackwalking to infer the calling method and thus the associated property. So you don't need to manually refer to the property at all. But I don't like that approach.

You can do aspect-orientated programming with an IL rewriter like PostSharp. Then you could replace the manual code with an attribute. PostSharp then adds your boilerplate code to properites with that attribute.

But I'm not sure if your overall design for localization is good. I've never seen it done this way. And I don't understand what the setter is for.

Community
  • 1
  • 1
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
  • Sorry for the very late accept. I would imagine that when trying to solve my problem, understanding how MVC's TryUpdateModel works would be helpful, this seems to also walk through the properties in some way and check them for attributes, etc. Will have a poke around in the MVC source. – Steve McGill Dec 01 '13 at 10:31