patterncsharpMinor
Comic book conversion
Viewed 0 times
bookconversioncomic
Problem
I have two small structs (might change to classes later) that get loaded into Generic lists. The loading of the lists is what I'm asking about. Can it be done better or more object-oriented?
You'll notice I'm using MS Enterprise library for data access to a SqlCe database.
I'm just looking for some opinions on this code. Is there a more efficient way to do what I've written so far? My goal is to make the code as efficient and small as possible while still being maintainable and expandable.
```
public class ConversionUtility
{
private List _books;
private List _publishers;
private void Load()
{
try
{
_books = GetBooks();
_publishers = GetPublishers();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void PerformConversion()
{
Load();
//still working on this
}
private List GetPublishers()
{
List tmp = new List();
Database db = EnterpriseLibraryContainer.Current.GetInstance("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Publisher");
while (rdr.Read())
{
tmp.Add(new ComicPublisher { Id = (int)rdr["PublisherId"], Name = rdr["PublisherName"].ToString() });
}
return tmp;
}
private List GetBooks()
{
List tmp = new List();
Database db = EnterpriseLibraryContainer.Current.GetInstance("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Book");
while (rdr.Read())
{
tmp.Add(new ComicBook { Id = (int)rdr["BookId"], Title = rdr["Title"].ToString(), Company = rdr["Company"].ToString() });
}
return tmp;
}
}
public struct ComicPublisher
{
public int Id { get; set; }
public string Name { get; set; }
}
public struct ComicBook
{
public int Id { get; set; }
public s
You'll notice I'm using MS Enterprise library for data access to a SqlCe database.
I'm just looking for some opinions on this code. Is there a more efficient way to do what I've written so far? My goal is to make the code as efficient and small as possible while still being maintainable and expandable.
```
public class ConversionUtility
{
private List _books;
private List _publishers;
private void Load()
{
try
{
_books = GetBooks();
_publishers = GetPublishers();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void PerformConversion()
{
Load();
//still working on this
}
private List GetPublishers()
{
List tmp = new List();
Database db = EnterpriseLibraryContainer.Current.GetInstance("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Publisher");
while (rdr.Read())
{
tmp.Add(new ComicPublisher { Id = (int)rdr["PublisherId"], Name = rdr["PublisherName"].ToString() });
}
return tmp;
}
private List GetBooks()
{
List tmp = new List();
Database db = EnterpriseLibraryContainer.Current.GetInstance("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Book");
while (rdr.Read())
{
tmp.Add(new ComicBook { Id = (int)rdr["BookId"], Title = rdr["Title"].ToString(), Company = rdr["Company"].ToString() });
}
return tmp;
}
}
public struct ComicPublisher
{
public int Id { get; set; }
public string Name { get; set; }
}
public struct ComicBook
{
public int Id { get; set; }
public s
Solution
Firstly:
Close your DataReaders!!!
Secondly:
You may want to consider abstracting boilerplate code like this:
Into some helper methods.
Close your DataReaders!!!
Secondly:
You may want to consider abstracting boilerplate code like this:
Database db = EnterpriseLibraryContainer.Current.GetInstance("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Book");Into some helper methods.
Code Snippets
Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("CeConnectionString");
IDataReader rdr = db.ExecuteReader(CommandType.Text, "SELECT * FROM Book");Context
StackExchange Code Review Q#6267, answer score: 4
Revisions (0)
No revisions yet.