Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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();
    }
}
share|improve this question
1  
Why are you even considering using an array of strings? Don't you have an entity for what you need? –  svick Nov 6 '12 at 23:52
    
@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. –  Matt Rohde Nov 7 '12 at 1:27
    
@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..... –  dreza Nov 7 '12 at 1:37
    
@dreza I updated the question to show some code. I thought about it some more and posted the code, now that I wrote it. –  Matt Rohde Nov 7 '12 at 4:08
    
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 ? –  avip Nov 7 '12 at 5:37
show 2 more comments

1 Answer

up vote 1 down vote accepted

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;
        }
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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