Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 985583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:11:14+00:00 2026-05-16T05:11:14+00:00

I have these two methods on a service: public Offer GetOffer(int id, string languageCode

  • 0

I have these two methods on a service:

public Offer GetOffer(int id, string languageCode = Website.LanguageSettings.DefaultLanguageCode)
        {
            Entities.Offer offerEntity = _db.Offers.FirstOrDefault(offer => offer.Id == id);

            if (languageCode.ToLower(CultureInfo.InvariantCulture) != Website.LanguageSettings.DefaultLanguageCode)
            {
                using (IDocumentSession session = store.OpenSession())
                {
                    Translation.Offer translatedOffer = session.LuceneQuery<Translation.Offer>(Website.RavenDbSettings.Indexes.Offers)
                        .Where(string.Format("ObjectId:{0} AND LanguageCode:{1}", id, languageCode))
                        .OrderByDescending(offer => offer.Id)
                        .FirstOrDefault();

                    var offerPOCO = Mapper.DynamicMap<Translation.Offer, Offer>(translatedOffer);
                    offerPOCO.Id = offerEntity.Id;

                    return offerPOCO;
                }
            }

            return Mapper.Map<Entities.Offer, Offer>(offerEntity);
        }

And

public Hotel GetHotel(int id, string languageCode = Website.LanguageSettings.DefaultLanguageCode)
        {
            Entities.Hotel hotelEntity = _db.Hotels.FirstOrDefault(hotel => hotel.Id == id);

            if (languageCode.ToLower(CultureInfo.InvariantCulture) != Website.LanguageSettings.DefaultLanguageCode)
            {
                using(IDocumentSession session = store.OpenSession())
                {
                    Translation.Hotel translatedHotel = session.LuceneQuery<Translation.Hotel>(Website.RavenDbSettings.Indexes.Hotels)
                        .Where(string.Format("ObjectId:{0} AND LanguageCode:{1}", id, languageCode))
                        .OrderByDescending(hotel => hotel.Id)
                        .FirstOrDefault();

                    Hotel hotelPOCO = Mapper.DynamicMap<Translation.Hotel, Hotel>(translatedHotel);
                    hotelPOCO.Id = hotelEntity.Id;

                    return hotelPOCO;
                }
            }

            return Mapper.Map<Entities.Hotel, Hotel>(hotelEntity);
        }

They are exactly the same in most aspects: they take the same params, build the same query and make the same operations, the only thing that varies is the type of objects they work with and output. Besides building a method to generate the Where() param string, I can’t think of any way I can merge most (or all) of this code into a single method and then just call it from the GetOffer() and GetHotel() methods since I’ll end up with several more just like these two.

Any advice is greatly appreciated.

Edit: adding the solution so if another poor soul comes across this problem he/she can have a starting point:

private TReturn GetObject<TReturn, TEntity, TTranslation>(int id, string languageCode, string ravenDbIndex) where TEntity:EntityObject 
                                                                                                                    where TTranslation:Translation.BaseTranslationObject
                                                                                                                    where TReturn:BasePOCO
        {
            // TODO Run more tests through the profiler
            var entities = _db.CreateObjectSet<TEntity>();
            var entityKey = new EntityKey(_db.DefaultContainerName + "." + entities.EntitySet.Name, "Id", id); // Sticking to the Id convention for the primary key
            TEntity entity = (TEntity)_db.GetObjectByKey(entityKey);

            if(languageCode.ToLower(CultureInfo.InvariantCulture) != Website.LanguageSettings.DefaultLanguageCode)
            {
                using(IDocumentSession session = store.OpenSession())
                {
                    TTranslation translatedObject = session.LuceneQuery<TTranslation>(ravenDbIndex)
                        .Where(string.Format("ObjectId:{0} AND LanguageCode:{1}", id, languageCode))
                        .OrderByDescending(translation => translation.Id)
                        .FirstOrDefault();

                    TReturn poco = Mapper.DynamicMap<TTranslation, TReturn>(translatedObject);
                    poco.Id = id;

                    return poco;
                }
            }

            return Mapper.Map<TEntity, TReturn>(entity);
        }

And then I just call:

GetObject<Hotel, Entities.Hotel, Translation.Hotel>(id, languageCode, Website.RavenDbSettings.Indexes.Hotels);

Whenever I need a hotel.

Thank you all for the great replies, learned quite a lot from them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:11:14+00:00Added an answer on May 16, 2026 at 5:11 am

    It looks as though you could refactor this into a generic method. Something similar to this (I’m making some assumptions about the ability to refactor some method calls, etc. But hopefully you get the idea)

    public T Get<T>(int id, string languageCode = Website.LanguageSettings.DefaultLanguageCode)
            {
                Entity<T> entity = _db<T>.FirstOrDefault(entity => entity.Id == id);
    
                if (languageCode.ToLower(CultureInfo.InvariantCulture) != Website.LanguageSettings.DefaultLanguageCode)
                {
                    using(IDocumentSession session = store.OpenSession())
                    {
                        Translation<T> translatedEntity = session.LuceneQuery<Translation<T>>(Website.RavenDbSettings.Indexes.Entities<T>)
                            .Where(string.Format("ObjectId:{0} AND LanguageCode:{1}", id, languageCode))
                            .OrderByDescending(entity=> entity.Id)
                            .FirstOrDefault();
    
                        T POCO = Mapper.DynamicMap<Translation<T>, T>(translatedEntity);
                        POCO.Id = entity.Id;
    
                        return POCO;
                    }
                }
    
                return Mapper.Map<Entities<T>, T>(Entity);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 492k
  • Answers 492k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The short answer to your question, sadly, is "No there's… May 16, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer In general both ways are in practice very close. The… May 16, 2026 at 10:21 am
  • Editorial Team
    Editorial Team added an answer It's because in http://fluroltd.com/clients/harveys/wp-content/themes/harveys/css/base.css you have: #page_case_studys { float:left; width:400px;… May 16, 2026 at 10:21 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

We have a web service that we will be hosting on a public web
I am trying to create a WCF Streaming Service. I have two requirements that
i have a usercontrol with two public properties public DateTime fromdate { get; set;
This is probably a simple one... Right now I have a working web service
Imagine you have an entity that has some relations with other entities and you
I have been banging my head of the wall for two days with this
I'm looking to make a service which I can use to make calls to
I have a Django application being served of (say) example.com . It contains a
Say I have a set (or graph) which is partitioned into groups. I am
I have built a console application that works okay when it references a .exe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.