6
\$\begingroup\$

Currently, in my project I have one class that is dedicated to the all of the queries for the IBM i and converts it into usable models for my .NET code. There is already 12 methods in this class and there will be many, many more to come. This is for a tool that pulls data from our IBM i and pushes it to our internet database server.

Should I split this class up? Should I make it a partial class and put the code across multiple files? Should I do multiple classes?

Just wondering what the best practice is on something like that.

Currently this is the framework I have in place

class IbmIDatabase
{
    private DateTime ZERO_DATE = new DateTime(1900, 1, 1, 0, 0, 0);
    private string _connString = String.Empty;
    SystemCodeRepository scr = new SystemCodeRepository();

    /// <summary>
    /// Initializes a new instance of the <see cref="IbmIDatabase"/> class.
    /// </summary>
    public IbmIDatabase()
    {
    }

    #region System Codes
    /// <summary>
    /// Gets all system codes.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<SystemCode> GetAllSystemCodes()
    {
    }
    #endregion System Codes

    #region Citations
    /// <summary>
    /// Gets all citations.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<ParkingTicket> GetAllCitations()
    {
    }
    #endregion Citations

    #region Queue
    /// <summary>
    /// Gets the queued records.
    /// </summary>
    /// <returns></returns>
    public IEnumerable<QueuedRecord> GetQueuedRecords()
    {
    }

    /// <summary>
    /// Marks the queue as processed.
    /// </summary>
    /// <param name="id">The id.</param>
    public void MarkQueueAsProcessed(int id)
    {
    #endregion Queue

    #region Utility Bill Customer
    /// <summary>
    /// Gets the utility bill customer.
    /// </summary>
    /// <param name="id">The customer id.</param>
    /// <returns></returns>
    public Customer GetUtilityBillCustomer(int id)
    {
    }

    /// <summary>
    /// Gets the utility bill customer.
    /// </summary>
    /// <returns></returns>
    public IQueryable<Customer> GetUtilityBillCustomer()
    {
    }
    #endregion

    #region Utility Bill Details
    /// <summary>
    /// Gets the customer history.
    /// </summary>
    /// <param name="id">CustomerId</param>
    /// <returns></returns>
    public IQueryable<BillHistory> GetCustomerHistory(int id)
    {
    }

    /// <summary>
    /// Gets the customer history.
    /// </summary>
    /// <returns></returns>
    public IQueryable<BillHistory> GetCustomerHistory()
    {
    }
    #endregion

    #region Utility Bill Payment Details
    /// <summary>
    /// Gets the customer payment history.
    /// </summary>
    /// <param name="id">The id.</param>
    /// <returns></returns>
    public IQueryable<BillPaymentHistory> GetCustomerPaymentHistory(int id)
    {
    }

    /// <summary>
    /// Gets the customer payment history.
    /// </summary>
    /// <returns></returns>
    public IQueryable<BillPaymentHistory> GetCustomerPaymentHistory()
    {
    }
    #endregion

    #region Utility Bill Summary
    /// <summary>
    /// Gets the customer summary.
    /// </summary>
    /// <param name="id">CustomerId</param>
    /// <returns></returns>
    public IEnumerable<BillSummary> GetCustomerSummary(int id)
    {
    }

    /// <summary>
    /// Gets the customer summary.
    /// </summary>
    /// <returns></returns>
    public IQueryable<BillSummary> GetCustomerSummary()
    {
    }
    #endregion
}

Within each method can be quite a few lines of code (up to many 100 or so?). I left one of the longest ones so far in the sample code.

\$\endgroup\$
  • 1
    \$\begingroup\$ Please post some code (according to the FAQ). \$\endgroup\$ – palacsint Jan 11 '12 at 19:29
  • \$\begingroup\$ Sorry, I missed that. I thought I was within the guidelines since it as a best practice question. \$\endgroup\$ – Mike Wills Jan 11 '12 at 19:40
8
\$\begingroup\$

This class has a well defined role and by keeping it intact you are satisfying the Single Responsibility Principle. So, I would leave it like that. Perhaps you might want to look into how you could refactor some common code out of each method and into a separate utility class, but that's all.

\$\endgroup\$
  • \$\begingroup\$ Even if I have 40 methods in one file? That just seems like a maintenance nightmare in the long run. \$\endgroup\$ – Mike Wills Jan 11 '12 at 20:14
  • \$\begingroup\$ Why? because of a lot of scrolling up and down? I believe that maintenance gets hampered by spaghetti code, bad/improper dependencies, unclear class roles, state duplication, etc, and not by how many functions you group inside a single file. You should have 100 functions in the class if that is what is required of your class. \$\endgroup\$ – Mike Nakis Jan 11 '12 at 20:20
  • 1
    \$\begingroup\$ I was just thinking if I use the partial class ability in C#, I could break things apart a bit. Then I would have the files IbmIDatabase.cs and IbmIDatabase.Utilities.cs and it would be obvious that for utilities related methods, you go in to that file. \$\endgroup\$ – Mike Wills Jan 11 '12 at 20:22
  • \$\begingroup\$ The partial class feature of C# is primarily intended to aid the development of tools like the Visual Studio Forms Designer. But in any case, if you feel it will simplify things for you, fine. I would not do it because when I am looking for something I would rather have one file to scan rather than two. But that's a matter of personal preference. Keep in mind that you can also use the #region directive to group methods within a single file and collapse/expand the groups as you please. \$\endgroup\$ – Mike Nakis Jan 11 '12 at 20:25
  • 1
    \$\begingroup\$ @MikeWills - break your classes up based on responsibilities, not LOC. If it takes 40 methods to implement one responsibility, then so be it \$\endgroup\$ – Adam Rackis Jan 11 '12 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.