public class MvNetDB
{
protected mvAccount _account = null;
private static Logger logger = LogManager.GetCurrentClassLogger();
protected void LoginToDB()
{
_account = new mvAccount(ConfigHelper.ServerLogin, ConfigHelper.ServerAddress);
}
protected void LogoutFromDB()
{
if (_account != null)
{
_account.Logout();
_account.Dispose();
}
}
protected mvItem ReadRecord(string filename, string recordID)
{
mvFile file = _account.FileOpen(filename);
mvItem item = file.Read(recordID);
return item;
}
}
public class Customer
{
public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public CustCode CustCode { get; set; }
}
public class CustCode
{
public string ID { get; set; }
public string Name { get; set; }
}
public class MvNetServiceCallRepository : MvNetDB, IServiceCallRepository
{
private Customer GetCustomerByID(string custID)
{
mvItem customerItem = null;
try
{
LoginToDB();
customerItem = ReadRecord("CUSTOMER", custID);
}
finally
{
LogoutFromDB();
}
Customer customer = new Customer();
customer.ID = custID;
customer.Name = customerItem["1"].ToString();
customer.Address = customerItem["2"].ToString();
customer.City = customerItem["3"].ToString();
customer.State = customerItem["4"].ToString();
customer.Zip = customerItem["5"].ToString();
string custCodeID = customerItem["42"].ToString();
customer.CustCode = GetCustCodeByID(custCodeID);
return customer;
}
private CustCode GetCustCodeByID(string custCodeID)
{
mvItem custCodeItem = null;
try
{
LoginToDB();
custCodeItem = ReadRecord("CUST.CODE", custCodeID);
}
finally
{
LogoutFromDB();
}
CustCode custCode = new CustCode();
custCode.ID = custCodeID;
custCode.Name = custCodeItem["1"].ToString();
return custCode;
}
}
Refactorings
No refactoring yet !
Moonshield
February 6, 2009, February 06, 2009 01:36, permalink
I would probably do something similar to this
public class MvNetDB
{
protected mvAccount _account = null;
private static Logger logger = LogManager.GetCurrentClassLogger();
protected void Login()
{
_account = new mvAccount(ConfigHelper.ServerLogin, ConfigHelper.ServerAddress);
}
protected void Logout()
{
if (_account != null)
{
_account.Logout();
_account.Dispose();
}
}
protected mvItem ReadRecord(string filename, string recordID)
{
mvItem item = null;
try
{
this.LoginToDB();
item = _account.FileOpen(filename).Read(recordID);
}
catch { }
finally
{
this.LogoutFromDB();
}
return item;
}
}
public class MvNetServiceCallRepository : MvNetDB, IServiceCallRepository
{
private Customer GetCustomerByID(string custID)
{
Customer oCustomer = null;
mvItem oItem = ReadRecord("CUSTOMER", custID);
if (oItem != null)
{
oCustomer = new Customer()
{
ID = custID,
Name = oItem["1"].ToString(),
Address = oItem["2"].ToString(),
City = oItem["3"].ToString(),
State = oItem["4"].ToString(),
Zip = oItem["5"].ToString(),
CustCode = GetCustCodeByID(oItem["42"].ToString())
};
}
return oCustomer;
}
private CustCode GetCustCodeByID(string custCodeID)
{
CustCode oCustCode = null;
mvItem oItem = ReadRecord("CUST.CODE", custCodeID);
if (oItem != null)
{
oCustCode = new CustCode()
{
ID = custCodeID,
Name = oItem["1"].ToString()
};
}
return oCustCode;
}
}
Ants
February 6, 2009, February 06, 2009 07:52, permalink
You can take advantage of IDisposable and using statements to remove the explicit try-finally blocks. The compiler will take care of doing the try-finally for you.
public class Customer
{
public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public CustCode CustCode { get; set; }
}
public class CustCode
{
public string ID { get; set; }
public string Name { get; set; }
}
public class MvNetSession : IDisposable
{
protected mvAccount _account = null;
public MvNetSession()
{
_account = new mvAccount(ConfigHelper.ServerLogin, ConfigHelper.ServerAddress);
}
~MvNetSession()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
if (_account != null)
{
_account.Logout();
_account.Dispose();
}
}
_account = null;
}
public mvItem ReadRecord(string filename, string recordID)
{
mvFile file = _account.FileOpen(filename);
mvItem item = file.Read(recordID);
return item;
}
}
public class MvNetServiceCallRepository
{
private Customer GetCustomerByID(string custID)
{
using (MvNetSession session = new MvNetSession())
{
mvItem customerItem = session.ReadRecord("CUSTOMER", custID);
return new Customer()
{
ID = custID,
Name = customerItem["1"].ToString(),
Address = customerItem["2"].ToString(),
City = customerItem["3"].ToString(),
State = customerItem["4"].ToString(),
Zip = customerItem["5"].ToString(),
CustCode = GetCustCodeByID(session, customerItem["42"].ToString())
};
}
}
private CustCode GetCustCodeByID(MvNetSession session, string custCodeID)
{
mvItem custCodeItem = session.ReadRecord("CUST.CODE", custCodeID);
return new CustCode()
{
ID = custCodeID,
Name = custCodeItem["1"].ToString()
};
}
private CustCode GetCustCodeByID(string custCodeID)
{
using (MvNetSession session = new MvNetSession())
return GetCustCodeByID(session, custCodeID);
}
}
Ants
February 6, 2009, February 06, 2009 08:39, permalink
Using delegates actually makes the code less readable, even with the use of lambda expressions:
public class Customer
{
public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public CustCode CustCode { get; set; }
}
public class CustCode
{
public string ID { get; set; }
public string Name { get; set; }
}
public class MvNetSession : IDisposable
{
protected mvAccount _account = null;
public MvNetSession()
{
_account = new mvAccount(ConfigHelper.ServerLogin, ConfigHelper.ServerAddress);
}
~MvNetSession()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected void Dispose(bool disposing)
{
if (disposing)
{
if (_account != null)
{
_account.Logout();
_account.Dispose();
}
}
_account = null;
}
public mvItem ReadRecord(string filename, string recordID)
{
return _account.FileOpen(filename).Read(recordID);
}
}
public class MvNetServiceCallRepository
{
delegate T FillRecordDelegate<T>(mvItem item);
private T ReadRecord<T>(MvNetSession session,
string filename,
string recordID,
FillRecordDelegate<T> fillRecord)
{
return fillRecord(session.ReadRecord(filename, recordID));
}
private Customer GetCustomerByID(string custID)
{
using (MvNetSession session = new MvNetSession())
{
return ReadRecord<Customer>(session, "CUSTOMER", custID,
customerItem =>
{
return new Customer()
{
ID = custID,
Name = customerItem["1"].ToString(),
Address = customerItem["2"].ToString(),
City = customerItem["3"].ToString(),
State = customerItem["4"].ToString(),
Zip = customerItem["5"].ToString(),
CustCode = GetCustCodeByID(session, customerItem["42"].ToString())
};
});
}
}
private CustCode GetCustCodeByID(MvNetSession session, string custCodeID)
{
return ReadRecord<CustCode>(session, "CUST.CODE", custCodeID,
custCodeItem =>
{
return new CustCode()
{
ID = custCodeID,
Name = custCodeItem["1"].ToString()
};
});
}
private CustCode GetCustCodeByID(string custCodeID)
{
using (MvNetSession session = new MvNetSession())
return GetCustCodeByID(session, custCodeID);
}
}
I'm new to C# and working on an app for my company. We use a legacy database and a product called mv.net to connect to it since there is no other good way to connect. I've got several classes that need to open a connection to the database, read some data and close the connection. I created a MvNetDB class that does the login, logout, and read. I have data classes Customer and CustCode that need to use these methods. I'm looking for the most efficient and best way without lots of repeating code. Here is a sample of what I'm trying to do. Some have suggested delegates, closures, etc., but I'm new and not familiar with those. Can anyone help me?