我自己不喜欢包含大量代码的 SO 问题,但我将其包括在这里,因为这个问题是关于代码生成策略而不是故障排除。
我已经从自己的代码生成器切换到 EF,并且遇到了生成的实体与 ObjectContext 的紧密绑定程度的问题。使用代码优先方法似乎为您提供了更多的杠杆作用,因为您可以像普通 POCO 类一样抛出实体对象,而 EF 没有牛。
来自代码生成背景,我定制了 T4 模板,为我的代码所期望的实体类添加了一些标准实现。下面是一个例子:
`
public partial class Action:
Task,
CloudTech.ATS.Library.Interfaces.ICloneable<CloudTech.ATS.Templates.Action>,
CloudTech.ATS.Library.Interfaces.IPersistXml<CloudTech.ATS.Templates.Action>
{
#region Constructors, Destructor and Initializers.
//====================================================================================================
// Constructors, Destructor and Initializers.
//====================================================================================================
public Action ()
{
this.InitializeData();
}
public void InitializeData ()
{
// Native Types.
this.TemplateId = default(int);
// Complex Types.
// Foreign Keys.
this.Template = new CloudTech.ATS.Templates.Template();
// Child Objects.
this.ActionParameters = new System.Collections.Generic.List<CloudTech.ATS.Templates.ActionParameter>();
}
#endregion Constructors, Destructor and Initializers.
#region Interface Implementation: CloudTech.ATS.Library.Interfaces.ICloneable<T>.
//====================================================================================================
// Interface Implementation: CloudTech.ATS.Library.Interfaces.ICloneable<T>.
//====================================================================================================
public CloudTech.ATS.Templates.Action Clone ()
{
return (new CloudTech.ATS.Templates.Action().CopyFrom(this));
}
public CloudTech.ATS.Templates.Action CopyFrom (CloudTech.ATS.Templates.Action source)
{
this.InitializeData();
// Native Types.
this.TemplateId = source.TemplateId;
// Complex Types.
// Foreign Keys.
this.Template.CopyFrom(source.Template);
// Child Objects disabled for XML.
//this.ActionParameters.CopyFrom(source.ActionParameters);
return (this);
}
public CloudTech.ATS.Templates.Action CopyTo (CloudTech.ATS.Templates.Action destination)
{
return (destination.CopyFrom(this));
}
#endregion Interface Implementation: CloudTech.ATS.Library.Interfaces.ICloneable<T>.
#region Interface Implementation: CloudTech.ATS.Library.Interfaces.IPersistXml<T>.
//====================================================================================================
// Interface Implementation: CloudTech.ATS.Library.Interfaces.IPersistXml<T>.
//====================================================================================================
public System.Xml.XmlElement ToXmlElement (System.Xml.XmlDocument document)
{
System.Xml.XmlElement element = null;
element = document.CreateElement(this.GetType().Name);
// Native Types.
try { element.Attributes.Append(document, "TemplateId", this.TemplateId.ToString()); }
catch { element.Attributes.Append(document, "TemplateId", ""); }
// Complex Types.
// Foreign Keys.
element.AppendChild(this.Template.ToXmlElement(document));
// Child Objects commented for XML.
//element.AppendChild(this.ActionParameters.ToXmlElement(document));
return (element);
}
public bool FromXmlElement (System.Xml.XmlElement element)
{
bool result = true;
this.InitializeData();
// Native Types.
try { this.TemplateId = int.Parse(element.Attributes ["TemplateId"].Value); }
catch { result = false; }
// Complex Types.
// Foreign Keys.
this.Template.FromXmlElement(element ["Template"]);
// Child Objects.
//this.ActionParameters.FromXmlElement(element ["ActionParameters"]);
return (result);
}
#endregion Interface Implementation: CloudTech.ATS.Library.Interfaces.IPersistXml<T>.
}
`
以上是除 EF CF 当然生成的标准实体声明之外的所有生成代码。
我现在正在寻找一种以某种方式更改构造函数的方法,以便我自己的自定义类可以声明一个无参数构造函数,而不会与生成的构造函数发生冲突。我宁愿不必使用工厂方法,因为 Initialize() 和 InitializeData() 对我们来说是一个非常古老的约定。