Class extension method - good or bad idea?

I already have an Entity Framework, as well as a repository and some static classes / methods for managing data. Here is a typical example:

public static IEnumerable<Supplier> Contains(IEnumerable<int> idList) { return SupplierView.Select().Where(x => idList.Contains(x.ID)); } 

These methods request my EF repository, and sometimes I need to pass in a few variables to get the data I need.

Given that my provider object already exists, I am considering creating methods to extend queries using a class, something like this:

  public static IEnumerable<Supplier> GetSimilar(this Supplier s) { return SupplierView.Select().Where(/* the criteria matches */)); } 

It will be used only for data queries, but since I base the extension method on the whole, I'm not sure if this is a great design idea, but it is certainly more convenient that passing parameters / checking them, etc.

I already have a partial class created for my main objects, but I tend to add properties, low impact properties.

Any thoughts?

+5
source share
1 answer

Is the Supplier its own class?

If so, then I would recommend simply extending this class - instead of using extension methods.

If this is a class generated from the EF database, it is a public partial class , so you can easily write additional methods for Supplier in the second file, also using the public partial class - those that are then combined into one .NET class at compile time.

I like extension methods, and they make a lot of sense - if you need to add methods to classes that don't control - for example .. the .NET Framework classes or third-party classes.

+12
source

Source: https://habr.com/ru/post/1214252/


All Articles