2011-05-06 57 views
1

我的想法是在数据库中存储文本string.Format()占位符{0} ... {n}。占位符值的类型存储在另一个数据库字段中,以逗号分隔的格式(CSV)调用“参数”。如何初始化一个字符串(类名)的对象为string.Format参数对象数组?

在数据库中的文字的一个例子:

发信人:{0}
地址:{1}

接收机:{2}
地址:{3}

和 “参数” 字段包含:

Company.FullName, Company.Address.FullAddress, Customer.FullName, Customer.Address.FullAddress

我希望这是显而易见的是, “公司”, “客户” 和“ 地址“代表类别。 FullName and FullAddress reperesent properties that returns a string。 “公司”和“客户对象能够加载正确地址应要求。

我正在考虑使用 “params对象[] PARAMS” 与的String.format()传递的值。

现在的问题是如何加载正确的对象,并将匹配计数的参数传递给string.Format方法?

要求

  1. 在数据库中必须是可编辑的文本;
  2. 占位符的数量可能会改变(添加或删除);
  3. 每个占位符在“参数”数据库字段中都有相应的参数;
  4. 每个参数代表一个类别名称及其属性。可以调用子类,但它们必须用点(。)分隔。例如:Company.Address.FullAddress;
  5. 只需要对象(s)特定于数据库中的文本应该被创建或加载。加载一堆对象以防万一,这不是一个好主意;
  6. 如果只有一种方法可以处理任务,那将会很棒;
  7. 如果有任何合适的图案或图案,请使用任何图案。也许复合,工厂建造者模式也许?

作为一种解决方案我看到:

  1. 硬代码的对象(一个或多个)负载为每个数据库记录。这就要求带有占位符的文本不能改变。所以这个解决方案不适合我。
  2. 使用每个参数的反射加载对象。这将是一个很好的动态方法,但如何使用客户地址信息加载正确的“地址”对象呢?客户对象通过其CustomerId创建/加载。

到目前为止,我这么远:

public string FillTemplate(int languageId, long templateTypeId, params object[] parameters) 
    { 
     string formatStringFromDb = LoadTemplateContentFromDb(languageId, templateTypeId); 

     return string.Format(formatStringFromDb, parameters); 
    } 

    public object[] LoadTemplateParameter(int languageId, long templateTypeId) 
    { 
     string csvTemplateParameters = LoadTemplateParametersFromDb(languageId, templateTypeId); 
     string[] stringParameters = csvTemplateParameters.Split(','); 
     object[] templateParametersToReturn = new object[stringParameters.Length]; 

     for (int i = 0; i < stringParameters.Length; i++) 
     { 
      // Split class name(s) and property 
      string[] classAndPropertyNames = stringParameters[i].Split('.'); 

      // Initialise and add the object to the parameter object array 
      templateParametersToReturn[i] = InitialiseParameterObject(classAndPropertyNames); 
     } 

     return templateParametersToReturn; 
    } 

    private static object InitialiseParameterObject(string[] classAndPropertyNames) 
    { 
     // Now what? How do I create new object(s) from a string? 
     // To create a Customer or Company class an appropriate ID is needed. 
     // How do I know which ID (Customer or Company) 
     // do I need to provide in advance? 
     throw new NotImplementedException(); 
    } 

谢谢你,如果能读到这里。更好的是,如果你对我有一些想法:)

PS。目前LoadTemplateContentFromDbLoadTemplateParametersFromDb对数据库进行了两次单独的调用,但我会重构它,以便一次询问整个模板信息。

+0

对我而言,以下内容并不清楚:您是否只有一个Address地址实例,并且想从中创建Customer和公司实例,或者您是否已经有Customer和Company实例? – 2011-05-06 07:33:37

+0

基本上,客户和公司确实存在(或可通过ID加载),并且每个都有自己的地址实例。 – 2011-05-06 08:06:09

回答

0

一旦有了对象,并为它的属性的字符串,这些方法我写别的东西可以帮助:

/// <summary> 
/// Gets an object property's value, recursively traversing it's properties if needed. 
/// </summary> 
/// <param name="FrameObject">The object.</param> 
/// <param name="PropertyString">The object property string. 
/// Can be the property of a property. e.g. Position.X</param> 
/// <returns>The value of this object's property.</returns> 
private object GetObjectPropertyValue(Object FrameObject, string PropertyString) 
{ 
    object Result = FrameObject; 

    string[] Properties = PropertyString.Split('.'); 

    foreach (var Property in Properties) 
    { 
     Result = Result.GetType().GetProperty(Property).GetValue(Result, null); 
    } 

    return Result; 
} 

public Type GetObjectPropertyType(string ObjectName, string PropertyString) 
{ 
    Object Result = GetObjectByName(ObjectName); 

    string[] Properties = PropertyString.Split('.'); 

    foreach (var Property in Properties) 
    { 
     Result = Result.GetType().GetProperty(Property).GetValue(Result, null); 
    } 

    return Result.GetType(); 
} 

为了得到它变成string.Format(),你可以使用上面的方法从一个数组去属性字符串,属性本身。然后你会通过这个作为最后的参数os string.Format()