简体   繁体   中英

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)

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.

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.

Object Inheritance.

Although you said you didn't want to dirty up your models with this inheritance. What about creating a new class that inherits from TableServiceEntity and inherit all your models from the new class such as.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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