0

I am using Entity Framework, Linq, WCF and MVC3. The system has been split into a tier approach and I am using WCF because multiple systems will be using the same Data Access and Business Logic.

Data Access – Business Logic – API (WCF) – Service Classes – MVC

For each View in my MVC application, LINQ queries need to be wrote which access different tables in the database and show summaries as seen below.

        var users =
           (from u in _userRepository.All
            from upd in u.UserPropertyData.DefaultIfEmpty()
            from upd1 in u.UserPropertyData.DefaultIfEmpty()
            where (upd.UserPropertyType.Alias == "Forename") && (upd1.UserPropertyType.Alias == "Surname")
            select new UserModel
            {
                Id = u.Id,
                Forename = upd.Value,
                Surname = upd1.Value,
                EmailAddress = u.Email
            });

This query is in my Business Logic section and is passing a UserModel across my WCF API to my Service class which passes the Model to my Controller which is then displayed in a View.

This approach doesn’t seem correct because the Data Access and Business Logic are being coupled to MVC by passing back a ‘View Model’. If anyone can think of a better approach I would appreciate it.

ministrymason
  • 1,783
  • 2
  • 20
  • 33

1 Answers1

0

If I understand it correctly, you're doing your data access through WCF? If so I would return the business data (classes) through the WCF services, do the mapping to ViewModel classes via the services classes of the MVC app and then pass all the view models through to the controllers/views. Please correct me if I've misunderstood your question.

Duy
  • 1,332
  • 1
  • 10
  • 19
  • Do you mean return the selection from the linq query above through WCF services. Linq queries cannot be sent through WCF can they? For example I cant build the query up on the client side? – ministrymason Oct 10 '11 at 10:22
  • Yes wrap all your data access in the WCF services. That way all your systems can just consume these wcf services to retrieve sata from the db, how to use/manipulate the data will be entirely up to the system (in the mvc app case, data would be mapped to view models for displaying on the views) – Duy Oct 10 '11 at 10:45
  • ok. But for some of the pages the data received through the WCF services will be the same data that is being displayed in the views as a 'View Model'. Just doesn't seem correct getting a 'View Model' through WCF – ministrymason Oct 10 '11 at 10:52
  • In those cases, you don't need to map your business entities to a 'view model', just pass them straight onto your controller/views. As I said above, your WCF services will query the DB and return business entities to whatever applications/systems that consume them. – Duy Oct 10 '11 at 11:00
  • Ok thanks for your help. Thats the approach im using. Just seemed like I am coupling the data access to the MVC app by receiving the business entities as 'view model' – ministrymason Oct 10 '11 at 11:06