Difference between revisions of "DD Class9"

esse quam videri
Jump to: navigation, search
(Repo Pattern)
m (Text replacement - "</csharp>" to "</syntaxhighlight>")
Line 20: Line 20:
 
         public string CheeseDescription { get; set; }
 
         public string CheeseDescription { get; set; }
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
 
Then interface for the repo
 
Then interface for the repo
 
<csharp>
 
<csharp>
Line 30: Line 30:
 
         void Remove(T entity);
 
         void Remove(T entity);
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
 
Now we want to expand the interface definition to cheeses
 
Now we want to expand the interface definition to cheeses
 
<csharp>
 
<csharp>
Line 43: Line 43:
 
         //Cheese GetByName(string name)
 
         //Cheese GetByName(string name)
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
 
Finally the repo interface can then be implemented like this
 
Finally the repo interface can then be implemented like this
 
<csharp>
 
<csharp>
Line 100: Line 100:
 
         }
 
         }
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
  
 
Link to the implememtation http://iam.colum.edu/datadesign/gbrowser.php?file=/App_code/Repo/CheeseRepo.cs
 
Link to the implememtation http://iam.colum.edu/datadesign/gbrowser.php?file=/App_code/Repo/CheeseRepo.cs
Line 114: Line 114:
 
         gv1.DataSource = db.GetCheeses();
 
         gv1.DataSource = db.GetCheeses();
 
         gv1.DataBind();
 
         gv1.DataBind();
</csharp>
+
</syntaxhighlight>
  
  
Line 133: Line 133:
 
         public string CheeseDescription { get; set; }
 
         public string CheeseDescription { get; set; }
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
  
 
and then create a new cheese repo
 
and then create a new cheese repo
Line 143: Line 143:
 
         gv1.DataSource = db.GetCheeses();
 
         gv1.DataSource = db.GetCheeses();
 
         gv1.DataBind();
 
         gv1.DataBind();
</csharp>
+
</syntaxhighlight>
  
 
LINQ classes in MVC must use the entire namespace
 
LINQ classes in MVC must use the entire namespace
Line 160: Line 160:
 
         public string CheeseDescription { get; set; }
 
         public string CheeseDescription { get; set; }
 
     }
 
     }
</csharp>
+
</syntaxhighlight>
  
 
==Homework==
 
==Homework==

Revision as of 18:27, 25 January 2016


Repo Pattern

Definition from P of EE http://martinfowler.com/eaaCatalog/repository.html MSDN The Repository Pattern

The repository pattern is a nice way to abstract data fetching logic and easily separate concerns from different layers of your app. It allows of to quickly refactor and swap repos.

First we can start with the cheese definition

<csharp>

public class Cheese
   {
       
       public int CheeseID { get; set; }
       
       public string CheeseName { get; set; }
       
       public string CheeseDescription { get; set; }
   }

</syntaxhighlight> Then interface for the repo <csharp> public interface IRepository<T>

   {
       T GetById(int id);
       List<T> GetItems();
       void Add(T entity);
       void Remove(T entity);
   }

</syntaxhighlight> Now we want to expand the interface definition to cheeses <csharp> /// <summary>

   /// ICheeseRepo Changes the type of the Generic Repo To Cheese
   /// </summary>
   public interface ICheeseRepo : IRepository<Cheese>
   {
       List<Cheese> GetItems();
       List<Cheese> GetCheeses();  //New Method To Get Cheeses
       //May want to expand the definition to get cheese by name
       //Cheese GetByName(string name)
   }

</syntaxhighlight> Finally the repo interface can then be implemented like this <csharp> public class TestCheeseRepository : ICheeseRepo

   {
       //could grab these from the Application Object
       private static List<Cheese> fakeCheeses =
           new List<Cheese> { new Cheese { CheeseID=1, CheeseName="test1", CheeseDescription = "test desc 1" },
               new Cheese { CheeseID=2, CheeseName="test2", CheeseDescription = "test desc 2" },
               new Cheese { CheeseID=2, CheeseName="test3", CheeseDescription = "test desc 3" }
           };


       #region ICheeseRepo Members
       public List<Cheese> GetItems()
       {
           return GetCheeses();
       }


       public List<Cheese> GetCheeses()
       {
           return fakeCheeses.ToList();
       }
       #endregion
       #region IRepository<Cheese> Members
       public Cheese GetById(int id)
       {
           //linq statement to find cheese by ID
           var currentCheese = from c in fakeCheeses where c.CheeseID == id select c;
           return currentCheese.FirstOrDefault();
       }
       public void Add(Cheese entity)
       {
           fakeCheeses.Add(entity);
       }
       public void Remove(Cheese entity)
       {
           fakeCheeses.Remove(entity);
       }
       #endregion
       //added statement to find cheese by name
       public Cheese GetByName(string name)
       {
           //linq statement to find cheese by name
           var currentCheese = from c in fakeCheeses where c.CheeseName == name select c;
           return currentCheese.FirstOrDefault();
       }
   }

</syntaxhighlight>

Link to the implememtation http://iam.colum.edu/datadesign/gbrowser.php?file=/App_code/Repo/CheeseRepo.cs

Now to use the Repo

http://iam.colum.edu/datadesign/classsource/repo/Default.aspx

http://iam.colum.edu/datadesign/gbrowser.php?file=/classsource/repo/Default.aspx.cs

<csharp> ICheeseRepo db = new TestCheeseRepository();

       gv1.DataSource = db.GetCheeses();
       gv1.DataBind();

</syntaxhighlight>


You can add LINQ helpers to the class <csharp> /// <summary>

   /// Cheese Class for Our Repo
   /// </summary>
   [Table(Name = "Cheese")]
   public class Cheese
   {
       [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
       public int CheeseID { get; set; }
       [Column]
       public string CheeseName { get; set; }
       [Column]
       public string CheeseDescription { get; set; }
   }

</syntaxhighlight>

and then create a new cheese repo

and update your repo initialization

<csharp> ICheeseRepo db = new CheeseRepository(ConfigurationManager.ConnectionStrings["cheeseConnectionString"].ToString());

       gv1.DataSource = db.GetCheeses();
       gv1.DataBind();

</syntaxhighlight>

LINQ classes in MVC must use the entire namespace <csharp> /// <summary>

   /// Cheese Class for Our Repo
   /// </summary>
   [System.Data.Linq.Mapping.Table(Name = "Cheese")]
   public class Cheese
   {
       [System.Data.Linq.Mapping.Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
       public int CheeseID { get; set; }
       [System.Data.Linq.Mapping.Column]
       public string CheeseName { get; set; }
       [System.Data.Linq.Mapping.Column]
       public string CheeseDescription { get; set; }
   }

</syntaxhighlight>

Homework

Use the repo pattern to create a repo for you own class. The the class should map to a table in your database. The repo should be able to show items in you db, add items and remove items.

Add the repo to your Web from site and your MVC site. The repose should be able to list items from the DB. Either the MVC site or the WebForms site should be able to add and delete items. I'll give 1 pt extra for updating items and 1 pt for adding/deleting in MVC and Web Forms.