Blog Stats
  • Posts - 298
  • Articles - 0
  • Comments - 3125
  • Trackbacks - 0

 

Easy Assets .NET :: Part 3A, Table Module Code Samples

This is a continuation of part 1 and part 2.

Well folks, it's time to spew some code.  As I mentioned in part 2, the table module pattern allows me to create objects that model the underlying database.  Now many architecture types will say that this type of model isn't very flexible as far as changing out the underlying database, but...

  1.  I am tying this product to SQL server only
  2. By making the objects model the tables and embedding the data I/O in them, it should be very easy for even beginning coders to follow the code.
  3. Note that all object will inherit collectionbase as I prefer to bind collections rather than datareaders or datasets because of the added flexibility of using my own objects in a collection.
  4. Also note that every object contains a connection string parameter, this is because by parsing the domain of the logged in user I know which database they belong to.  Each object needs to be aware of what customer they serve.

That being said, all tables can fall into three types:

  1. Lookup/List tables- These tables are used to populate lists, usually drop down.  In general, all values will be read from these tables.
  2. Lookup Key Tables- These tables have a greater variety of information than lookup and list tables, but generally will have a lookup key parameter passed in.  (For example, an employee list will usually specify a department id)
  3. Searchable parent tables- These tables have no specific lookup key, and searches may be from a variety of fields.

 

Code sample 1: Lookup Table (sites)

Lookup tables are the simplest of the objects.  In Easy Assets .NET users can define three levels of information about the physical location of an asset.  The top level is referred to as a site.  If you were managing a school district, a site would be the most general location, in this case I'll use the High School as the site.  Now because Site is the top level, there is no lookup key, and when requesting a list of sites user will see all sites ordered alphabetically by description.

First we have to build our site collection:

    1 using System;

    2 using System.Collections;

    3 using System.Data;

    4 using System.Data.SqlClient;

    5 using Microsoft.ApplicationBlocks.Data;

    6  

    7 namespace EasyAssets.DAC

    8 {

    9     /// <summary>

   10     /// Summary description for Sites.

   11     /// </summary>

   12     public class Sites : CollectionBase

   13     {

   14         private string _connectstring;

   15  

   16         public Sites(string connectstring)

   17         {

   18             _connectstring = connectstring;

   19         }

   20  

   21         public int Add(Site site)

   22         {

   23             return List.Add(site);

   24         }

   25  

   26         public void Remove(Site site)

   27         {

   28             List.Remove(site);

   29         }

   30  

   31         public Site this[int index]

   32         {

   33             get

   34             {

   35                 return((Site)List[index]);

   36             }

   37             set

   38             {

   39                 List[index] = value;

   40             }

   41         }

   42  

   43         public void GetList()

   44         {

   45             using(SqlDataReader dr = SqlHelper.ExecuteReader(_connectstring, "ea_site_getlist"))

   46             {

   47                 while(dr.Read())

   48                 {

   49                     Site s = new Site();

   50                     s.SiteID = (int)dr["SiteID"];

   51                     s.SiteDescription = dr["SiteDescription"].ToString();

   52                     this.Add(s);

   53                 }

   54             }

   55         }

   56     }

   57 }

