简体   繁体   中英

abstract class Or Interface for SoftDelete

I have a project , I used Coefirst . In my project I Want Implement SoftDelete there is two way .

1st: User an Interface Like belowe :

 public interface ISoftDelete
{
    bool IsDeleted { get; set; }
}

If I use this way , I should Implement all classes that I want softDelete.

2nd : use An Abstract Class Like belowe :

public abstract class SoftDelete
{
    public bool IsDelelted { get; set; }
}

I dont know which one is better and there is no similar question on the Net.

Interface is a data passing contract and you don't have any implementations but abstract class can have default implementations. If you want to have implementation in your base class (here SoftDelete) you must use abstract class.

  1. Keeps it nice and simple, you don't need any special handling for the IsDeleted objects.

  2. You will need to handle inheritance in your database, that means realistically table per type inheritance. I'd recommend against this for this kind of approach as it will complicate queries and migrations, and can lead (down the line) to performance issues.

So I'd say 1 is better, even though it requires duplication in the code, its probably going to save you some headaches in the db.

according to MSDN

If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

I think Interface is better choice

Recommendations for Abstract Classes vs. Interfaces

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