Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My application's structure looks like this:

Solution: MySolution

Projects in MySolution: Host, Business, Server.

References between the projects looks like this: Host <-- Business --> Server (Business references Host and Server but Host does not reference Server and vice versa because this causes a circular-)

SO, I have some Database stuff in Server.

  • There I have a property: DBConenctionString - Which should contain the connection string.

  • What I want to do is to set this property from Host.

  • I am also keeping all the methods that has anything to do with the database, in the Server project.

Keeping interfaces in mind - so that I can also at a later stage just create a online application(web).

I have some code and its working but I don't know it this is the right way. If someone can just show me something or point me in the right direction?

CODE: I have this interface in Library (connection between the other two projects):

    public interface IDBUtils
    {
        string DBConnectionString { get; set; }

        bool OpenConnection();

        bool CloseConnection();

        bool ExecuteSQL(string sql);

        bool CreateDatabase(string servername, string databaseName, string databaseDataPath);

        bool CreateDatabaseUser(string servername, string username, string password);
    }

Then the class in Library that has the code for the interface:

public string DBConnectionString
{
    get
    {
        return DBService.DB.DBUtils.DBConnectionString;
    }
    set
    {
        DBService.DB.DBUtils.DBConnectionString = value;
    }
}

public bool OpenConnection()
{
    return DBService.DB.DBUtils.OpenConnection();
}

public bool CloseConnection()
{
    return DBService.DB.DBUtils.CloseConnection();
}

public bool ExecuteSQL(string sql)
{
    return DBService.DB.DBUtils.ExecuteSQL(sql);
}

public bool CreateDatabase(string servername, string databaseName, string databaseDataPath)
{
    return DBService.DB.DBUtils.CreateDatabase(servername, databaseName, databaseDataPath);
}

public bool CreateDatabaseUser(string servername, string username, string password)
{
    return DBService.DB.DBUtils.CreateDatabaseUser(servername, username, password);
}

I'm only going to show the property and one method on the server otherwise this is going to get too long.

On the Server: Property:

public static string DBConnectionString { get; set; }

The OpenCOnnection() Method:

        public static bool OpenConnection()
        {
            bool success = false;
            try
            {
                if (DBConnectionString.Length > 0)
                {
                    conn = new SqlConnection(DBConnectionString);
                    conn.Open();
                    success = true;
                }
            }
            catch { /* Do nothing... */ }
            return success;
        }

=========================================================================================

To set the property from Host on Server: It uses the interface on Library:

iDBUtils.DBConnectionString = connectionString.ToString();

To execute a method from Host on Server:

iDBUtils.OpenConnection();

So as you can see, Library is the "Middle Man".

share|improve this question
1  
What about having a Data layer that is your db layer and using a config file for the connection string? –  dreza Nov 7 at 8:41
 
Yes I understand that, but what about the methods then? –  Frederik Moller Nov 7 at 8:48
 
Also, If I put the connection string in the config file, will a company like be able to connect to a central server without going and editing the file? The app is basically on its own and not manual editing is done at the moment. –  Frederik Moller Nov 7 at 8:49
1  
So it's 4 projects then? What's the direction of dependencies between Library and the rest? Imo Business shouldn't depend on anyone, not on the UI, not on the data. –  retailcoder Nov 7 at 11:22
1  
Is the Catch { /* do nothing */ } really swallowing everything that could go wrong with opening a connection? Imo that method should either succeed or blow up with something more meaningful than a false... –  retailcoder Nov 7 at 11:38
show 3 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.