简体   繁体   English

从模型抽象继承的类

[英]Abstract inherited class from model

I have a model that must inherit from a third party class to get some extra functionality. 我有一个必须从第三方类继承的模型才能获得一些额外的功能。 (It's the TableServiceEntity from Azure, not really important for this example) (这是来自Azure的TableServiceEntity,对于此示例而言并不重要)

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

I really don't want to dirty up my models with this inheritance, especially if we ever decide to swap out providers. 我真的不想用这种继承弄脏我的模型,尤其是如果我们决定换掉提供者的话。

I'm looking for any ideas around abstracting the inherited class out, or tacking it on somehow with an IoC container. 我正在寻找有关抽象继承类的任何想法,或者使用IoC容器以某种方式解决它。

The only possibility I've thought of so far is to spin off a partial class for each model. 到目前为止,我想到的唯一可能性是为每个模型派生一个局部类。 Then put the third party inheritance reference in that so we can trash them if we move away from azure at some point. 然后将第三方继承引用放在其中,以便在某些时候远离天蓝色的情况下可以丢弃它们。 There are a couple azure specific properties I have to set as well and would prefer to keep them out of my domain model (from a readability perspective). 我还必须设置一些天蓝色的特定属性,并且希望将它们排除在我的域模型之外(从可读性的角度来看)。

So we would end up with: 因此,我们最终得到:

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

and

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

I'm just not convinced this is the best approach. 我只是不相信这是最好的方法。

Any ideas? 有任何想法吗?

user, 用户,

I am not following 100% here but what you may want to look it is one of these avenues. 我不在这里100%关注,但是您可能想看看它是这些途径之一。

Object Inheritance. 对象继承。

Although you said you didn't want to dirty up your models with this inheritance. 尽管您说过不想用这种继承dirty up模型。 What about creating a new class that inherits from TableServiceEntity and inherit all your models from the new class such as. 如何创建一个继承自TableServiceEntity的新类,并从新类继承所有模型,例如。

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

}

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

}

Another option you may want to explore follows the same lines as above using your own base model but having TableServiceEntity a property of this model. 您可能想要探索的另一种选择是使用您自己的base模型,遵循与上述相同的内容,但是TableServiceEntity是此模型的属性。 Such as. 如。

/// <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
{

}

Now depending on your IoC controller and how the TableServiceEntity is created the second option may not be possible. 现在,取决于您的IoC控制器和TableServiceEntity的创建方式,第二个选项可能无法实现。

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

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