Jose Gomez
Jose Gomez

Reputation: 122

How to apply the same query to different entities?

So, I'm beggining to use EF, and I'm developing an application using it as ORM. The thing is I haven't had much time to dig into the documentation (I Plan to, in due time)and I'm kind of lost in some things. For example, I have these two queries:

public static int GetNextPadlockNumber()
        {
            LockersDBEntities1 entities = new LockersDBEntities1();
            var query = (from p in entities.PadLocks select p.PadlockNumber).DefaultIfEmpty(0).Max();
            return (int)query + 1;
        }

        public static Data.PadLock GetPadLockByNumber(int number)
        {
            Data.LockersDBEntities1 entities = new LockersDBEntities1();
            var query = (from p in entities.PadLocks where p.PadlockNumber == number select p).FirstOrDefault();
            return query;
        }

and

public static int GetNextLockerNumber()
        {
            LockersDBEntities1 entities = new LockersDBEntities1();
            var query = (from l in entities.Lockers select l.LockerNumber).DefaultIfEmpty(0).Max();
            return (int)query+1;
        }

        public static Data.Locker GetLockerByNumber(int number)
        {
            Data.LockersDBEntities1 entities = new LockersDBEntities1();
            var query = (from l in entities.Lockers where l.LockerNumber == number select l).FirstOrDefault();
            return query;
        }

And the thing is They're Exactly the same query. Isn't there Any way to do this without having to specify that I want a locker or a padlock? Thanks in advance heroes.

Upvotes: 4

Views: 113

Answers (2)

SWeko
SWeko

Reputation: 30882

One of the great things with EF is that you can do things like that. It's not trivial, and it will take some trial and error, but you can abstract a lot of the database complexities away.

Now, assuming that LockersDBEntities1 is a DbContext, you could something like this:

public static class LockersDBEntities1Extensions
{
   public static int GetNextNumber<T>(
        this LockersDBEntities1 context, 
        Expression<Func<T, int>> getNumberExpression)
   {
     var query = (from item in context.Set<T> 
                  select getNumberExpression(item))
                 .DefaultIfEmpty(0)
                 .Max();
     return (int)query + 1;
    }
}

and use it like:

int nextPadlockNumber = new LockersDBEntities1()
                                    .GetNextNumber<Padlock>(p => p.PadlockNumber)

and

int nextPadlockNumber = new LockersDBEntities1()
                                    .GetNextNumber<Locker>(l => l.LockerNumber)

The getNumberExpression expression is needed because there is no common way to access the number across all entities. It might be overdesign, but if that is an issue I would do something like this:

//this should be a better name, to reflect the real scenarion
interface ILockNumberProvider 
{
   int Number {get; }
}

and implement that interface on Locker, Padlock and other classes that need to provide lock numbers. Then, in the method above the expression can be omitted, and a generic constraint can be used, like:

public static class LockersDBEntities1Extensions
{
   public static int GetNextNumber<T>(this LockersDBEntities1 context)
                     where T:ILockNumberProvider 
   {
     var query = (from item in context.Set<T> 
                  select item.Number)
                 .DefaultIfEmpty(0)
                 .Max();
     return (int)query + 1;
    }
}

Upvotes: 2

dutzu
dutzu

Reputation: 3910

You could have used the LockerNumber and PadlockNumber as Identity Autoincrement fields since that is what you are doing, and then named them the same (Id for instance).

And that way the need to "get next locker number" is no longer necessary since every new entity inserted will get the next available number and with a Repository pattern you could have those methods as common methods in the Base Repository class as something like this:

public IEntity GetEntityById(int number)
{
   return ObjectSet.Single(x => x.Id == number);
}

Upvotes: 5

Related Questions