Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# .NET MVC EF
I've used EF-DB first approach and have written a simple app to retrieve the data from the database and show it in the view. The code is simple and ok for a beginner like me, but how can I implement an effective design-pattern like interface-specification pattern or generic repository pattern, and possibly dependency injection to improve the code below ?
 
Please note that I've started looking into topics like interfaces, design patterns, etc only recently, So I'd greatly appreciate it if you could please break down the explanation to a beginner level.
 
Below are my view-model and the controller. My model contains a stored-procedure and a table in the .edmx file - Thanks
 
**View-Model**
        public class OfficeDTO
        {
            public IEnumerable<employeedto> Employees { get; set; }
            public IEnumerable<departmentdto> Departments { get; set; }
        }
    
        public class EmployeeDTO
        {
           public int EmpId { get; set; }
           public string EmpName { get; set; }
        }
    
        public class DepartmentDTO
        {
            public string DeptCode { get; set; }
            public string DeptName { get; set; }
        }
**Controller**
 
      public ActionResult Index()
                {
                    EF.OfficeEntities ctx = new EF.OfficeEntities();
                    Models.OfficeDTO office = new Models.OfficeDTO();
                  
                    using (ctx)
                    {
        
                        var empList = ctx.GetEmployeesByYearJoined("2009")
                                  .ToList();
        
                        var empResults = (from q in empList
                                       select new Models.EmployeeDTO
                                       {
                                           EmpId = q.EmpID,
                                           EmpName = q.FirstName + q.LastName
                                       }).ToList();
                        office.Employees = empResults;
        
                        var depResults = (from q in ctx.Departments
                                          select new Models.DepartmentDTO
                                          {
                                              DeptCode = q.DepartmentCode,
                                              DeptName = q.DepartmentName
        
                                          }).ToList();
                        office.Departments = depResults;
                    }
        
                    return View(office);
                }</departmentdto></employeedto>
Posted 15-Feb-13 10:39am
Edited 15-Feb-13 10:45am
v2
Comments
TRK3 at 15-Feb-13 16:23pm
   
Don't.
 
Look up "YAGNI" -- it is a far, far more valuable skill for a professional programmer to learn "YAGNI" than it is to learn "design patterns".
 
There is absolutely no reason to make use of a "design pattern" unless that pattern actually solves an existing problem that you have.
 
Don't even try to do it as "a learning exercise".
 
The only thing you should do when initially studying "design patterns" is take note of what problem that particular design pattern solves -- and take great pains to understand what the problem is that it claims to solve.
 
Then, if you actually encounter that problem at some point in the future, remember that there is a design pattern for that and go look it up and see how it applies to your actual problem. Decide if it actually does solve your problem. And only then apply it.
 
Don't try to use as many design-patterns as possible. Don't try to make your code fit a design-pattern if that pattern doesn't solve a problem you actually need to solve.
 
There is absolutely nothing wrong with the code you wrote.
 
It's clear what it does. It's easy to follow, and it should be simple to debug.
 
If you start throwing in dependency-injection and generic-repository and, and ... it becomes so abstract that you won't be able to tell what the code does just by looking at it, and debugging it will be an exercise in stepping through all kinds of nested calls and indirections -- way more work than it is worth.
devdev13 at 15-Feb-13 18:35pm
   
Thanks a lot for your comment. I will certainly consider your advice, but if you have to improve the code I've written above, what are the steps you would take ? I was going through the code-first approach, and almost every piece of code i see, looks pretty different from what i've written. And most of the people I speak to suggest me to use generic repository pattern because it supports unit-testing, and increases performance when accessing data from multiple sources. what are your thoughts ? Is there anychance you could please refactor my code, and make it look more like the ones on ASP.NET website ? Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 120
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,611
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100