I need help architecting a data access layer using Entity Framework. Here are the requirements:
- The database is preexisting, an absolute mess, and it's structure and cannot be altered
- Entity Framework will be used
- The database will be used by dozens of .NET MVC applications each with their own set of business objects, logic, and read/write permissions.
- The data objects required by these applications usually bear little resemblance to the tables stored in the database.
- The data access layer needs to be as simple to use as possible for the developers using it.
I've never worked on anything of this scope before and would appreciate any help on how to architect this solution. What I have so far is:
Project 1: DataLayer
• contains a purely EF-generated data model
Project 2: BusinessLayer
• contains logic to convert objects from the DataLayer into business objects used by the views and vice versa. Most (but not all) logic is shared amongst applications. • Contains common DTO / business objects used by all or multiple applications
Project 3: ServiceLayer.Application1
• contains all the DTO / business objects for Application 1 • contains a repository to be used by Application 1
Project 4: ServiceLayer.Application2
• contains all the DTO / business objects for Application 2 • contains a repository to be used by Application 2
Say an MVC application using the data layer needed to get back an IQueryable, and the business object FacultyFormattedForAView consists of data from 6 different tables with some business logic thrown on top. Here is how I envision things going:
- The application creates an instance of their respective repository in the service layer and calls IQueryable GetAllFacultyFormattedForAView().
- The service layer makes a call to the business layer: IQueryable GetAllFaculty(). The Faculty business object consists of data from 4 different tables.
- The business layer hands back a LINQ statement converting objects from the generated EF classes living in the data layer to a Faculty
- The service layer converts the Faculty objects into FacultyFormattedForAView objects and returns the result to the UI
Using this structure, the common palette of commands would exist in the business layer to avoid any code duplication and the repository would only expose the commands that a particular application has access to and provide additional formatting specific to that application.
I am using IQueryable rather than IEnumerable so paging and sorting can be used. Seeing how I'm only exposing the business objects / DTO to the developers, I figured this was ok although I would like it if they wouldn't have to do a ToList() or anything in order to actually execute the query.
This is the best I could come up with but I can't shake the feeling I could be doing something better.
I would appreciate any and all feedback.