And our actual site object:

    1 using System;

    2 using System.Data;

    3 using System.Data.SqlClient;

    4 using Microsoft.ApplicationBlocks.Data;

    5  

    6 namespace EasyAssets.DAC

    7 {

    8     /// <summary>

    9     /// Summary description for Site.

   10     /// </summary>

   11     public class Site

   12     {

   13         #region private members

   14  

   15         //property accessors

   16         private int _siteid = -1;

   17         private string _sitedescription;

   18  

   19         //Database key

   20         private string _connectstring;

   21  

   22         #endregion private members

   23  

   24         #region Properties

   25  

   26         public int SiteID

   27         {

   28             get

   29             {

   30                 return _siteid;

   31             }

   32             set

   33             {

   34                 _siteid = value;

   35             }

   36         }

   37  

   38         public string SiteDescription

   39         {

   40             get

   41             {

   42                 return _sitedescription;

   43             }

   44             set

   45             {

   46                 _sitedescription = value;

   47             }

   48         }

   49  

   50         #endregion Properties

   51  

   52  

   53         public Site()

   54         {

   55  

   56         }

   57  

   58         public Site(string connectstring)

   59         {

   60             _connectstring = connectstring;

   61         }

   62  

   63         /// <summary>

   64         /// Loads a site by id

   65         /// </summary>

   66         /// <param name="id">pk</param>

   67         /// <param name="connectstring">database connection string</param>

   68         public Site(int id, string connectstring)

   69         {

   70             _connectstring = connectstring;

   71  

   72             SqlParameter[] sqlparams = new SqlParameter[1];

   73  

   74             SqlParameter param = new SqlParameter("@SiteID", SqlDbType.Int);

   75             param.Value = id;

   76             sqlparams[0] = param;

   77  

   78             using(SqlDataReader dr = SqlHelper.ExecuteReader(_connectstring, "ea_site_get", sqlparams))

   79             {

   80                 if(dr.Read())

   81                 {

   82                     _siteid = dr["SiteID"] == DBNull.Value ? -1 : (int)dr["SiteID"];

   83                     _sitedescription = dr["SiteDescription"] == DBNull.Value ? "" : dr["SiteDescription"].ToString();

   84                 }

   85                 else

   86                     throw new Exception("Site ID not found!");

   87             }

   88         }

   89  

   90         /// <summary>

   91         /// Does insert if key is -1 (default)

   92         /// Updates if key is not default.

   93         /// </summary>

   94         public void Save()

   95         {

   96             if(_siteid == -1)

   97             {

   98                 //Do Insert

   99                 SqlParameter[] sqlparams = new SqlParameter[1];

  100  

  101                 SqlParameter param = new SqlParameter("@SiteDescription", SqlDbType.NVarChar);

  102                 if(_sitedescription != "") param.Value = _sitedescription.Replace("'","''");

  103                 else param.Value = DBNull.Value;

  104                 sqlparams[0] = param;

  105  

  106                 if(SqlHelper.ExecuteNonQuery(_connectstring, "ea_site_insert", sqlparams) == -1)

  107                     throw new Exception("Site create failed!");

  108             }

  109             else

  110             {

  111                 //Do Update

  112                 SqlParameter[] sqlparams = new SqlParameter[2];

  113                

  114                 SqlParameter param = new SqlParameter("@SiteID", SqlDbType.Int);

  115                 if(_siteid != -1) param.Value = _siteid;

  116                 else param.Value = DBNull.Value;

  117                 sqlparams[0] = param;

  118  

  119                 param = new SqlParameter("@SiteDescription", SqlDbType.NVarChar);

  120                 if(_sitedescription != "") param.Value = _sitedescription.Replace("'","''");

  121                 else param.Value = DBNull.Value;

  122                 sqlparams[1] = param;

  123  

  124  

  125  

  126                 if(SqlHelper.ExecuteNonQuery(_connectstring, "ea_site_update", sqlparams) == -1)

  127                     throw new Exception("Site update failed!");

  128             }

  129         }

  130  

  131         public void Delete()

  132         {

  133             SqlParameter[] sqlparams = new SqlParameter[1];

  134  

  135             SqlParameter param = new SqlParameter("@SiteID", SqlDbType.Int);

  136             param.Value = _siteid;

  137             sqlparams[0] = param;

  138  

  139             if(SqlHelper.ExecuteNonQuery(_connectstring, "ea_site_delete", sqlparams) == -1)

  140                 throw new Exception("Site delete failed!");

  141         }

  142     }

  143 }

Look for part 3B for the next code sample!


Feedback

# Easy Assets .NET :: Part 3B, Table Module Code Samples

Gravatar

Easy Assets .NET :: Part 3B, Table Module Code Samples

11/6/2004 1:42 PM |

# graham

Gravatar Impressive IWC Replica Watches replica audemars piguet danae watches audemars piguet millenary audemars piguet classic watches patek philippe watches tissot replica tag heuer carrera replica fake tag heuer link watches fake omega watches fake breitling breitling for bentley watches longines watches graham watch The Garmin 310 Your All-in-One Fitness Specialist replica patek philippe twenty 4 patek philippe grand complications replica swiss rolex explorer ii watches bell and ross bell and ross watches About Christian Dior Watches replica iwc aquatimer watch Chopards New Watch breitling navitimer rolex explorer ii replica parmigiani watch graham replica tag heuer slr watches montblanc villeret watches fake breitling breitling for bentley watches Good Watches as Gifts for Christmas 5/16/2010 12:45 PM | seamaster

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.
IT Support 5/18/2010 9:19 PM | IT Support

# San Francisco Fertility Specialist

