2
\$\begingroup\$

This is related to DataDictionary Application - Model

I haven't done much on the controllers yet, as it's the first time I've written Controller-type classes and wanted some quick feedback on really simple stuff, so these presently just contain various Get methods. If this is ok for a start, I'll begin adding in all the other stuff the controllers will do.

Controller

Here's where all that structuring (hopefully) in the Model starts to come in useful. I decided to write controllers for each schema - DictionaryController, SysDataController and CompareController. I realised after writing my DictionaryController, the other two would be near identical:

//original DictionaryController
class DictionaryController {
    private Dictionary.Context context;

    public DictionaryController() {
        this.context = new Dictionary.Context();
    }

    // gets all databases
    public List<Dictionary.Database> GetDatabases() {
        this.context.Databases.Load();
        return this.context.Databases.ToList();
    }

    // get database with the given primary key
    public Dictionary.Database GetDatabase(params object[] primaryKey) {
        return this.context.Databases.Find(primaryKey);
    }

    // gets all schemas
    public List<Dictionary.Schema> GetSchemas() {
        this.context.Schemas.Load();
        return this.context.Schemas.ToList();
    }

    // get all schemas belonging to the database with the given ID
    // any primary key can be passed, it will just read the first item in the array
    public List<Dictionary.Schema> GetSchemas(params object[] databasePrimaryKey) {
        var databaseID = (int)databasePrimaryKey[0];

        return (from schemas in this.Context.Schemas
                where schemas.DatabaseID == databaseID
                select schemas).ToList();
    }

    // etc
}

So, I scrapped that and instead wrote a general Controller abstract class (time to put that IContext to work!):

public abstract class Controller<TDatabase, TSchema, TTable, TColumn>
    where TDatabase : class, IEntityKey, IDatabaseKey
    where TSchema : class, IEntityKey, ISchemaKey
    where TTable : class, IEntityKey, ITableKey
    where TColumn : class, IEntityKey, IColumnKey 
{
    protected IContext<TDatabase, TSchema, TTable, TColumn> Context { get; set; }

    public List<TDatabase> GetDatabases()
    {
        this.Context.Databases.Load();
        return this.Context.Databases.ToList();
    }

    public TDatabase GetDatabase(params object[] primaryKey)
    {
        return this.Context.Databases.Find(primaryKey);
    }

    public List<TSchema> GetSchemas()
    {
        this.Context.Schemas.Load();
        return this.Context.Schemas.ToList();
    }

    public List<TSchema> GetSchemas(params object[] databasePrimaryKey)
    {
        var databaseID = (int)databasePrimaryKey[0];

        return (from schemas in this.Context.Schemas
                where schemas.DatabaseID == databaseID
                select schemas).ToList();
    }

    // etc
}

And now my concrete controllers are much more simple:

public class DictionaryController
    : Controller<Dictionary.Database, Dictionary.Schema, Dictionary.Table, Dictionary.Column>
{
    public DictionaryController()
    {
        this.Context = new Dictionary.Context();
    }
}

public class CompareController
    : Controller<Compare.Database, Compare.Schema, Compare.Table, Compare.Column>
{
    public CompareController()
    {
        this.Context = new Compare.Context();
    }
}

public class SysDataController
    : Controller<SysData.Database, SysData.Schema, SysData.Table, SysData.Column>
{
    public SysDataController()
    {
        this.Context = new SysData.Context();
    }
}

Is this a good way to structure it? Or should I consider breaking it down further into DictionaryDatabaseController, DictionarySchemaController etc?

\$\endgroup\$

Your Answer

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

Browse other questions tagged or ask your own question.