摘要从模型继承类

时间:2014-01-09 21:23:40

标签: c#

我有一个必须从第三方类继承才能获得额外功能的模型。 (这是Azure的TableServiceEntity,对于此示例并不重要)

public class Business : TableServiceEntity
{
    public string Name {get; set;}
}

我真的不想用这种继承来弄脏我的模型,特别是如果我们决定换掉提供者的话。

我正在寻找关于抽象继承类的任何想法,或者使用IoC容器以某种方式解决它。

到目前为止,我唯一想到的可能性就是为每个模型分离一个部分类。然后把第三方继承引用放在那里,这样如果我们在某个时候离开azure就可以将它们丢弃。我必须设置几个azure特定属性,并且更愿意将它们保留在我的域模型之外(从可读性角度来看)。

所以我们最终得到:

public partial class Business
{
    public string Name {get;set;}
}

public partial class Business : TableServiceEntity
{
    public Business()
    {
        AzureProperty1 = "";
        AzureProperty2 = "";
    }
}

我只是不相信这是最好的方法。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

用户,

我不是在这里100%关注,但您可能希望看到它是这些途径之一。

对象继承。

虽然你说你不想dirty up你的模型具有这种继承。如何创建一个继承自TableServiceEntity的新类,并从新类继承所有模型,如。

/// <summary>
/// Inherits from table service entity
/// </summary>
public class BaseModel : TableServiceEntity
{

}

/// <summary>
/// Inherits from base model
/// </summary>
public class Business : BaseModel
{

}

您可能想要探索的另一个选项使用您自己的base模型,但使用TableServiceEntity此模型的属性,遵循与上述相同的行。如。。

/// <summary>
/// Inherits from table service entity
/// </summary>
public class BaseModel
{

    public BaseModel()
    {
    }

    public BaseModel(TableServiceEntity entity)
    {
        this.Entity = entity;
    }

    protected TableServiceEntity Entity { get; set; }
}

/// <summary>
/// Inherits from base model
/// </summary>
public class Business : BaseModel
{

}

现在,根据您的IoC控制器以及如何创建TableServiceEntity,可能无法使用第二个选项。