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 8559069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:57:15+00:00 2026-06-11T15:57:15+00:00

I have the following Controllers (in diagram) which Call the Static method to get

  • 0

I have the following Controllers (in diagram) which Call the Static method to get the data. As I am going to use the data at many other pages , I dont want to requets for the data again and again. That’s why I am checking whether data is null and then returning the value appropriately.

private static IEnumerable<MovieDetails> GetJsonData(string jsonRequestURL)
{
    try
    {
        using (var wc = new WebClient())
        {
            var json = wc.DownloadString(jsonRequestURL);
            var rootObj = JsonConvert.DeserializeObject<MoviesListRootObject>(json);

            var responseObject = rootObj.movieResponse;

            var movieDetails = responseObject.Select(movieDetail =>
                                                        new MovieDetails
                                                        {
                                                            Description = movieDetail.description,
                                                            MovieURI = movieDetail.formats.res150p,
                                                            Thumbnail = movieDetail.image,
                                                            Title = movieDetail.title,
                                                            ID = movieDetail.id
                                                        });
            return movieDetails;
        }
    }

    catch (Exception exception)
    {
        //Error Handling
    }
}

This is perfect till the time, GetJsonData is Desrializng to only One type(Currently MoviesListRootObject what If I want to ConvertTo AnotherType of Root object ex: FeaturedlistRootObject).

But how in the same method I can Desirialing to other types?
Diagram of controllers

Are generics the only option or there exist some better design/feature I can use (not sure how helpful dynamic will be)?

Again, if I am only left with generics, can I avoid writing code based on different types?

  • 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-06-11T15:57:16+00:00Added an answer on June 11, 2026 at 3:57 pm

    There seem to be two parts of your code that can change:

    1. the type of the root object (e.g. MoviesListRootObject) and the type of the result (e.g. IEnumerable<MovieDetails>)
    2. how is the result created from the root object

    Generics are the perfect solution for #1, but they won’t help you much with #2, you need some other mechanism for that. Depending on the structure of your code, the best solution for #2 may differ. I think you pretty much have two choices here:

    1. make the method generic and use a delegate to create the result
    2. make the whole type generic and create the result by calling an abstract method

    Choice 1 could look something like this:

    private static TResult GetJsonData<TRoot, TResult>(
        string jsonRequestURL, Func<TRoot, TResult> resultSelector)
    {
        using (var wc = new WebClient())
        {
            var json = wc.DownloadString(jsonRequestURL);
            var rootObj = JsonConvert.DeserializeObject<TRoot>(json);
            return resultSelector(rootObj);
        }
    }
    
    private static IEnumerable<MovieDetails> GetMovieDetails(string jsonRequestURL)
    {
        return GetJsonData<MoviesListRootObject, IEnumerable<MovieDetails>>(
            jsonRequestURL,
            rootObj =>
            {
                var responseObject = rootObj.movieResponse;
    
                var movieDetails = responseObject.Select(
                    movieDetail =>
                        new MovieDetails
                        {
                            Description = movieDetail.description,
                            MovieURI = movieDetail.formats.res150p,
                            Thumbnail = movieDetail.image,
                            Title = movieDetail.title,
                            ID = movieDetail.id
                        });
                return movieDetails;
            });
    }
    

    This way, the general code for downloading JSON data is separated from the specific code for downloading movie details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following on my base controller class which my other controllers inherit
I have the following issue: I want to create a method which accepts input
I have the following in my Rails 3 controllers/application.rb (which I'm trying to write
I have the following controller which contains a view: Lead.Controllers.UrlSearch = Ember.Object.extend init: ->
I have the following layout for my mvc project: /Controllers /Demo /Demo/DemoArea1Controller /Demo/DemoArea2Controller etc...
I currently have the following controller method in a Rails app: def index @entries
I have the following application_controller method: def current_account @current_account ||= Account.find_by_subdomain(request.subdomain) end Should I
I have following spec to test controller method: context #create do it should redirect
I have the following code in one of my controllers (in a Rails 3.1
I have the following directory structure: modules/ api/ controllers/ ApiController.php InventoryController.php OtherController.php The init()

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.