3 Replies - 95 Views - Last Post: 12 July 2019 - 07:53 AM Rate Topic: -----

#1 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Map value in file to a C# model

Posted 12 July 2019 - 07:17 AM

I am working on my call center platform and the task at hand is for it to manage the script that populates on the agents screen. The script will have to populate all of the lead information as well as product and agent information, so it will have to load that data from either the query string or the db on the server side.

I have an idea of how to complete it, but i'm worried about the performance and how much time it would take to insert any of the values into the script. Its important for the script to load on the agents screen as fast as possible.

My idea

example script
Hi {Lead.FirstName}, my name is {Agent.FirstName} and im....



example model
public class ScriptModel{
    public LeadModel Lead{get;set;} 
    public EmployeeModel Agent{get;set;} 
}



I thinking i could do the following
1) take the "ScriptModel" and convert it to a JObject (using NewtonSoft.Json),
2) then search the script for the "{...}"
3) string.Split('.') the value inside of "{...}"
4) Loop through the JObject until the final split value
5) insert the final value into the script

My questions are,
1) what is the best (performance wise) way to search the script file for "{...}" and retrieve the value inside
2) Is there a more "Native" way to complete this task?

Is This A Good Question/Topic? 0
  • +

Replies To: Map value in file to a C# model

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,396
  • Joined: 12-June 08

Re: Map value in file to a C# model

Posted 12 July 2019 - 07:27 AM

Have you considered just straight up string interpolation? Would be faster than search/replace.

https://docs.microso...ns/interpolated
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6962
  • View blog
  • Posts: 23,666
  • Joined: 05-May 12

Re: Map value in file to a C# model

Posted 12 July 2019 - 07:52 AM

I'm assuming that the text is dynamic rather than hard coded, so I think our OP is looking for something mustache-like.

About 5 years ago, before I'd even heard of mustache, I'd implemented my own text templating using the .NET Framework regular expressions, and a Dictionary<> to determine the replacement values. I couldn't hard code the text templates because we wanted the business users to be self-sufficient where they can create and edit their own forms without having to come back to us for a code change.
Was This Post Helpful? 1
  • +
  • -

#4 UG Cyber   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 621
  • Joined: 24-July 08

Re: Map value in file to a C# model

Posted 12 July 2019 - 07:53 AM

@modi123_1, I will do some more research into that and see how it behaves. I didn't think you could inject params when loading the string from a file using interpolation.

@Skydiver, Exactly! that looks like it would work out just how i want. Thank you =)

This post has been edited by UG Cyber: 12 July 2019 - 07:56 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1