Search code examples
c#entity-frameworkgeneric-programming

Entity Framework, Method with any entity as parameter (How to make code more generic)


I have a method that takes a context and entity object as parameters. This method should be able to determine if a common property (COID in my code) of any class(table) has a value.

I can't find a way to rewrite this code so it is more generic, and at the moment I am checking the type of each entity passed to the method.

    public async static Task<bool> IsCOIDAssigned(ProjectEntities _context, object _entity)
    {
        var bSuccess = false;

        //First type to check
        if (_entity is tblLine)
        {
            var _line = _entity as tblLine;
            await _context.Entry(_entity).ReloadAsync().ContinueWith(x =>
            {
                if (!x.IsFaulted)
                {
                    var query = from c in _context.tblLines
                                where c.ID.Equals(_line.ID)
                                select c;
                    if(query.Single().COID.GetValueOrDefault() == 0)
                    {
                        Console.WriteLine("Not assigned");
                        bSuccess = true;
                    }  
                    else
                    {
                        Console.WriteLine("Assigned");
                        bSuccess = false;
                    }
                }
                else
                {
                    bSuccess = false;
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
        };

        //Second type to check
        if (_entity is tblDevice)
        {
            var _device = _entity as tblDevice;
            await _context.Entry(_entity).ReloadAsync().ContinueWith(x =>
            {
                if (!x.IsFaulted)
                {
                    var query = from c in _context.tblDevices
                                where c.ID.Equals(_device.ID)
                                select c;

                    if (query.Single().COID.GetValueOrDefault() == 0)
                    {
                        Console.WriteLine("Not assigned");
                        bSuccess = true;
                    }
                    else
                    {
                        Console.WriteLine("Assigned");
                        bSuccess = false;
                    }
                }
                else
                {
                    bSuccess = false;
                }

            }, TaskScheduler.FromCurrentSynchronizationContext());
        };


        //Third type to check ....
        //Fourth type to check ....

        return bSuccess;
    }

Does anyone see a better solution to the problem?


Solution

  • I followed arekzyla's solution to make my tables inheret tblBase. However, since I have a database-first model it was not possible to modify my model through code (not sure if this is possible?)

    I managed to get what I was looking for thanks to arekzyla, but with less linq queries involved, I am casting my entities as TblBase which is a class containing ID and COID (common table properties.)

    public static async Task<bool> IsEntityCheckedOut(ProjectEntities _context, object _entity)
        {
            var bCheckedOut = false;
            await _context.Entry(_entity).ReloadAsync().ContinueWith(x =>
            {
                if (!x.IsFaulted)
                {
                    var _baseentity = _entity as TblBase;
                    if (_baseentity.COID.GetValueOrDefault() != 0)
                    {
                        Console.WriteLine("Assigned");
                        bCheckedOut = true;
                    }
                    else
                    {
                        Console.WriteLine("Not Assigned");
                    }
                }
            }, TaskScheduler.FromCurrentSynchronizationContext());
            return bCheckedOut;
        }