简体   繁体   English

从实体框架的通用接口继承的实体

[英]Entities inheriting from generic interfaces in Entity Framework

I have the following interfaces: 我有以下界面:

public interface IInitializable
{
    void Initialize ();
}

public interface IPersistXmlElementTo
{
    XmlElement ToXmlElement (XmlDocument document);
}

public interface IPersistXmlElement<T>:
    IPersistXmlElementTo,
    IInitializable
{
    T FromXmlElement (XmlElement element);
}

public interface IPersistXmlDocumentTo:
    IPersistXmlElementTo
{
    XmlDocument ToXmlDocument ();
}

public interface IPersistXmlDocument<T>:
    IPersistXmlDocumentTo,
    IPersistXmlElement<T>,
    IInitializable
{
    T FromXmlDocument (XmlDocument document);
}

public interface ICloneable<T>
    where T: ICloneable<T>
{
    T Clone ();
}

public interface ICopyable<T>:
    IInitializable,
    ICloneable<T>
    where T: ICopyable<T>
{
    T CopyFrom (T source);
    T CopyTo (T destination);
}

public interface IEntity
{
    long Id { get; set; }
    byte [] TimeStamp { get; set; }
    DateTime DateTimeCreated { get; set; }
    DateTime DateTimeModified { get; set; }
}

All entity classes inherit from IEntity at the moment. 目前,所有实体类都从IEntity继承。 I'd now like IEntity to inherit from the interfaces above. 我现在希望IEntity从上面的接口继承。 That means converting IEntity to a generic interface. 这意味着将IEntity转换为通用接口。 Something like: 就像是:

public interface IEntity<T>:
    IPersistXmlDocument<T>,
    IPersistXmlElement<T>,
    ICloneable<T>,
    ICopyable<T>,
    IInitializable
    where T: IEntity<T>

I've had trouble with EF while inheriting from classes but interfaces seem to work ok so far. 从类继承时,我在使用EF时遇到了麻烦,但到目前为止接口似乎还可以。 Are there any gotchas to look out for? 有什么需要注意的地方吗? Any special configurations in EF required to switch from Entity: IEntity to Entity: IEntity<Entity> ? Entity: IEntity切换到Entity: IEntity<Entity>所需的EF中的任何特殊配置?

事实证明,通用接口对EF毫无影响。

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

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