Force Derived Class to Implement Static Method C#

So the situation is like, I have few classes, all of which have a standard CRUD methods but static. I want to create a base class which will be inherited so that it can force to implement this CRUD methods. But the problem is, the CRUD methods are static. So I’m unable to create virtual methods with static (for obvious reasons). Is there anyway I can implement this without compromising on static.

Also, the signature of these CRUD methods are similar.

E.g.
ClassA will have CRUD methods with these type of Signatures

  1. public static List<ClassA> Get()
  2. public static ClassA Get(int ID)
  3. public static bool Insert(ClassA objA)
  4. public static bool Update(int ID)
  5. public static bool Delete(int ID)

ClassB will have CRUD signatures like

  1. public static List<ClassB> Get()
  2. public static ClassB Get(int ID)
  3. public static bool Insert(ClassB objB)
  4. public static bool Update(int ID)
  5. public static bool Delete(int ID)

So I want to create a base class with exact similar signature, so that inherited derived methods will implement their own version.

For E.g. BaseClass will have CRUD methods like

  1. public virtual static List<BaseClass> Get()
  2. public virtual static BaseClassGet(int ID)
  3. public virtual static bool Insert(BaseClass objBase)
  4. public virtual static bool Update(int ID)
  5. public virtual static bool Delete(int ID)

But the problem is I can’t use virtual and static due to it’s ovbious logic which will fail and have no meaning.

So is there any way out?

Also, I have few common variables (constants) which I want to declare in that base class so that I don’t need to declare them on each derived class. That’s why i can’t go with interface also.

Anything that can be done with Abstract class?

4

Such methods shouldn’t be static (what if you have to deal with two databases?) and they also shouldn’t be on your entity classes.

Instead, you should have a separate type, that represents the storage for this entity. That type could be generic, so that code that’s not entity-specific doesn’t have to be repeated.

This way, inheritance works (storage for ClassA can inherit from some base storage) and you don’t have to perform the nonsensical “I have to create a person to get a list of all persons”.

This pattern is for example used by Entity Framework, there the storage type is called DbSet<T>.

1

You’re going about this the wrong way. Any time you want to have a set of related methods like this and it looks like they should be static, consider the possibility that all this means is that you’re trying to put them in the wrong place. In this case, you seem to be working towards the table data gateway pattern. Just move those methods into a separate class as virtual methods, have a single shared instance of each such class, and an abstract generic superclass to ensure the implementations are consistent.

Another way of handling this is that the base class takes a dependency of something like:

public abstract class MyBaseClass<T> : where T : object {
    protected IMyDataRepository _repository;

    public MyBaseClass(IMyDataRepository repository){
       _repository = repository;
    }

    public abstract bool TryInsert(T item);
    // so you can implement explicit persistence on the derived classes

}

You can then use the repository to handle the state and persistence changes while uncoupling it statically from your class. You can achieve a degree of separation over inheriting directly from DbSet<T> if your program requires it.

See: https://www.martinfowler.com/eaaCatalog/repository.html

With static methods, you aren’t going to get polymorphism or any sort of base class dispatch for these methods. So, I think you’re looking for the ‘wrong thing’. But don’t let that get you down!

If you simply want to enforce that these methods are created on all classes deriving from a base, or interface, I can think of two easy ways off the top of my head.

First, in your unit test project, create a test that uses reflection across your assembly and fails if it finds any classes derived from your base that don’t supply static implementations of your crud routines.

Second, create a T4 template that builds a junk class for each class derived from your base. In the junk class, place dummy calls to each crud method. When you compile, the compiler will choke on those missing static methods.

A non-technical solution would be to hit any developers who check in classes with missing static methods with a giant foam hammer during code-review.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật