Redundancja metod kontrolerów

0

Witam, dzisiaj przy tworzeniu pewnego systemu zorientowałem się iż zataczam koło i musi istnieć lepsze rozwiązanie. Mianowicie mam 4 kontrolery, które posiadają część wspólnych metod.
Każdy kontroler musi m.in oczekuje metody, która będzie mu zwracała liste obiektów danego typu. Problemem jest okropna redundancja mojego kodu. Nie potrafie zaimplementować odpowiedniego wzorca, tak by za to wszystko odpowiadała jedna metoda.

Kod nie jest skomplikowany, więc myśle, że nie musze więcej wyjaśniać.


public class FolderManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Get()
        {
            new Folder().Get(10);

            return View();
        }
    }

    public class ProductManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Get()
        {
            new Product().Get(10);

            return View();
        }
    }

    public class UserManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Get()
        {
            new User().Get(10);

            return View();
        }
    }

    interface IQuery
    {
        object Get();
        object Get(int counter);
        object GetDetails();
        void Add();
    }
    class User : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<User> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.User.ToList();
                }
                else
                {
                    list = databaseContext.User.Take(counter).ToList();
                }
            }

            return list;
        }
    }

    class Product : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<Product> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.Product.ToList();
                }
                else
                {
                    list = databaseContext.Product.Take(counter).ToList();
                }
            }

            return list;
        }
    }

    class Folder : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<Folder> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.Folder.ToList();
                }
                else
                {
                    list = databaseContext.Folder.Take(counter).ToList();
                }
            }

            return list;
        }
    }

1

Wystarczy z zwykłe dziedziczenie. Kontroler jest to klasa jak każda inna, w nim można dziedziczyć zachowanie. Kod który się powtarza wrzucasz do bazówki, a potem stosując pattern template method/strategy podmieniasz user product itd.

Z implementacje IQuery mozesz potraktować analogicznie.

0

Teraz poprawiłem, chodzi o to, że każda metoda w tym samym kontrolerze ma inny obiekt jako parametr

Moglbys podac przyklad bo post na gorze jest taki sam jak byl.

0

Nie moge edytować posta, więc wkleje kod raz jeszcze.

Klasy modeli do metody Add są rózne więc np:


public class UserModel
{
     public string Title
     public Item tem {get; set;}
}


public class FolderManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add(FolderModel model)
        {
            return View();
        }

        public ActionResult Get()
        {
            new Folder().Get(10);

            return View();
        }
    }

    public class ProductManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add(ProductModel model)
        {
            return View();
        }

        public ActionResult Get()
        {
            new Product().Get(10);

            return View();
        }
    }

    public class UserManageController
    {
        public ActionResult GetDetails()
        {
            return View();
        }

        public ActionResult Add()
        {
            return View();
        }

        public ActionResult Get()
        {
            new User().Get(10);

            return View();
        }
    }

    interface IQuery
    {
        object Get();
        object Get(int counter);
        object GetDetails();
        void Add();
    }
    class User : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<User> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.User.ToList();
                }
                else
                {
                    list = databaseContext.User.Take(counter).ToList();
                }
            }

            return list;
        }
    }

    class Product : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<Product> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.Product.ToList();
                }
                else
                {
                    list = databaseContext.Product.Take(counter).ToList();
                }
            }

            return list;
        }
    }

    class Folder : IQuery
    {
        public void Add()
        {
            throw new NotImplementedException();
        }

        public object Get()
        {
            return _GetRecords(0);
        }

        public object Get(int counter)
        {
            return _GetRecords(counter);
        }

        public object GetDetails()
        {
            throw new NotImplementedException();
        }

        private object _GetRecords(int counter)
        {
            List<Folder> list;

            using (var databaseContext = new DatabaseContext())
            {
                if (counter == 0)
                {
                    list = databaseContext.Folder.ToList();
                }
                else
                {
                    list = databaseContext.Folder.Take(counter).ToList();
                }
            }

            return list;
        }
    }

2

Zrób może generycznie

1

Albo zrob to generycznie, albo a jesli ´Add´ jest diametralnie różna to nie dodawaj jej do bazowki.

0
szydlak napisał(a):

Zrób może generycznie

Jak moge to zrobić generycznie, jeśli na podstawie tego obiektu tworze odpowiednią liste i wywołuje odpowiednio inny obiekt w kontekście bazy danych?

0

Np. metodą Set<T>() na DbContextcie

0
mad_penguin napisał(a):

Np. metodą Set<T>() na DbContextcie

Będe wdzięczny na pokazanie tego - ewentualne przykłady na kodzie, ponieważ nie widze tego, zwłaszcza metody Get która zwraca liste.

1

Na szybko coś takiego

class AbstractQuery<T> : IQuery
{
	public void Add()
	{
		throw new NotImplementedException();
	}

	public object Get()
	{
		return _GetRecords(0);
	}

	public object Get(int counter)
	{
		return _GetRecords(counter);
	}

	public object GetDetails()
	{
		throw new NotImplementedException();
	}

	private List<T> _GetRecords(int counter)
	{
		using (var databaseContext = new DatabaseContext())
		{
			var dbSet = databaseContext.Set<T>();
			if (counter == 0)
			{
				return dbSet.ToList();
			}
			else
			{
				return dbSet.Take(counter).ToList();
			}
		}
	}
}

Swoją drogą IMHO zamiast traktować 0 jako "zwróć wszystkie" lepiej już użyć typu int? i traktować null jako wszystkie. Przekazując 0 jako liczbę elementów spodziewam się otrzymać 0 elementów, a nie wszystkie :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1