目前,我们的系统使用类似于Java Entity Beans的东西,尽管它是用C ++ / SQL编写的。本质上,有些类(或多或少)符号化表,这些类的实例等同于表行。我想补充一点,这种方法一开始就有缺陷,请看这篇着名的文章:http://blogs.tedneward.com/2006/06/26/The+Vietnam+Of+Computer+Science.aspx 另一方面,它运作良好,只要你接受它会导致一些不纯的东西,有时甚至是一些黑客。
然而,实际问题如下:虽然这些实体中的许多实体在内存占用量相对较少(包含整数,浮点数和字符串的十几列)并且导致良好的性能,但其中一些实际上并非如此。
扭曲:这些“胖”对象经常在没有完整数据的情况下使用。想象一下,您有一个“Passport”类,其中包含生物识别数据,家庭关系树,以及名称和出生日期。如果要显示护照列表,则只需要基本数据。
我目前正在做的是创建一个Passport实例,但是分两步填充它。第一步只添加简单字段,但保持重字段打开(为NULL)。稍后可以将该实例传递给将添加所有困难字段的函数。只要我没有犯错并使用“浅”实例,我需要“完整”版本,这样可以毫无障碍地工作。当然,我可以添加各种内部检查,但不仅规模很小(为每个实体重新实现它们,或多或少),它也非常容易出错。
我的问题是:我想在编译时区分这两个版本,而不仅仅是在运行时。这样,我会在它们发生之前发现大多数错误。
似乎有效的唯一想法是将两个部分分成两个实体,然后将它们作为元组传递。如果缺少第二个元组,显然胖数据尚未加载。虽然这有效,但它会导致卑鄙的语法:
std::vector< EntityTuple<EmptyPassport, FullPassport>>
我所获得的所有类型安全都是以可读性为代价的,这并不是一个很大的改进。目前,我没有更好的想法,并怀疑这在C ++中实际上是不可能的,但我可能是错的。非C ++ - 建议也是受欢迎的,未来可能有更好的方法来做到这一点。当然,如果有人能够说明为什么这是不可能的,我也会接受。
答案 0 :(得分:1)
<强>概述强>
让我提出一些处理“重”属性的想法,如blob,图像,文件。请记住,没有“一个解决方案”。我个人拒绝“加载所有重型属性”标志的想法,&amp;建议其他想法。
之前,我继续,请忽略小的语法或逻辑错误,并专注于代码示例的逻辑。
[1]定义示例
首先,让我们从一个简单的例子开始:
public class EmployeeClass
{
public:
int Key;
char FirstName[150];
char LastName[150];
Image* Photo; // <- picture
Blob* Contract; // <- scanned contract
}; // class
首先,您不必加载或不加载“重”属性,因为“实体”模型或其他编程技术会这样说。
实际上,我不会为重物添加标志,因为这意味着加载所有“重”属性或者根本不加载任何“重”属性。而且,有时您可能需要加载其中一些,但不是全部。
[2]加载属性
通常,程序的逻辑表示何时加载属性。
通常的做法是为每种情况使用不同的构造函数:
public class EmployeeClass
{
public:
int Key;
char FirstName[150];
char LastName[150];
Image* Photo;
Blob* Contract;
public:
// --> generic constructor
EmployeeClass()
{
Key = 0;
strcpy(FirstName, "");
strcpy(LastName, "");
Photo = null;
Contract = null;
} // EmployeeClass()
// --> "light" constructor
EmployeeClass
(
int AKey,
char* AFirstName,
char* ALastName
)
{
Key = AKey;
strcpy(FirstName, AFirstName;
strcpy(LastName, ALastName);
Photo = null;
Contract = null;
} // EmployeeClass()
// --> "heavy" constructor
EmployeeClass
(
int AKey,
char* AFirstName,
char* ALastName,
Image* APhoto,
Blob* AContract
)
{
Key = AKey;
strcpy(FirstName, AFirstName;
strcpy(LastName, ALastName);
Photo = APhoto;
Contract = AContract;
} // EmployeeClass()
void Insert();
}; // class
void Test()
{
...
int AKey = 0;
char AFirstName[150];
char ALastName[150];
Image* APhoto = null;
Blob* AContract = null;
// --> calling "light" constructor
AKey = 1;
strcpy(AFirstName, "Mary");
strcpy(ALastName, "Thompson");
EmployeeClass* AEmployee = new EmployeeClass
(AKey, AFirstName, ALastName);
AEmployee->Insert();
// --> calling "heavy" constructor
AKey = 2;
strcpy(AFirstName, "John");
strcpy(ALastName, "Doe");
Image* APhoto = LoadPhoto();
Blob* AContract = LoadContract();
EmployeeClass* AEmployee = new EmployeeClass
(AKey, AFirstName, ALastName, APhoto, AContract);
AEmployee->Insert();
// --> calling "dummy" constructor,
// --> more work, but, more flexible
AKey = 1;
strcpy(AFirstName, "Mary");
strcpy(ALastName, "Thompson");
EmployeeClass* AEmployee = new EmployeeClass();
AEmployee->Key = AKey;
strcpy(AEmployee->FirstName, AFirstName);
strcpy(AEmployee->LastName, ALastName);
AEmployee->Photo = LoadPhoto();
AEmployee->Contract = LoadContract();
AEmployee->Insert();
...
} // void Test()
许多开发人员只使用“普通光构造器”,&amp;拒绝拥有几个构造函数的想法。
[3]其他帮助
让我们暂时跳过“重”属性,稍后会继续。
这是许多C / C ++开发人员不喜欢的建议,但是,我个人觉得在处理实体对象时非常有用。我使用“两步初始化”。
对于每个实体类,我声明一个构造函数,没有用于清除字段的参数, 并且,添加一个具有非常特定标识符的虚方法,该方法承担构造函数的角色。
然后,我可以添加几个用作构造函数的虚方法,比如决定是否加载“重”属性。
所以,前面的例子就是这样的:
public class EmployeeClass
{
public:
bool F_EmployeeClass_IsReady;
public:
int Key;
char FirstName[150];
char LastName[150];
Image* Photo;
Blob* Contract;
public:
// --> only generic constructor
Employee()
{
F_EmployeeClass_IsReady = false;
Key = 0;
strcpy(FirstName, "");
strcpy(LastName, "");
Photo = null;
Contract = null;
} // EmployeeClass()
virtual bool IsReady()
{
return F_EmployeeClass_IsReady;
} // bool IsReady(...)
// --> works like "generic" constructor from previous example
virtual void Create()
{
Key = 0;
strcpy(FirstName, "");
strcpy(LastName, "");
Photo = null;
Contract = null;
F_EmployeeClass_IsReady = true;
} // void Create()
// --> works like "light" constructor from previous example
virtual void CreateLight
(
int AKey,
char* AFirstName,
char* ALastName
)
{
Key = AKey;
strcpy(FirstName, AFirstName);
strcpy(LastName, ALastName);
Photo = null;
Contract = null;
F_EmployeeClass_IsReady = true;
} // void CreateLight()
virtual void Destroy()
{
F_EmployeeClass_IsReady = false;
} // void Destroy()
// --> works like "heavy" constructor from previous example
virtual void CreateHeavy
(
int AKey,
char* AFirstName,
char* ALastName,
Image* APhoto,
Blob* AContract
)
{
Key = AKey;
strcpy(FirstName, AFirstName);
strcpy(LastName, ALastName);
Photo = APhoto;
Contract = AContract;
F_EmployeeClass_IsReady = true;
} // void CreateHeavy()
void Insert();
}; // class
void Test()
{
...
int AKey = 0;
char AFirstName[150];
char ALastName[150];
Image* APhoto = null;
Blob* AContract = null;
// --> calling "light" constructor
AKey = 1;
strcpy(AFirstName, "Mary");
strcpy(ALastName, "Thompson");
EmployeeClass* AEmployee = new EmployeeClass();
AEmployee->CreateLight(AKey, AFirstName, ALastName);
AEmployee->Insert();
AEmployee->Destroy();
delete AEmployee;
// --> calling "heavy" constructor
AKey = 2;
strcpy(AFirstName, "John");
strcpy(ALastName, "Doe");
Image* APhoto = LoadPhoto();
Blob* AContract = LoadContract();
EmployeeClass* AEmployee = new EmployeeClass();
AEmployee->CreateHeavy
(AKey, AFirstName, ALastName, APhoto, AContract);
AEmployee->Insert();
AEmployee->Destroy();
delete AEmployee;
// --> calling "dummy" constructor,
// --> more work, but, more flexible
AKey = 1;
strcpy(AFirstName, "Mary");
strcpy(ALastName, "Thompson");
EmployeeClass* AEmployee = new EmployeeClass();
AEmployee->Create();
AEmployee->Key = AKey;
strcpy(AEmployee->FirstName, AFirstName);
strcpy(AEmployee->LastName, ALastName);
AEmployee->Photo = LoadPhoto();
AEmployee->Contract = LoadContract();
AEmployee->Insert();
AEmployee->Destroy();
delete AEmployee;
...
} // void Test()
在前面的示例中,每个实体都使用2个步骤创建,“虚拟”构造函数和补充方法(每种情况不同)具有有意义的标识符,在选择如何准备实体对象时非常有用。
同样可以销毁每个物体。
[4]重物业方法
最后,您可能需要添加一些负责在需要时加载“重”属性的方法。有时会被明确调用,有时会自动调用。
public class EmployeeClass
{
public:
bool F_EmployeeClass_IsReady;
public:
int Key;
char FirstName[150];
char LastName[150];
Image* Photo;
Blob* Contract;
public:
// --> only generic constructor
Employee()
{
F_EmployeeClass_IsReady = false;
Key = 0;
strcpy(FirstName, "");
strcpy(LastName, "");
Photo = null;
Contract = null;
} // EmployeeClass()
virtual bool IsReady()
{
return F_EmployeeClass_IsReady;
} // bool IsReady(...)
void LoadPhoto();
void SavePhoto();
void LoadContract();
void SaveContract();
// --> works like "generic" constructor from previous example
virtual void Create()
{
Key = 0;
strcpy(FirstName, "");
strcpy(LastName, "");
Photo = null;
Contract = null;
F_EmployeeClass_IsReady = true;
} // void Create()
// --> works like "light" constructor from previous example
virtual void CreateLight
(
int AKey,
char* AFirstName,
char* ALastName
)
{
Key = AKey;
strcpy(FirstName, AFirstName);
strcpy(LastName, ALastName);
Photo = null;
Contract = null;
F_EmployeeClass_IsReady = true;
} // void CreateLight()
virtual void Destroy()
{
F_EmployeeClass_IsReady = false;
} // void Destroy()
// --> works like "heavy" constructor from previous example
virtual void CreateHeavy
(
int AKey,
char* AFirstName,
char* ALastName,
Image* APhoto,
Blob* AContract
)
{
Key = AKey;
strcpy(FirstName, AFirstName);
strcpy(LastName, ALastName);
Photo = APhoto;
Contract = AContract;
F_EmployeeClass_IsReady = true;
} // void CreateHeavy()
// --> works like "heavy" constructor from previous example
virtual void CreateAndLoad
(
int AKey,
char* AFirstName,
char* ALastName
)
{
Key = AKey;
strcpy(FirstName, AFirstName);
strcpy(LastName, ALastName);
LoadPhoto();
LoadContract;
F_EmployeeClass_IsReady = true;
} // void CreateAndLoad()
void Insert();
}; // class
void Test()
{
...
int AKey = 0;
char AFirstName[150];
char ALastName[150];
Image* APhoto = null;
Blob* AContract = null;
// --> calling "load" constructor
AKey = 1;
strcpy(AFirstName, "Mary");
strcpy(ALastName, "Thompson");
EmployeeClass* AEmployee = new EmployeeClass();
AEmployee->CreateLoad(AKey, AFirstName, ALastName);
AEmployee->Insert();
AEmployee->Destroy();
delete AEmployee;
...
} // void Test()
使用其他方法,你可能有一个忽略它们的[假]构造函数,并且不加载“重”属性,一个调用它们的[假]构造函数。或者,使用不使用它们的[假]构造函数,&amp;显式调用加载器以获取特定的“重”属性。
如果您从文件系统路径加载图像,这些也有帮助,&amp;将它们保存到数据库字段中,或者反之,从数据库字段加载文件,&amp;将它们保存到文件系统路径。
干杯。
答案 1 :(得分:1)
[编辑]你的“旗帜”想法没问题。
在另一个答案中,我提供了另一种解决方案,我个人认为它是一个更好的解决方案,但并不意味着你的想法是错误的。
以下示例,我已经在某些情况下应用了它的相同的“标志”想法,可能的实现,您可能想要或不想要遵循。
示例:
public class EmployeeClass
{
// --> logic fields
private:
bool F_HeavyLoaded;
// --> "entity" fields
public:
int Key;
char FirstName[150];
char LastName[150];
Image* Photo; // <- picture
Blob* Contract; // <- scanned contract
public:
// --> constructors
EmployeeClass
(
int AKey,
char* AFirstName,
char* ALastName
)
{
Key = AKey;
strcpy(FirstName, AFirstName;
strcpy(LastName, ALastName);
Photo = null;
Contract = null;
F_HeavyLoaded = false;
} // EmployeeClass()
// --> "heavy" constructor
EmployeeClass
(
int AKey,
char* AFirstName,
char* ALastName,
Image* APhoto,
Blob* AContract
)
{
Key = AKey;
strcpy(FirstName, AFirstName;
strcpy(LastName, ALastName);
Photo = APhoto;
Contract = AContract;
F_HeavyLoaded = true;
} // EmployeeClass()
public:
// --> "entity" methods
bool IsHeavyLoaded() { return F_HeavyLoaded; }
void Insert();
void Update();
void Delete();
}; // class
在此示例中,有一个名为“F_IsHeavyLoaded”的私有标志字段,只能在构造函数中进行修改。它使用函数以只读方式公开提供。
干杯。