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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:05:54+00:00 2026-05-28T22:05:54+00:00

I created a class that’s called UserSessionModel; in it I’m storing some data about

  • 0

I created a class that’s called UserSessionModel; in it I’m storing some data about the user and in particular I’m storing several json strings that are the results of queries and serializations.

I have several methods and properties in UserSessionModel that overall look like this:

public string SomeUserDataInJson1 { get; set; }
public string SomeUserDataInJson2 { get; set; }
.... 3 more properties like this
public int UserID { get; set; }

private void GetSomeUserDataInJson1
{
   ObjectData1 TheObjectData1 = new ObjectData1();
   UserQueries TheUserQueries = new UserQueries();
   JavascriptSerializer TheSerializer = new JavascriptSerializer();

   TheObjectData1 = TheUserQueries.GetData1(TheUserID);

   this.SomeUserData1InJson = TheSerializer.Serialize(TheObjectData1);
}

This code is repeated 5 times, with the only change being the ObjectData, the name of the query and the property SomeUserData that’s getting set.

Is there a way to make this “better” with an interface or some other c# tools?

Thanks.

  • 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-28T22:05:55+00:00Added an answer on May 28, 2026 at 10:05 pm

    Ok, lets assume the following regarding to your example: You’re having data for processing with queries defined differently per user (userId).

    Our data container class… very simple here, contains only a string.

    public class Data
    {
        public string Content { get; set; }
    }
    

    Next step, lets have a look at the query… could be using that interface (could use events, for the response but lets keep it simple here).

    public interface IQuery
    {
        Data Process(Data data);
    }
    

    You could have a relation to the user by adding the userId to the IQuery interface but I would prefer to have another interface to solve that:

    public interface IUserQueryProvider
    {
        IEnumerable<IQuery> GetQuerysForUser(uint id);
    }
    

    This way you can alter your user to query resolving in a seperate place.

    You’ll have a serializer/converter, too. Ok, lets make an Interface here for serialization of (processed) data.

    public interface ISerializer
    {
        string Serialize(Data data);
    }
    

    Now, lets have a look at implementations, first of all the serializer… doesn’t do anything magical here and you should fill in the things you need for serialization of objects (JSON, …)

    public class JavascriptSerializer : ISerializer
    {
        public string Serialize(Data data)
        {
            return data.Content; //whatever you want do instead for serialization
        }
    }
    

    Now let us go to our Queries. I assume you’re not very familiar with design patterns and you’re meaning something like a Command Pattern instead (for processing jobs, see my link in the comments for more info about design pattern). 3 implementations follows as samples:

    public class ReplaceQuery : IQuery
    {
        private readonly string match;
        private readonly string text;
    
        public ReplaceQuery(string match, string text)
        {
            this.match = match;
            this.text = text;
        }
    
        public Data Process(Data data)
        {
            return data.Content.Contains(match) ? new Data {Content = data.Content.Replace(match, text)} : null;
        }
    }
    
    public class GreetingToQuery : IQuery
    {
        private readonly string greeting;
        private readonly string place;
    
        public GreetingToQuery(string greeting, string place)
        {
            this.greeting = greeting;
            this.place = place;
        }
    
        public Data Process(Data data)
        {
            return data.Content.Contains(greeting) ? new Data {Content = data.Content + place + "."} : null;
        }
    }
    
    public class LineEndingQuery : IQuery
    {
        public Data Process(Data data)
        {
            return data.Content.LastIndexOf(".", StringComparison.Ordinal) == data.Content.Length - 1 &&
                   data.Content.Length > 0
                       ? new Data {Content = "\n"}
                       : null;
        }
    }
    

    If we want to resolve which querys belongs to a user we need our IUserQueryProvider implementation. It is nothing more than a dictionary in this case (but could be easyly switched to other implementations).

    public class SampleQueryProvider : Dictionary<uint, IEnumerable<IQuery>>, IUserQueryProvider
    {
        public IEnumerable<IQuery> GetQuerysForUser(uint id)
        {
            IEnumerable<IQuery> queries;
            TryGetValue(id, out queries);
            return queries;
        }
    }
    

    Last but not least… the glue for everything. I added another Interface here for our “generator engine”.

    public interface IScriptGenerator
    {
        event Action<string> Script;
    
        void Generate(Data data, IEnumerable<IQuery> queries);
    }
    

    To make it more flexible I made the interface/implementation following a design principle introduced by Ralf Westphal called Event Based Components (EBC). Google is your friend if you are interested in this topic.

    public class SampleScriptGenerator : IScriptGenerator
    {
        private readonly ISerializer serializer;
    
        public event Action<string> Script;
    
        public SampleScriptGenerator(ISerializer serializer)
        {
            this.serializer = serializer;
        }
    
        public void Generate(Data data, IEnumerable<IQuery> queries)
        {
            foreach (string serialized in from query in queries select query.Process(data) into result where result != null select serializer.Serialize(result))
            {
                OnSerialize(serialized);
            }
        }
    
        private void OnSerialize(string serialized)
        {
            var handler = Script;
            if (handler != null) handler(serialized);
        }
    }
    

    And now lets put it all together and let us fly:

        static void Main()
        {
            var generator = new SampleScriptGenerator(new JavascriptSerializer());
            generator.Script += Console.Write; // bind to console output here
    
            var queryProvider = new SampleQueryProvider
                                    {
                                        {
                                            1, // user with id 1
                                            new List<IQuery>
                                                {
                                                    new ReplaceQuery("<name>", "frenchie"),
                                                    new GreetingToQuery("bonjour", "the universe"),
                                                    new LineEndingQuery()
                                                }
                                            },
                                        {
                                            2, // user with id 2
                                            new List<IQuery>
                                                {
                                                    new ReplaceQuery("<name>", "stegi"),
                                                    new GreetingToQuery("hello", "the world"),
                                                    new LineEndingQuery()
                                                }
                                            }
                                    };
    
    
            var data1 = new Data {Content = "My name is <name>."};
            var data2 = new Data {Content = "I say hello to "};
            var data3 = new Data {Content = "I say bonjour to "};
            var data4 = new Data {Content = "."};
    
    
            // you cold combine data and user query execution into lists and loops, too
            generator.Generate(data1, queryProvider.GetQuerysForUser(1));
            generator.Generate(data2, queryProvider.GetQuerysForUser(1));
            generator.Generate(data3, queryProvider.GetQuerysForUser(1));
            generator.Generate(data4, queryProvider.GetQuerysForUser(1));
            generator.Generate(data1, queryProvider.GetQuerysForUser(2));
            generator.Generate(data2, queryProvider.GetQuerysForUser(2));
            generator.Generate(data3, queryProvider.GetQuerysForUser(2));
            generator.Generate(data4, queryProvider.GetQuerysForUser(2));
    
            Console.ReadKey();
        }
    }
    

    You should see something like:

    My name is frenchie.
    I say bonjour to the universe.
    My name is stegi.
    I say hello to the world.
    

    As your homework… try to add your own query implementation and data to process. How would you add recursion here? 😉

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

Sidebar

Related Questions

I have created a class that parses some document from file. class Parser {
I have some class that I created visually in Flash Professional CS5 by transferring
I created a class that wraps a UITextView and adds some ui elements. I
in my C#-Silverlight-3-Application, I have created a class, that is doing some calculations that
I've created a class that uses a static NSSet to define some elements shared
the problem is this... I created a class that extends UIBezierClass called PathExtended, in
Im doing some practices on XNA, and i created a class that represents a
I've created class that takes Exception type in constructor private readonly Exception _exception; public
I created a class that's something like this: public class MovieTheaterList { public DateTime
I created a class that handles serial port asynchronously. I use it to communicate

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.