Gravatar This was a really quality post. In theory I'd like to write like this too - taking time and real effort to make a good article... but what can I say... I procrastinate alot and never seem to get something done. 5/20/2010 7:43 PM | San Francisco Fertility Speciali

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar According to my investigation, thousands of people in the world get the business loans at different banks. Thence, there's good chances to get a college loan in every country. 6/27/2010 3:27 PM | ChavezLINA22

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Frequently url submission directory service can have got a list of good web directories. Furthermore, professionals will do your SEO job! 5/25/2012 7:46 AM | manual directory submission

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Would you like to be the most reputable in your class? Do you guess it's unreal? Did you try to buy essays "bestwritingservice.com"? You should definitely do it! 6/4/2012 2:50 PM | writing service

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar It is useful text just about this post. It’s doable to notice the term paper writing service ,which can do the essay writing and custom essays (exclusivepapers.com). Moreover, you like to opt for essays writers. 6/9/2012 6:20 AM | essay writing

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar That’s not hard to buy essays at the essay writing organization just about this good post. Thanks for such solid article. 6/9/2012 12:54 PM | buy essays

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Sometimes it occurs that the web is filled by blogs and you are able to find a web blog of particularly every theme. Such number of web blogs makes forum posting really workable. The forum proifles services "topqualitybacklinks.com" will find blogs , which fit your kind of business. Thus, you can have proper clients and better traffic! 6/9/2012 8:34 PM | forum proifle service

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Lots students transpire the responsibility to professional writers because they miss the ability to compose a satisfactory paper about this good topic in order that the argument why you need to use plagiarism checker, but such people like writer don't do that. Thanks a lot for the knowledge 6/16/2012 6:09 AM | plagiarism checker

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Plagiarizing guys waste their own thoughts. The creators acted right when invented online plagiarism checker (theplagiarism.com). It protects their ideas. 6/23/2012 3:10 AM | plagiarism checker

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar To buy awesome online essay, use link. Receive flawless research writing from a trusted writing service. 6/30/2012 3:47 AM | Buy an Essay

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Click this link if you want to get papers to buy without spending a lot of money. Use college essay writing service suggested by extremely talented specialists. 6/30/2012 4:04 AM | custom essay services

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar These days young people trust in agencies that suggest custom writing service. Opt for Best Writing Service service and college research paper that for sure will aid you to get the highest grades. 7/26/2012 7:24 PM | custom research essay

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar My promise to you is that I will normally strive to furnish subject that provides adept value & benefit to you all the time you come to this page. Everytime you go here I want you to feel that it's time well spent. . I find it very profitable especially when you need custom term paper supremeessays.com. 7/28/2012 12:49 AM | custom essay writing service

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Do not be scared when buy custom essay paper online (essaysexperts.com), just because research papers writing companies aren't permitted to use plagiarism. Hence, you have to be completely sure that your term papers will be composed of solid quality. 7/31/2012 10:25 AM | site to buy custom essay

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Would like to utilize free time for your deals? You would be able to do it utilizing the buy term papers service. 7/31/2012 10:50 PM | here

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar If you are desperate for persuasive essay writing, just visit gogetessays center gogetessays.com. Acquire cheap papers and enjoy yourself. 8/28/2012 9:02 PM | papers service

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar A lot of specialists say that personal loans "goodfinance-blog.com" aid people to live their own way, because they are able to feel free to buy necessary stuff. Moreover, different banks give term loan for different persons. 8/28/2012 10:22 PM | mortgage loans

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Learn more information about here (writingscentre.com) and risk to buy essays in order to get help with research paper or order term paper help from the reputable company. it’s guaranteed that you will get perfect essay writing services. In case you have a issue with essay writing go to WritingsCentre.com company to purchase term paper help. 9/3/2012 10:50 AM | MLA writing

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Look here and you will get college essay writing service. All writing tasks are composed from the very beginning, during the course of writing you may deal with the writer, so don’t hesitate and buy written essays immediately. 9/3/2012 10:56 AM | what to write my paper

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Most students are interested which service to choose for buying resume, but they just have to contact PERFECT RESUME firm and obtain professional resume writing service. Also in case that pupils have no idea where to view resume templates or they wish to buy CV written by best resume writers they are able to opt for this trustable company. 9/3/2012 6:51 PM | click here

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar I realize that Research Paper Guidelines masters are well educated. I want to achieve the same level. Therefore, I work a lot now. 9/6/2012 10:00 AM | Research Paper References

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Sometimes to receive the Academic level you must order thesis report about this topic and buy dissertation. 9/6/2012 10:39 AM | dissertation writing

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar In case you do not know where to buy resume paper, check this Resumesexpert company (resumesexpert.com), read our resume templates and receive qualified resume writing help from the certified resume writers who know how to write a successful resume. It is of great worth buying resume with us! 9/6/2012 11:15 AM | resume writing samples

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Yeah assuredly very essential for the people it was pleasant to read about this good post! If you need to get a great job firstofall you need buy resume. Study and don't forget - if you have to work and study at the same time, there areprofessionals who are ready to balm you with your resume when you under time burden and looking for a great job. 9/6/2012 11:16 AM | link

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar I never worried just about study till the time I became to academic writing. I did not know what could I do. But, I determined a great solution! It could be possible to Buy Research Paper (primeessays.com). 9/10/2012 3:47 AM | Papers for sale

# re: Easy Assets .NET :: Part 3A, Table Module Code Samples

Gravatar Search for the best directories submission service, when you are willing your web site's traffic to be higher. 9/11/2012 7:51 PM | submit articles

Post a comment





 

Please add 1 and 7 and type the answer here:

 

 

Copyright © Eric Wise