-2

I have to load an organization data inside a table and then replace it's data inside a hundred string text that contains tags like this: <# PI_Name #>
what is the best method to take the data one time from table and then replace related data into string tags?

Thank you

Shadman
  • 131
  • 1
  • 10

2 Answers2

0

EDIT:

After Rereading your question a few times i think i may have misunderstood what your asking, your not asking how to store data so that it can be acessed in multiple places your asking how to replace tags in a string with data

in which case i would suggest building a tag dictionary

Dictionary<string,string> Tags;
string mask;
string output = mask;
foreach(var tag in tags)
{
    output = output.Replace(tag.Key,tag.Value)
}

if you don;t want to prebuild your tag dictionary you can user RegEx and Reflection to do it the hard way using Regex to spot the tags in the string then reflection to look up properties the same as the tag

see RegEx and Get property value from string using reflection in C#

Note you could also use MS's string.Format function but then you would have to follow the MS tag rules which are positional not named and user curly braces ie string.Format("({0},{1})", 50, 20) would return "(50,20)"


The best mothod would be to create an object model that supports your data needs, once you have you can then tailor your access to it correctly

MS's MVVM pattern give a good overview on the concept

but in overview it should work similar to this, create a data model, that stores the data in a sensible manor so that related items are co-located, then have these classes manage the data, ie translate them to and from the database and parsing any complex data such as the string you mention (you might user EntityFamework or some other code generator for this layer)

then have your business layer that manages the data, thos layer deals with moths, validation, Undos, and changing the data, the business layer doesn't need to be 1:1 with the data layer you have have business models that manage several objects or only a portion

finally on top of this you have your presentation layer that manages what the user sees and how the user interacts with the business layer

example SUDO

Database Table {FirstName, Surname, DOB}

DataModel
{
    String firstname;
    String Surname;
    dateTime DOB;
    static DataModel Load(string name){}//read from DB
    static DataModel Load(dateTime dob){}//read from DB
    void Save(){}//write to DB
}

BusinessModel
{
    DataModel orginalData
    String FullName;
    int Age;
    bool IsValid;//validate changes ie that age as less than 100 
    void Reset(){}//undo changes and reset data to orginalData
    string ValidateText(){}//Text explaining why is valid is false
    void Save(){}//copy changes to DataModel and ask it to save to DB
    //Events to inform GUI of data changes that need refreshing
}
Community
  • 1
  • 1
MikeT
  • 5,398
  • 3
  • 27
  • 43
  • that will be a good method but unfortunately i should use a predetermined programming method that our company follows it and MVVM does not allowed. Thank you. – Shadman Jul 26 '16 at 11:26
  • If your company doesn't allow layered data models then they should stop using .Net as everything about .net is layered, MVVM is just MS's latest buzzword for a process thats been around for decades, i originally learned it as GOD GUI ObjectModel DataModel, the acronym isn't important its just the division of labour into appropriate classes that you need – MikeT Jul 26 '16 at 11:41
0

write datatable property for your organization table. There you can set the value of your table and get the values alter the columns as well.