C# with Database code simplication -


i working on project centered around oracle database (though don't expect matter) , find myself have fair amount of duplicate code, exceptions. best way i've seen far question https://stackoverflow.com/a/1554/865868, suggests use of delegates. looked perfect solution until tried implement in project. found had 1 case not practical.

let me describe program bit. have 2 sections of code deal database operations. idea call function returns datatable, called loaddatatable(). have function deals inserting items list table.

private void addtolist(list<string> itemlist) {     try {                  using (oracleconnection connection = new oracleconnection(connectionstring)) {         connection.open();         foreach (string item in itemlist) {             using (oraclecommand command = new oraclecommand()) {                 command.connection = connection;                 //insert operation here                 //......                 command.executenonquery();             }         }      } catch (oracleexception ex) {         string messageboxtitle = "database exception";         switch (ex.number) {             case 00001:                 messagebox.show("constraint violation error", messageboxtitle, messageboxbuttons.ok);                 break;             case 12154:                 messagebox.show(string.format("connection error: {0}", ex.message), messageboxtitle);             break;             default:                 messagebox.show(ex.tostring());             break;         }     } }  private datatable loaddatatable() {     datatable datatable = new datatable();     try {         using (oracleconnection connection = new oracleconnection(connectionstring)) {             connection.open();             using (oraclecommand command = new oraclecommand()) {                 command.connection = connection;                 command.commandtext = sql;                 command.commandtype = commandtype.text;                  using (oracledataadapter oda = new oracledataadapter(command)) {                     oda.fill(datatable);                 }             }         }     } catch (oracleexception ex) {         string messageboxtitle = "database exception";         switch (ex.number) {             case 12154:                 messagebox.show(string.format("connection error: {0}", ex.message), messageboxtitle); //duplicate exception                 break;             default:                 messagebox.show(ex.tostring());                 break;             }         }     return datatable; } 

keep in mind had rewrite , simplify code discuss it. anyway, looking @ delegate example realized paramaters issue. can't use params list<string> types, there no doubt in usefulness of delegates able 1 one centralized section exceptions not duplicated.

private delegate void databaseoperation(list<string> itemlist); private void performdatabaseoperation(databaseoperation operation, list<string> itemlist){     try {         operation(itemlist);     } catch (oracleexception ex) {         string messageboxtitle = "database exception";         switch (ex.number) {             case 00001:                 messagebox.show("constraint violation error", messageboxtitle, messageboxbuttons.ok);                 break;             case 12154:                 messagebox.show(string.format("connection error: {0}", ex.message), messageboxtitle);             break;             default:                 messagebox.show(ex.tostring());             break;         }     } }  private void addtolist(list<string> itemlist) {                   using (oracleconnection connection = new oracleconnection(connectionstring)) {     connection.open();     foreach (string item in itemlist) {         using (oraclecommand command = new oraclecommand()) {             command.connection = connection;             //insert operation here             //......             command.executenonquery();         }     } } 

how use:

list<string> itemlist = new list<string>(); //code fill list performdatabaseoperation(addtolist, itemlist); 

problem unable implement loaddatatable() delegate not have parameters. use of params on delegate not work since list incompatible. i'm looking improve coding techniques re usability , readability find myself spinning wheels reading various threads on subject, since not go in depth enough beyond simple example doesn't catch problem find myself having now. ensure gets answered let me pose final question. how write code avoid duplicating exceptions?

update

for looking solve similar problem, see below. keep in there plenty more can done improve code. also, interested in discussion around var keyword discussed here, go here. hope helps:

private delegate void databaseoperation();  private void performdatabaseoperation(databaseoperation operation) {     try {         operation();     } catch (oracleexception ex) {         string messageboxtitle = "database exception";         switch (ex.number) {             case 00001:                 messagebox.show("constraint violation error", messageboxtitle, messageboxbuttons.ok);                 break;             case 12154:                 messagebox.show(string.format("connection error: {0}", ex.message), messageboxtitle);             break;             default:                 messagebox.show(ex.tostring());             break;         }     } }  private void addtolist(list<string> itemlist) {                   using (oracleconnection connection = new oracleconnection(connectionstring)) {     connection.open();     foreach (string item in itemlist) {         using (oraclecommand command = new oraclecommand()) {             command.connection = connection;             //insert operation here             //......             command.executenonquery();         }     } }  private datatable loaddatatable() {     datatable datatable = new datatable();      using (oracleconnection connection = new oracleconnection(connectionstring)) {         connection.open();         using (oraclecommand command = new oraclecommand()) {             command.connection = connection;             command.commandtext = sql;             command.commandtype = commandtype.text;              using (oracledataadapter oda = new oracledataadapter(command)) {                 oda.fill(datatable);             }         }     }      return datatable; } 

keep in mind delegates capture variables parent method's body. called closure. therefore not necessary delegate have parameter list.

var itemlist = new list<string>(); performdatabaseoperation(     () => {         ...         itemlist.add(...);         ...     } ); 

update

you call this:

list<string> itemlist = new list<string>(); performdatabaseoperation(() => addtolist(itemlist)); 

the trick pass lambda expression performdatabaseoperation has no parameters (the empty braces ()); performdatabaseoperation has no itemlist parameter. body of lambda expression uses itemlist declared , initialized before call performdatabaseoperation. c# compiler magic rest.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -