简体   繁体   中英

Strategy Design Pattern and EF Core

I should implement strategy design pattern and I was thinking about what the right way is. Let's say I have the following example below.

I know Discriminator is able to do that on EF Core level but I should take off the inheritance for that case ( Mapping inheritance in EntityFramework Core ). What is the best practice? If you look in that other thread I linked, there is a solution with HasDiscriminator but I don't know about the inheritance.

public abstract class Strategy
{
    public int Id { get; set; }
    public string Name { get; set; }

    public abstract void SomeLogic();
}

public class FirstStrategy : Strategy
{
    public string CustomField { get; set; }

    public override void SomeLogic()
    {
        throw new NotImplementedException();
    }
}

public class SecondStrategy : Strategy
{
    public int CustomValue { get; set; }

    public override void SomeLogic()
    {
        throw new NotImplementedException();
    }
}

The strategy pattern is about behavior, while Entity Framework Core deals with data.

To persist an inheritance hierarchy with EF it should be fine sticking with the default pattern without customizing the discriminator (if that is what you want to avoid). See Inheritance for further information.

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