Conditional transformer

65 views
Skip to first unread message

Ricardo Brandão

unread,
Jul 21, 2015, 9:43:29 AM7/21/15
to rav...@googlegroups.com
Hi,

I have the following transformer:

public class ResultTranslationTransformer : AbstractTransformerCreationTask<Result>
{
    public const string LANGUAGE_PARAMETER_KEY = "LanguageName";

    public InterfaceTranslationTransformer()
    {
        TransformResults = results =>
            from result in results
            let translation = LoadDocument<ResultTranslation>(result.Id + "/" + Parameter(LANGUAGE_PARAMETER_KEY))
            select new
            {
                result.Id,

                Name = translation != null ? translation.Name : result.Name,
                Language = translation != null ? translation.Language : result.Language,
            };
    }
}

However I want to conditionally apply this transformer to the results with the following condition:

if(result.Language != Parameter(LANGUAGE_PARAMETER_KEY))
{
    // apply transformer
}

Otherwise I want to return the result without performing any transformation.

Any ideas?

Thanks in advance.

P.S. I have taken a look at this discussion but it seems that no proper solution has been provided.

Michael Yarichuk

unread,
Jul 21, 2015, 10:59:14 AM7/21/15
to rav...@googlegroups.com
Can you share more info about what do you want to achieve?
In general, why not apply transformer on a query that is already filtered by your condition?

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Best regards,

 

Michael Yarichuk

RavenDB Core Team

Tel: 972-4-6227811

Fax:972-153-4-6227811

Email : michael....@hibernatingrhinos.com

 

RavenDB paving the way to "Data Made Simple" http://ravendb.net/  

Ricardo Brandão

unread,
Jul 21, 2015, 11:46:13 AM7/21/15
to rav...@googlegroups.com
Sure.

I have an application where users can create content in different languages. For that, users are able to create content in a specific language and then that content can be translated into different languages.

For that I have the following entities:

public class Result {
    public string Id {get;set;}
   
public string Name {get;set;}
   
public string Language {get;set;}
}

public class ResultTranslation {
   public string Id {get;set;}
   
public string ResultId {get;set;}
   
public string Name {get;set;}
   
public string Language {get;set;}
}

What I need/want to do is to load some content in a requested language like this:

var results = DocumentSession
                .Load<ResultTranslationTransformer, Result>(_ids, configuration =>
                    configuration.AddTransformerParameter(ResultTranslationTransformer.LANGUAGE_PARAMETER_KEY, _languageName));

in order to retrieve the results in the requested language.

Since the content can be created in different languages, the transformer needs to check if it is necessary to "translate" the result or if we are good to go.

Thanks

Chris Marisic

unread,
Jul 21, 2015, 12:41:28 PM7/21/15
to rav...@googlegroups.com
Don't do that. Databases aren't meant for executing application/business code.

Use your application to properly encode your data then persist completed documents.

Ricardo Brandão

unread,
Jul 21, 2015, 12:58:29 PM7/21/15
to rav...@googlegroups.com
My data is not business aware. However I don't want to load things I don't need/have and that is why I want to transform the results conditionally when loading the data from the database. 

Furthermore I really don't want to keep one file with all the translations (for a lot of reasons but mainly performance by issues) and that is why I have separate documents for translations.

Chris Marisic

unread,
Jul 21, 2015, 1:48:22 PM7/21/15
to rav...@googlegroups.com
UserContent/1 
{
locale: en-us
content: "foo foo foo"
}

then when you need DE-GB or whatever is a valid locale


var translated = session.Get("UserContent/1/DE-GB") 

if(translated = null)
{
 translated = Translate("DE-GB", session.Get("UserContent/1"))
translated.Id = "UserContent/1/DE-GB"
session.Store(translated)
session.SaveChanges();
}

return translated

Oren Eini (Ayende Rahien)

unread,
Jul 22, 2015, 2:10:11 AM7/22/15
to ravendb
public class ResultTranslationTransformer : AbstractTransformerCreationTask<Result>
{
    public const string LANGUAGE_PARAMETER_KEY = "LanguageName";

    public InterfaceTranslationTransformer()
    {
        TransformResults = results =>
            from result in results
            let translation = LoadDocument<ResultTranslation>(result.Id + "/" + Parameter(LANGUAGE_PARAMETER_KEY))
            select  Parameter("LangKey") != "en-US" ?  new
            {
                result.Id,

                Name = translation != null ? translation.Name : result.Name,
                Language = translation != null ? translation.Language : result.Language,
            } : result;
    }
}


Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


On Tue, Jul 21, 2015 at 4:43 PM, Ricardo Brandão <ricardo...@welisten.eu> wrote:

--

Ricardo Brandão

unread,
Jul 22, 2015, 4:03:07 AM7/22/15
to RavenDB - 2nd generation document database
Hi Chris,

We already had a solution similar to that but we were not very happy with it because the code was getting too complicated. And since this is going to be used in many places we decided to find a better solution.

Ricardo Brandão

unread,
Jul 22, 2015, 4:04:33 AM7/22/15
to RavenDB - 2nd generation document database
Hi Oren,

That's it and you kept it really simple! 

Thanks.

Ricardo Brandão

unread,
Jul 22, 2015, 4:08:09 AM7/22/15
to RavenDB - 2nd generation document database
One quick question Oren,

What's the impact of loading the translation? Because with you solution we still always load a translation.

Oren Eini (Ayende Rahien)

unread,
Jul 22, 2015, 4:49:47 AM7/22/15
to ravendb
I don't understand the question. What do you mean?

Ricardo Brandão

unread,
Jul 22, 2015, 5:15:23 AM7/22/15
to RavenDB - 2nd generation document database
If you take a look at:


 from result in results
            let translation
= LoadDocument<ResultTranslation>(result.Id + "/" + Parameter(LANGUAGE_PARAMETER_KEY))
            select  Parameter("LangKey") != "en-US" ?  new

You'll see that we will load the translation first. However, if we already have the result in the requested language, we don't need to do this load (actually, it will return null).

My question is that if this affects the performance.

Oren Eini (Ayende Rahien)

unread,
Jul 22, 2015, 5:18:13 AM7/22/15
to ravendb
Yes, probably.
You can move this into the other section, though.

Chris Marisic

unread,
Jul 23, 2015, 2:54:06 PM7/23/15
to RavenDB - 2nd generation document database, ricardo...@welisten.eu
Embedding this in the database is definitely not a better solution, it is a radically worse solution. 
Reply all
Reply to author
Forward
0 new messages