1

菜鸟问题

我在我的项目中使用实体框架,它在设计器中生成实体,例如:

public partial class CLMEntities : ObjectContext

我希望 CLMEntities 继承项目中的另一个类以获得更多功能:

public class MoreFunctionality

...当然我不能继承,因为它已经继承自ObjectContext

任何想法我怎么能做到这一点?

4

1 回答 1

3

几个选项,取决于功能MoreFunctionality是什么:

1:使其成为通用装饰器类而不是父类,如下所示:

public class MoreFunctionality<TEntities> where TEntities : ObjectContext
{
    private readonly TEntities _objectContext;

    public MoreFunctionality(TEntities objectContext)
    {
        this._objectContext = objectContext;
    }

    public TEntities ObjectContext
    {
        get { return this._objectContext; }
    }

    // Other behaviour
}

...并传递装饰器而不是ObjectContext.

2:使其成为接口并使用扩展方法添加行为,如下所示:

public interface IMoreFunctionality
{
}

public partial class CLMEntities : ObjectContext, IMoreFunctionality
{
}

public static class MoreFunctionalityExtensions
{
    public static void SomeFunctionality(
        this IMoreFunctionality moreFunctionality)
    {
        // Do something...
    }

    public static void SomeMoreFunctionality(
        this IMoreFunctionality moreFunctionality)
    {
        // Do something else...
    }
}
于 2013-02-03T08:55:45.917 回答