0

First excuse the poorly written summary as I am not able to describe more precisely what I am looking for.

I have the following class:

public abstract class Table<T>
{
    public ReadOnlyCollection<T> list;

    protected abstract Func<T, object[], bool> Func { get; }
    protected T Find(params object[] objects) => list.First(element => Func(element, objects));

}

Which contains and abstract Func<T, object[], bool> that each derived class has to implement. So for example, the following classes:

public class Person
{
    public string FirstName;
}

public class Person2
{
    public string FirstName;
    public string LastName;
}

public class Employee
{
    public int ID;
}

Will have their corresponding Table<T> implementation like this:

public class TablePerson : Table<Person>
{
    protected override Func<Person, object[], bool> Func => (p, obj) 
    => p.FirstName == (string)obj[0];

    public Person GetPerson(string firstName) => Find(firstName);
}

public class TablePerson2 : Table<Person2>
{
    protected override Func<Person2, object[], bool> Func => (p, obj) 
    => p.FirstName == (string)obj[0] && p.LastName == (string)obj[1];

    public Person2 GetPerson2(string firstName, string lastName) => Find(firstName, lastName);
}

public class TableEmployee : Table<Employee>
{
    protected override Func<Employee, object[], bool> Func => (p, obj)
    => p.ID == (int)obj[0];

    public Employee GetEmployee(int id) => Find(id);
}

I am trying to change the object[] parameter of Func<T, object[], bool> to something that is type aware if possible. I am also not sure if this is the way it should be programmed.

Any help is appreciated.

elgato
  • 506
  • 1
  • 5
  • 20
  • 2
    what you might really are looking for is a ORM.... assuming that table is something db releated – Mat Oct 13 '22 at 18:16
  • What about `Table` although you will need separate classes for each n-ary number of generic parameters. – Charlieface Oct 13 '22 at 18:45
  • @Mat table is only a class wrapping a list that resides in memory – elgato Oct 13 '22 at 19:05
  • @Charlieface I believe the combinatorial explosion of input parameters would become overwhelming pretty soon – elgato Oct 13 '22 at 19:14
  • Why? Like I said: you only need a new class for each *number* of parameters, so up to 8 or 10 different classes at most. `Table` `Table` `Table` etc. You can abstract out some functionality into a non-generic base class which each inherits – Charlieface Oct 13 '22 at 19:17
  • `protected abstract Func Func { get; }` gets rid of the casts. That said I would keep them, not sure what's wrong with the way you have it now. – Zer0 Oct 13 '22 at 21:59
  • 1
    you can also use inMemory database with a lot of ORMs. For example Enity Framework. This also benefits that you can switch the datasource to something more useful than in memory in the future without headache. I already worked with "abstract the ORM away" solutions. For me this is the worst anti pattern you can go for... it's hard to come up with something more useful than a community of millions of developers... https://stackoverflow.com/questions/59651007/using-entity-framework-core-3-1-with-useinmemorydatabase-option-in-serviceprovid – Mat Oct 17 '22 at 15:20

0 Answers0