Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Context

Web application based on the .NET Framework written in C# and following the MVC design pattern.

Question

What is the most efficient way to structure an entity layer consisting on various data inputs?

Background

I am developing an application for a client that requires my reading and writing data from various sources and I would like to build a single entity layer/model to abstract the various data sources so that I only need to query the unified model from code. Here is an example of the disparate sources:

  • MS SQL Data Warehouse (about 200 tables of enterprise-wide data
  • MS Dynamics AX web services
  • A new MS SQL database that is specific to the application being developed
  • MS Active Directory for authenticating users and for getting user profile and group information.
  • In some cases, SharePoint web services.

I have used ADO.NET a good bit (.edmx files and so forth) and fundamentally, I know how to do this but I am struggling a bit with a solid, clean approach. Do I create two separate .edmx files for the two SQL DBs and then write object classes for the Active Directory, SharePoint and AX stuff? Do I simply write one big model file? A whole separarate project just to make the abstraction?

Many thanks!

share|improve this question
    
See this question: stackoverflow.com/questions/7109942/… - One EDMX for multiple sources isn't feasible. Therefore your goal of having a single integrated model is not achievable with Entity Framework. At best you could have a single assembly that knows how to work with multiple data sources - but this is going to involve you hand-building the integration. EF can't really help you with that. –  Joel Brown May 23 '12 at 3:55

3 Answers 3

up vote 1 down vote accepted

It sounds like you are trying to model a set of disparate data sources as a single model.

If that is indeed the case, my opinion is that creating a project to make the abstraction is the way to go.

I say that because you are dealing with different TYPES of sources. For example, if you just had two databases you could put a link between them, and abstract that way.

But, you have a data warehouse, a few web services, and Active Directory to deal with. I don't know if there would be one way to consolidate across all of those, and even if there were, it could be messy.

Putting the abstraction in a separate project also ensures that you only have to change ONE place when your entities change. It also ensures that you hide the details of what is going on behind the scenes from your front end. This can be important, particularly when you have to add something else (not if - with that many sources something else will surely come along later).

share|improve this answer
    
Thanks Alan. This is sound advice. I am going to leave the question open for about 24 hours so that further input is not discouraged, but I think you are right. –  Matthew Patrick Cashatt May 22 '12 at 20:08

Having a separate assembly for the Data Access is a good idea in this case and it makes it easier to point a separate testing project at it to more cleanly test your data access layer. You can develop the entity models as plain classes and then use services to populate them. You may want to take a look at Entity Framework Code First for the database access as it will get away from the .edmx dependency and keep your entity classes cleaner.

There is a tutorial by Scott Guthrie on using Code First with existing databases.

share|improve this answer

I have previously used the repository model in a project that sounds very similar to this. I captured entities as plain classes and then for the entities that were stored in SQL I used Entity Framework Code First to generate the tables in SQL for me. Also, if your information will be stored in multiple databases you will just need a DBContext class for each datasource. Using the repository model will allow you to point to any source to populate each repository of entities.

As far as web services, I generally split these out into another project to make a physical division between a service layer and a data layer.

So in this scenario I would have 2 projects. One for the data layer (this would implement the repository pattern, include related entities, and handle CRUD to datastores using EF or ADO.Net), and one for the service layer (this will talk to AD service, SharePoint services, and any other services. It will also contain the related entities for these services).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.