views:

39

answers:

2

Hi,

There is a legacy application in my company which is still in use. I am in the middle of porting it to asp.net (using mvc framework) and also have to create new functionality. Because it's in use I have to develop it on top of existing database, which was developed really badly (not all relations exist, triggers used instead of relations, lots of fields are not used any more) So I plan to create a new database later. I am planning to make a different EF model, but to keep all the namespaces and classnames the same, is it right? What if i would like to switch it back? How to organize my code, model, ... etc. to make switching to different database easier. I would really appreciate any help.

EDIT To be more precise, I would like to make my application to run on both old and new databases. Keep in mind that the schema of the 2 dbs is different

A: 

I have a similar situation where I had a non-ef Oracle based application. I switched it to sql server EF based database system and rebuilt the application on top of that. What I would do as you are porting to a new framework (ASP MVC) is also to start again with the database as well. What you can do is to write c# code to do imports from old database to new database, doing any conversions between fields etc as part of the import process. When I did my conversion I had a nightly import to the new system from the old system which allowed the users to get up to speed with the new system as they could see existing data.

Calanus
I actually thought about it, but the problem is that the data inputted into the new database should also come to the old one. So if I use this method I will have to implement an export feature as well, or make some kind of synchronization
kalan
Yes you probably will have to do some sort of synchronization, but should be possible to find out the most recent data by comparing timestamps.
Calanus
+1  A: 

If you want to take two different databases and treat them as if they were the same database, insofar as your application is concerned, then you need a layer of abstraction between them that hides the differences.

I think you have a couple of options here. One would be to use two different EF models and then you see repository pattern to abstract the differences between them away. A second option would be to do some sort of conditional mapping.

You have not been very specific about the differences between the databases, so it's difficult to give specific examples of each option, but the general idea for the first is that you write an interface representing the storage, like this:

interface IAnimalRepository
{
    IQueryable<Animal> SelectAll();
    Animal Insert(Animal newRecord);
    // etc.
}

Now you write your service/business layer against this interface, rather than directly against the context.

You can implement the interface for, say the "old" EF mapping, like this:

class AnimalRepository: IAnimalRepository
{
    private OldEntities Context { get; set; }

    public AnimalRepository(OldEntities context)
    {
        this.Context = context;
    }

    IQueryable<Animal> IAnimalRepositorySelectAll()
    {
        return from oa in Context.OldAnimals
               select new Animal  // Animal is a POCO
               {
                   Id = oa.Id,
                   Name = oa.Name
               };
    }

    // etc.
}

The second option is to do a conditional mapping. In this case you would use the new Code First functionality which is currently in CTP for Entity Framework 4, which allows you to specify table and row names at runtime.

Of the two options, I would probably choose the repository pattern option, because I think you'll want to use the repository pattern anyway. It makes unit testing and other concerns much simpler. But either one will work.

Craig Stuntz