vendredi 22 janvier 2016

Hydrating Domain Model from multiple databases

I have an application with layered design as follows.

Project.Common      // Domain models
Project.DataAccess  // Repositories, Persistence models
Project.Services
Project.Web         // Presentation Layer

Services project uses UnitOfWork class (defined in DataAccess project) to query database. Repositories in DataAccess return domain models defined in Common project. UnitOfWork class takes connection enumeration to connect to different databases.

I come across an issue when I need to populate a domain model for which the data comes from different databases (different db servers). How would I do this?

It seems I would have to query separate repositories, with each repository returning a domain object (not persistence object since it is exposed to Services layer). And then build a more complex domain object from simpler domain objects in Services layer. Is this correct approach?

Sample code in Services layer to query database.

using (var unitOfWork = new UnitOfWork(DatabaseConnection.MyDbServer1, requireTransaction: false))
{
    var repo = RepositoryFactory.GetRepository<IMyRepository>(unitOfWork);
    IEnumerable<MyDomainObject> output = repo.GetData();
}

Aucun commentaire:

Enregistrer un commentaire