asp.net - I am required to structure the C# coding better by organizing code into functions. I am not sure how to go about this: -


i using videogames database (sql) , coding asp project (visual studio). page has coding not organised , functionality called repeating coding. required structure c# coding better organizing code functions. can please give me guideline on how go it.

using system; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols;  namespace asplinqtosql {     public partial class : page     {         protected void page_load(object sender, eventargs e)         {             var ctx = new videogamesdatacontext();             var products = p in ctx.products                            select new                            {                                p.productid,                                p.productname,                                p.productdescription,                                p.listprice                            };             gridview1.datasource = products;             gridview1.databind();          }          protected void dropdownlist1_selectedindexchanged(object sender, eventargs e)         {             int supplierid = int.parse(dropdownlist1.selectedvalue);             var ctx = new videogamesdatacontext();             var products = p in ctx.products                            p.supplierid == supplierid                            select new                            {                                p.productid,                                p.productname,                                p.productdescription,                                p.listprice                            };             gridview1.datasource = products;             gridview1.databind();         }          protected void btnsearch_click(object sender, eventargs e)         {             if (chklike.checked == true)             {                 int supplierid = int.parse(dropdownlist1.selectedvalue);                 var ctx = new videogamesdatacontext();                 var products = p in ctx.products                                p.supplierid == supplierid                                && p.productname.startswith(txtproductname.text.tostring())                                select new                                {                                    p.productid,                                    p.productname,                                    p.productdescription,                                    p.listprice                                };                 gridview1.datasource = products;                 gridview1.databind();             }             else             {                 int supplierid = int.parse(dropdownlist1.selectedvalue);                 var ctx = new videogamesdatacontext();                 var products = p in ctx.products                                p.supplierid == supplierid                                select new                                {                                    p.productid,                                    p.productname,                                    p.productdescription,                                    p.listprice                                };                 gridview1.datasource = products;                 gridview1.databind();               }         } 

you should start separating ui logic (i.e. assigning data controls , reading data controls) data access logic (i.e. making queries database). suggest not extract methods, extract classes:

products repository, hides data access logic:

public interface iproductsrepository {     ienumerable<product> findall();     ienumerable<product> findbysupplierid(int supplierid); }  public class productsrepository : iproductsrepository {     private object _propertyname;     private dbcontext _db;      public productsrepository(dbcontext db)     {         _db = db;     }      public ienumerable<product> findall()     {         return _db.set<product>();     }      public ienumerable<product> findbysupplierid(int supplierid)     {         return _db.set<product>()                   .where(p => p.supplierid == supplierid);     } } 

also i'd create view model class instead of creating same anonymous objects each time:

public class productviewmodel {     public int productid { get; set; }     public string productname { get; set; }     public string productdescription { get; set; }     public decimal listprice { get; set; } } 

and used automapper map between product , productviewmodel. place mapping configuration in global.asax

mapper.createmap<product, productviewmodel>(); 

next - move products displaying separate method. code like:

public partial class : page {     private iproductsrepository _productsrepository;      public about()     {         mapper.createmap<product, productviewmodel>();         var db = new videogamesdatacontext();         _productsrepository = new productsrepository(db);     }      protected void page_load(object sender, eventargs e)     {                     var products = _productsrepository.findall();         showproducts(products);     }      void dropdownlist1_selectedindexchanged(object sender, eventargs e)     {         int supplierid = int.parse(dropdownlist1.selectedvalue);         var products = _productsrepository.findbysupplierid(supplierid);         showproducts(products);     }      protected void btnsearch_click(object sender, eventargs e)     {         int supplierid = int.parse(dropdownlist1.selectedvalue);         var products = _productsrepository.findbysupplierid(supplierid);          if(chklike.checked)         {             string name = txtproductname.text;             products = products.where(p => p.productname.startswith(name));         }          showproducts(products);     }      public void showproducts(ienumerable<product> products)     {         var viewmodels = mapper.map<ienumerable<productviewmodel>>(products);         gridview1.datasource = viewmodels;         gridview1.databind();     } } 

next steps - injecting repository page, or extracting presenter, keep ui simple.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -