0

I have some Database-First MVC3 project that is actually an envelope for around 30 tables with CRUD operations.
There is EF5 with standard DBContext class and a DBSet for every table. The Linq2SQL is used for CRUD operations.
Every table is implemented in a separate controller with Index action that returns View, and a number of Json actions for CRUDs.
Right now, to optimize code I use C# type templates and extension methods.
Is it worth to implement there IoC patterns (NInject?, UnitOfWork?) just to optimize my code further and to remove code repetitiveness?
Do I rise code readability when I pool out all CRUD actions to IoC Reporitory/DBContext factory?

    public class tblCurrencyController : MvcApplication2.Controllers.AllInOneController<tblCurrency> { }
    public class tblCountryController : MvcApplication2.Controllers.AllInOneController<tblCountry> { }
    public class tblPriceLevelController : MvcApplication2.Controllers.AllInOneController<tblPriceLevel> { }
    public class tblJobController : MvcApplication2.Controllers.AllInOneController<tblJob> { }

    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    public class AllInOneController<T> : MyAuthenticationController where T : class, KeyValueSupported, new()
    {
        private MyDBContext context = new MyDBContext();

        public ActionResult Index()
        {
            var model = new AllInOneModel() {
                TypeName = typeof(T).AssemblyQualifiedName,
                Items = context.Set<T>().ToList()
            };

            return View("AllInOneView", model);
        }

        [HttpPost]
        public ActionResult Edit(T model)
        {
            Dictionary<string, string> errors = null;    
            var itm = context.Set<T>().AsEnumerable().FirstOrDefault(x => x.Key == model.Key);

            if (itm != null)
            {
                itm.Value = model.Value;
                context.Entry(itm).State = EntityState.Modified;
                errors = Helpers.ValidateJsonModel(context);
            }
            else
            {
                errors = new Dictionary<string, string>();
                errors.Add("genex", string.Format(Helpers.Resource("generr_item_not_found"), model.Key));  
            }

            return JsonNet(errors);
        }

        [HttpPost]
        public ActionResult Create(T model)
        {
            Dictionary<string, string> errors = null;    
            var items = context.Set<T>();
            var newitemId = items.Any() ? items.AsEnumerable().Max(x => x.Key) : 0;
            newitemId++;

            context.Set<T>().Add(new T() { Key = newitemId, Value = model.Value });
            errors = Helpers.ValidateJsonModel(context);

            return JsonNet(errors);
        }

        public ActionResult Delete(string itemId)
        {
            Dictionary<string, string> errors = null;    
            int parsedItemId = int.Parse(itemId);
            var item = context.Set<T>().AsEnumerable().Single(x => x.Key == parsedItemId);
            context.Set<T>().Remove(item);
            errors = Helpers.ValidateJsonModel(context);

            return JsonNet(errors, JsonRequestBehavior.AllowGet);
        }
    }
mirik
  • 356
  • 4
  • 18
  • Start your journey here: https://stackoverflow.com/tags/dependency-injection/info. – Steven Feb 26 '14 at 08:24
  • possible duplicate of [What is dependency injection?](http://stackoverflow.com/questions/130794/what-is-dependency-injection) – Steven Feb 26 '14 at 08:25
  • I'm not looking for some general academic answer, I need a strict answer relevant to my specific project. I saw a thousands of IoC resources but till now I see no reason of implementing it. – mirik Feb 26 '14 at 09:48
  • "One of the major advantages of dependency injection is that it can make testing lots easier." - Unitest is not critical. – mirik Feb 26 '14 at 09:50
  • Well, if your application is pure CRUD, I wouldn't even bother. I would probably even use LightSwitch to just drag and drop the whole application together. – Steven Feb 26 '14 at 10:23
  • Yes, but there are more than 30 controllers with almost identical CRUD operatons. So, I'm looking for the way to combine all Creates/Edits/Deletes to the relevant IoC Repositories and thus make controlers lighter. – mirik Feb 26 '14 at 10:27
  • This is not related to IoC/DI in general. What you should aim for is a generic repository, like `IUnitOfWork`, use a base class for your model and use the same repository. It is a matter of code reuse, which, in this case, IoC won't help you achieve. But yes, you increase readability and make controllers simpler when you move persistence to a repository/unit of work of some sort. – cvbarros Feb 26 '14 at 13:03
  • Is there a change that you write some small example of a IUnitOfWork as a base for all model classes and a Repository for CRUD actions? – mirik Feb 27 '14 at 12:45
  • I beleave this link http://stackoverflow.com/questions/6656683/how-to-marry-entityframework-repository-unitofwork-and-automapper-altogether-i can be the answer I'm looking for. – mirik Mar 01 '14 at 13:35

0 Answers0