Should i use repository pattern for updating a single field? If so, how?

712 views Asked by At

i have like about 10 controller that have the exact same method, the only thing that changes is the table, and it is to update a single field from a table(explained better ahead)... So i was thinking about creating a method on my interface of generic repository for that...

The thing is, the class where i implement those methods doesn´t know what table we talking about, so i can´t reach the column, and of course, the columns have a diferent name...

More specific, i want to update order of a record.. if i place the record from 1 to 3 it changes those records order from 1 to 3.. This happens exacly in all the tables, each table as a field called OrderCompany or OrderCountry, etc... Since i cant get the field, how am i suposed to update in the repository ?

my code to update :

 [HttpPost]
        public async Task UpdateOrderAsync([FromBody] OrdemModel ordemObj)
        {
            using (var db = _context)
            {
                int ordemFinal = 0;

                //guardar item a ser movido
                var itemPegado = await _context.RH_Cargos.FindAsync(ordemObj.itemAMover);
                if (ordemObj.itemOndeFoi != 0)
                {
                    var itemFinal = await _context.RH_Cargos.FindAsync(ordemObj.itemOndeFoi);
                    ordemFinal = (int)itemFinal.OrdemCargos;
                }

                
                //update a todos os valores intermedios quando é para aumentar a ordem do item
                if (itemPegado.OrdemCargos < ordemFinal)
                {
                    var areas = db.RH_Cargos.Where(x => x.OrdemCargos > itemPegado.OrdemCargos & x.OrdemCargos <= ordemFinal).ToList();
                    areas.ForEach(o => o.OrdemCargos -= 1);
                }

                //update a todos os valores intermedio quando é para diminuir a ordem do item
                if (itemPegado.OrdemCargos > ordemFinal)
                {
                    ordemFinal++;
                    var areas = db.RH_Cargos.Where(x => x.OrdemCargos < itemPegado.OrdemCargos & x.OrdemCargos >= ordemFinal).ToList();
                    areas.ForEach(o => o.OrdemCargos += 1);

                }

                //guardar o item que se quis mover para o spot certo
                itemPegado.OrdemCargos = ordemFinal;

                //guardar
                db.SaveChanges();
            }

Any help is appreciated! thanks :)

1

There are 1 answers

5
Luke Briner On

You could use a PocoGenerator (for example https://marketplace.visualstudio.com/items?itemName=SimonHughes.EntityFrameworkReversePOCOGenerator) to create repository classes, each of which can inherit a base-class repository with common properties/methods but this sounds like a good example of somewhere where you should not be over-abstracting it. If you have different tables and are updating different columns then you are doing different things and should have clear explicit code that shows this rather than hiding behind a forced abstraction.

Otherwise you could use a number of patterns like Command pattern but if the problem is just your code above, I would leave it as it is, it doesn't look too bad.