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.

7
  • 2
    what you might really are looking for is a ORM.... assuming that table is something db releated Commented Oct 13, 2022 at 18:16
  • What about Table<TObject, TKey1, TKey2> although you will need separate classes for each n-ary number of generic parameters. Commented Oct 13, 2022 at 18:45
  • @Mat table is only a class wrapping a list that resides in memory Commented Oct 13, 2022 at 19:05
  • @Charlieface I believe the combinatorial explosion of input parameters would become overwhelming pretty soon Commented Oct 13, 2022 at 19:14
  • 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... stackoverflow.com/questions/59651007/… Commented Oct 17, 2022 at 15:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.