简体   繁体   English

将字符串集放入表示层

[英]Getting String Sets into Presentation Layer

We're working on an hospital information system that is being written on C# and using NHibernate to map objects to database. 我们正在使用C#编写医院信息系统,并使用NHibernate将对象映射到数据库。 MVC pattern is being used to separate business logic from UI. MVC模式用于将业务逻辑与UI分开。 Here is the problem, 这是问题所在

How do you get variable sized different set of strings to UI? 如何获得可变大小的不同字符串集到UI?

For example a Contact object have a property named City that holds which city contact lives. 例如, Contact对象具有一个名为City的属性,该属性保存住哪个城市的联系人。 In country that the app is written for has more than 80 cities. 在为该应用程序编写的国家/地区中,有80多个城市。 How could you write those cities to a combo box? 您如何将那些城市写到组合框中? (or a data grid, tables, ...) In this example the city number is fixed. (或数据网格,表格等)。在此示例中,城市编号是固定的。 There is no need to add another city for a long time. 长时间无需添加其他城市。 (If the city list changes, recompiling is not a problem) (如果城市列表更改,则重新编译不是问题)

For example a Contact object have another property named FooBar which is going to be hold 1000 different string values and these values is going to be selected from a combo box for that property. 例如,一个Contact对象具有另一个名为FooBar属性,该属性将容纳1000个不同的字符串值,并且将从该属性的组合框中选择这些值。 And this set can be grown if users want. 如果用户需要,可以扩展此集合。 How do you load the combo box with these values? 如何用这些值加载组合框? (If the string list statically written to combo box object, recompiling is a problem) (如果字符串列表静态写入组合框对象,则重新编译是一个问题)

I have different solutions as below 我有以下不同的解决方案

  1. All string values statically written to combo box in code or designer 将所有字符串值静态写入代码或设计器中的组合框
  2. Get the values from a resource file 从资源文件获取值
  3. Write those values to an XML file (Actually same as above, but no need to recompile) 将这些值写入XML文件(实际上与上面相同,但无需重新编译)
  4. Make a City object and get the values into a list from CITY table with NHibernate 使用NHibernate创建一个City对象并将值放入CITY表的列表中
  5. Make a class named StringHolder which has a Type and Value property. 创建一个名为StringHolder的类,该类具有TypeValue属性。 All string values(including City and FooBar ) would be written in just one table named STRINGHOLDER . 所有字符串值(包括CityFooBar )将只写在一个名为STRINGHOLDER表中。 And get those values with a key like "CITY" or "FOOBAR" with NHibernate. 并使用NHibernate使用“ CITY”或“ FOOBAR”之类的键获取这些值。

Which one would you choose? 你会选哪一个? Or could you suggest me another one? 还是可以建议我另一个?

Thanks all 谢谢大家

I would vote for solution #4. 我会为解决方案4投票。 That's the way I have always done it in similar situations. 这就是我在类似情况下总是这样做的方式。 It just seems like a cleaner solution. 这似乎是一个更清洁的解决方案。

If the locations are actually going to be used for anything, get them into the database. 如果这些位置实际上将用于任何用途,请将其放入数据库中。 If the data "isn't really used", but cities lookup is provided to make the user interface better, then the XML file option is not a bad way to go either. 如果数据“并没有真正使用”,但是提供了城市查询以改善用户界面,那么XML文件选项也不是坏方法。

By used, I mean stuff like list all emplyees in New York and stuff like that. 通过使用,我的意思是像列出纽约的所有员工之类的东西。 If it's "dead data", just to be displayed, go for the solutions that will require the least amount of work and least risk - which might be the file option. 如果只是显示“失效数据”,请选择需要最少工作量和最少风险的解决方案-这可能是文件选项。

How do you feel of using List<string> for a list of City? 您如何将List <string>用于城市列表? Load this list of strings in your DAL or BL and then pass it on to UI. 将此字符串列表加载到DAL或BL中,然后将其传递给UI。

Same solution should be good for FooBar values too. 同样的解决方案也应适用于FooBar值。

In case you have IDs associated with City or FooBar, say NY and its numeric ID in DB is 1, then you can use KeyValuePair<TKey, TValue>. 如果您具有与City或FooBar关联的ID,例如NY,并且其在数据库中的数字ID为1,则可以使用KeyValuePair <TKey,TValue>。 With generics you can dictate what data goes in this KeyValuePair. 使用泛型,您可以决定此KeyValuePair中将包含哪些数据。 City name or FooBar's string value can be key and numeric ID can be value. 城市名称或FooBar的字符串值可以是键,数字ID可以是值。

Just 2 cents. 只需2美分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM