3

Okay, the title is long, but my problem is here:

I have about 10-12 different Objects and I have to work with them in my software. It's easy, I'm just creating classes and do whatever I want. But, all this classes representing database rows and main problem - my software have to be able reading/writing this objects from/to different databases (oracle, mysql, interbase, postgres, firebird). Objects are stored in different databases, even in different tables in different fields (names and types) [and all this, cause This databases are not syncronized between themselfs].

So I get trouble writing code to provide all works: let's think it's around 5 databases * 10 objects = 50 classes for all, and extensions in future...

Can anybody tell me the Right Way, Best Practice, how to construct this?

5
  • A very dirty solution would be to create a Key Value store for each database type and map all objects to Key Value backing data. Then you only need a couple of adapters to actually save and retrieve the data from the various databases. Aug 27, 2013 at 12:49
  • Right Way would be to move everything into one DB
    – athabaska
    Aug 27, 2013 at 12:51
  • athabaska, I agree, but it's not possible. It's an old infractucture and I can do nothing with it as a software developer =( I even not able to copy data to One database, cause it's around half Tb in every per day.
    – Artem E
    Aug 27, 2013 at 12:53
  • Stop writing a question and start writing 50 classes 8}
    – user1200540
    Aug 27, 2013 at 12:56
  • ahah %) thanks. But, as I see I'll be working with this software about year or more, till I say bye to company i'm working in :) So, I decided to think twice before coding
    – Artem E
    Aug 27, 2013 at 12:58

4 Answers 4

3

First of all, write your class to wrap all this calls for different databases. Probably you will use the data providers provided by the database to connect to each one.

This driver, provides a series of classes to work with the databases, using ado.net. Use this classes on your wrap class, to import/export the data. Normally this data providers have a way to map the database type to .net clr types.

And, you have to discover the key points for each database, for example: Where the database in question stores information for table, table fields, indexes, keys..

EDIT:

  • Read the table in the database storing his information and information of the fields for that table(field names, type, length, etc)
  • In this class that you will write to store this table info, create a field for store the clr type too (like i said before, this data providers provide a way to map the database type to .net clr type)
  • Ok, now you have the table info, respecting the characteristic for each database.

Now, you can start your import/export operations.

For example, supposing you are reading from oracle and writing into sql.

  • You read the table info for that table.
  • Read the data from the table(select) and stores on an datatable
  • Now, all you have to do is an insert into sql table, using of course, the data readed from oracle.
  • And remember the info that you got for the table? You need to respect the type mapping for each database.

See:

http://technet.microsoft.com/en-us/library/ms151817.aspx

1
  • So I Need Interface/Abstarct class to provide reading/writing data to several databases, a class implementing/extending it for each database. And in this classes I have to be able reading database-to-object map and getting/setting objets fields by theirs names?
    – Artem E
    Aug 27, 2013 at 13:11
2

Objects are stored in different databases, even in different tables in different fields (names and types)

If the names of the fields are different then you need to map your fields to the object at the dataaccess level. No need of creating 50 classes. 10 are enough. when you read data from the database map it to the class and use that class in the application. Similarly when you are about to save data you should read the fields from same database and pass to the database accordingly.

3
  • Hmm. Okay, I see. I understand mapping here, but I don't know how to provide all the databases with same code. Is it real to provide interbae, firebird, oracle, mysql with Entity Framework or somethink like that?
    – Artem E
    Aug 27, 2013 at 12:56
  • you can't provide the same feed to all the animals here. You will have to write a method that takes input the data and transforms the data according to the database needs.
    – Ehsan
    Aug 27, 2013 at 12:58
  • so, I need as much providers as much databases I have to provide? And use them sending data&mapping? It that trully The Right Way?
    – Artem E
    Aug 27, 2013 at 13:00
1

I would achieve your requirements in the following way:

First, I would add a number of dbml (Linq2SQL) files, one for each database. You could use ADO.NET or something else if you prefer. You'll have to call your generated data objects differently... something like MySqlDataObject, OracleDataObject, FirebirdDataObject, etc.

In your UI, you only need one set of data objects. Therefore, in your data layer, you will need methods that fill each data object from the different databases... like CopyToMySqlDataObject(ClrDataObject clrDataObject), CopyToOracleDataObject(ClrDataObject clrDataObject), CopyToFirebirdDataObject(ClrDataObject clrDataObject).

Finally, I would use a DataProvider class that references all of your data source objects. This class would host each action available and in each method, would call every data source. Take a look at this example:

public int UpdateDataObject(ClrDataObject clrDataObject)
{
    using (MySqlDataContext dataContext = new MySqlDataContext())
    {
        MySqlDataObject mySqlDataObject = dataContext.MySqlDataObjects.Where(d => 
d.Id == clrDataObject.Id).FirstOrDefault();
        CopyToMySqlDataObject(clrDataObject, mySqlDataObject);
        dataContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
        return 0;
    }
    ...
    using (OracleDataContext dataContext = new OracleDataContext())
    {
        OracleDataObject oracleDataObject = dataContext.OracleDataObjects.Where(d => 
d.Id == clrDataObject.Id).FirstOrDefault();
        CopyToOracleDataObject(clrDataObject, oracleDataObject);
        dataContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
        return 0;
    }
}
4
  • 2
    Linq2sql will only support sql server, EF would be the better choice Aug 27, 2013 at 13:17
  • Is there anythink like dbml for EF?
    – Artem E
    Aug 27, 2013 at 13:18
  • 2
    @christiandev, thanks... good point. Yes Artem, ADO.NET Entity Data Models have edmx files, rather than dbml files.
    – Sheridan
    Aug 27, 2013 at 13:21
  • @Sheridan, maybe you worked with it? I tried to find out providers for Interbase, Firebird, but found only OleDB - and I don't like it. I will not get autogenerated classes for this databases?
    – Artem E
    Aug 27, 2013 at 13:25
1

repository pattern and single responsibility for each repository, and possibly a generic repository that invokes each of the specific respository classes.

each database will have a repository class, being responsible for dealing with that database.

therefore if you had a customer class (likely a DTO at repository level), with a property called Name, then each repository is responsible for reading the "name" field from there respective table and then mapping it to the DTO name field, you shouldn't have a different domain level customer class for each database, thats just nuts...

If you are using dependency injection you can inject these repositories, and safely remove them one by one when they are not needed...

If you need transaction support then you can add another layer in the repositories and roll your own transaction coordinator etc.

the issue with @Sheridan example above is as you add and remove databases that method keeps changing, its not closed for modification ...

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.