0
\$\begingroup\$

I am asking this for coding style. I have a program class that calls a function(of a data access class) that reads from a table (using entity framework by the way), and I'm preparing to write the DataAccess.ReadMethod().

Is it better to pass the function a 2d string array string[rows][fields], reading each field and each row, and put it in the DataAccess.ReadMethod's out parameter string[rows][fields] to return to the main program and have the main program display it? or just repeatedly call the DataAccess.ReadMethod(string[fields]) and in the main program print each record out individually to the console?

I imagine that my ReadMethod() will look something like this:

public class DataAccess
{
    public Exception ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery, out string sManDetails)
    {
        Exception ex = null;
        sManDetails = "";
        try
        {
            foreach (Man m in myQuery)
            {
                sManDetails += m.ManID.ToString() + " " + m.Name + "\n";
            }
        }
        catch (Exception myException)
        {
            ex = myException;
        }
        return ex;
    }
}

the calling code in program class looks like this:

class program
{
    static private void DoRead()
    {
        var dbEntities = new TestDatabaseEntities();
        UserInterface MyUI = new UserInterface();
        DataAccess MyDA = new DataAccess();
        Exception ex = null;
        string sMyMen = "";

        var query = from person in dbEntities.Men
                    where true
                    select person;
        MyUI.DisplayDivider();
        ex = MyDA.ReadMan(dbEntities, query, out sMyMen);
        if (ex != null)
            sMyMen = ex.ToString();
        MyUI.DisplayMessage(sMyMen);
        MyUI.DisplayDivider();
    }
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ Why are you even considering using an array of strings? Don't you have an entity for what you need? \$\endgroup\$
    – svick
    Commented Nov 6, 2012 at 23:52
  • \$\begingroup\$ @svick DataAccess.ReadMethod() needs to return a variable which stores the contents that it read from the entity, i figured on storing the data in a string array, so that it can be passed back to the program class, and the program class can call my UserInterface.DisplayMethod(string somestring) method. I want to keep logic seperate from database access and UI. \$\endgroup\$
    – Matt Rohde
    Commented Nov 7, 2012 at 1:27
  • \$\begingroup\$ @ArmorCode It might be best if you wrote the ReadMethod() with the way you imagine it to be done and then post the code for review? Otherwise I'm not sure if this is the best forum for your question..... \$\endgroup\$
    – dreza
    Commented Nov 7, 2012 at 1:37
  • \$\begingroup\$ @dreza I updated the question to show some code. I thought about it some more and posted the code, now that I wrote it. \$\endgroup\$
    – Matt Rohde
    Commented Nov 7, 2012 at 4:08
  • \$\begingroup\$ Could you start by sharing your thoughts about returning an Exception? Please consult this link: Is it a good idea to return an Exception from a method ? \$\endgroup\$
    – avip
    Commented Nov 7, 2012 at 5:37

1 Answer 1

1
\$\begingroup\$

I have discovered, with thanks to you each, that it is better to call the display method within the DataAccess.ReadMethod() than to pass strings, Here's what I think i should do:

public class DataAccess
{
    public bool CreateMan(TestDatabaseEntities dbEntities, Man M)
    {
        return TryDataBase(dbEntities, "Man created successfully",
            () =>
            {
                dbEntities.Men.Add(new Man { ManID = M.ManID, Name = M.Name });
            });
    }

    public bool UpdateMan(TestDatabaseEntities dbEntities, IQueryable<Man> query, Man man)
    {
        return TryDataBase(dbEntities, "Man updated Successfully",
            () =>
            {
                foreach (Man M in query)
                {
                    M.Name = man.Name;
                }
            });
    }

    public bool DeleteMan(TestDatabaseEntities dbEntities, IQueryable myQuery)
    {
        return TryDataBase(dbEntities, "Man deleted Successfully",
            () =>
            {
                foreach (Man M in myQuery)
                {
                    dbEntities.Men.Remove(M);
                }
            });
    }

    public bool ReadMan(TestDatabaseEntities dbEntities, IQueryable myQuery)
    {
        UserInterface MyUI = new UserInterface();
        bool bSuccessful;

        bSuccessful = TryDataBase(dbEntities, "Records read successfully",
            () =>
            {   
                MyUI.DisplayDivider();
                foreach (Man m in myQuery)
                {
                    MyUI.DisplayMessage(new string[] { m.ManID.ToString(), m.Name });
                }
                MyUI.DisplayDivider();
            });


        return bSuccessful;
    }

    public bool TryDataBase(TestDatabaseEntities MyDBEntities, string SuccessMessage,  Action MyDBAction)
    {
        UserInterface MyUI = new UserInterface();
        try
        {
            MyDBAction();
            MyUI.DisplayMessage(SuccessMessage);
            return true;
        }
        catch (Exception e)
        {
            MyUI.DisplayMessage(e.ToString());
            return false;
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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