2017-06-02 31 views
0

假设我们有一个数据对象Patient,并且我们将一个List<Patient>绑定到UI小部件。加密类和列表<T>

public class Patient 
{ 
    public Patient(){} 
    public string Name {get;set;} 
    public string MedicalNotes {get;set;} 
} 

我们要加密MedicalNotes属性的内容写入记录到数据库之前(假设我们是一家小公司,买不起,它提供了透明加密功能的SQL数据库的许可费)和解密该列的内容,然后将其绑定到UI小部件。

难道我们实例化一个单加密类和饲料的引用,它的Patient构造,使列表中的每个病人对象可以调用加密对象的方法?

抑或是加密的实例留在Patient对象之外,数据库IO类和List<Patient>

+1

IMO,'Patient'类不应该负责加密本身或将自己保存到数据库。它应该代表*患者*。 – Amy

+0

@Amy:我倾向于同意你的看法。 – Tim

+0

如何将模型存储在数据库中?一些ORM?普通的旧SQL? XML序列化? – dymanoid

回答

1

如果我是你,我会将它分成3层,你的模型层不关心加密过程,数据库也不关心。这些责任都将要采取的另一方:

模型层:

public class Patient 
{ 
    public string Name { get; set; } 
    public string MedicalNotes { get; set; } 
} 

数据库层:

public static class PatientDb 
{ 
    public static void SavePatient(Patient patient) 
    { 
     //whatever happens here, you didn't post this 
    } 
} 

中间层:

public class PatientHelpers 
{ 
    public void SavePatient(Patient unencryptedPatient) 
    { 
     var encrypted = Crypto.EncryptPatient(unencryptedPatient); 
     PatientDb.SavePatient(encrypted); 
    } 
} 

public static class Crypto 
{ 
    public Patient EncryptPatient(Patient patient) 
    { 
     //whatever happens here, you didn't post this 
     return patient; 
    } 
} 
+0

谢谢。我喜欢这种关注的分离。当SelectCommand返回一个DataTable,并且每行都映射到一个Patient对象时,任何加密列在被分配给相应的Patient属性之前都会被解密。 – Tim

1

你说你的数据库层需要这种增加安全性之间进行调解。所以我想它属于这一层。保存前加密,加载后解密。