HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Interfaces and Classes between Projects

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
projectsbetweenclassesandinterfaces

Problem

My application's structure looks like this:

Solution: MySolution

Projects in MySolution: Host, Business, Server.

References between the projects looks like this:

Host  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. I have a property, DBConnectionString - which should contain the connection string. What I want to do is to set this property from the Host but I want to keep all methods that have 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 an web application.

I have some code and it's working but I don't know if this is the right way.

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,

Solution

Naming

IDBUtils is a name for a namespace, but for an interface representing a database system it is poorly named. Better use something like IDatabase, IDatabaseServer or IDatabaseSystem.

OpenConnection()

This method can be simplified by removing the success variable and also using a guard clause, which reduces horizontal spacing.

public static bool OpenConnection()
{
    if (String.IsNullOrWhiteSpace(DBConnectionString.Length))
    {
        return false;
    }

    try
    {
        conn = new SqlConnection(DBConnectionString);
        conn.Open();
        return true;
    }
    catch { /* Do nothing... */ }
    return false;
}

Code Snippets

public static bool OpenConnection()
{
    if (String.IsNullOrWhiteSpace(DBConnectionString.Length))
    {
        return false;
    }

    try
    {
        conn = new SqlConnection(DBConnectionString);
        conn.Open();
        return true;
    }
    catch { /* Do nothing... */ }
    return false;
}

Context

StackExchange Code Review Q#33974, answer score: 3

Revisions (0)

No revisions yet.