3

I have a class library which contains all of my Entity Framework code.

I'm developing an solution that involves several sub-projects, including an ASP.NET MVC project. My model has been separated into a separate assembly because I need to use it from the various other projects in the solution.

I have divided my solution into three tier:

1) `DAL` (Data Access Layer Entity Framework .EDMX) DATABASE First approach...
2) `Service` layer (will be calling DAL to get data from db....)
3) `UI` (which will be MVC 4 framework)

So in my Service project I have reference of Data Access dll

in ASP.NET MVC Project I have reference of Service project

However, I am now starting to build a ASP.NET MVC.

I tried adding a controller and selecting Model class from my DAL received an error

Error 1 The type 'myproj_dal.requester' is defined in an assembly that is not referenced. You must add a reference to assembly 'myproj_dal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. \issoa_ui\Controllers\RequesterController.cs

here is my controller looks like:

public ActionResult Index()
{
    issoa_service.RequesterService reqService = new issoa_service.RequesterService();
    var model = reqService.GetRequesters(); //**<<<ERROR**
    return View(model);
}

In my Web.Config I have connection string exactly that I have in my DAL app.config.

Here is my RequesterService looks like this:

public class RequesterService
{   
    db_entities edmx = new db_entities();

    public IList<Requester> GetRequesters()
    {
        IList<Requester> model = edmx.Requester.ToList();
        return model;
    }
}

Model.edmx
>>>>>Model.tt
       >>>Requester.cs


//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace myproj_dal
{
    using System;
    using System.Collections.Generic;

    public partial class Requester
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string MiddleName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
    }
}

my question is:

1) Why I'm getting the above error and do i need to add reference of DAL to UI? if yes then i am defeating the whole purpose of loosely coupled.

2) What is the right way of doing; if I'm not doing based on the best standards.

Can anybody please point me or correct me or shout at me or redirect me to some kinda explanation or a sample project will help.?

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    To do this, you would need to add a reference to your DAL in your web project. Of course you would have to do this if you want to use a class from one project in another project. If you want to avoid coupling them, why not have a separate project for your entity models, which you reference from both projects that need it? – Ant P Sep 12 '13 at 21:00
  • You saying add DAL ref to my MVC project? thats only option i have to work around? – Nick Kahn Sep 12 '13 at 21:07
  • If you want to use a class from one project in another project, you have to reference it. Period. That's the whole point of references. – Ant P Sep 12 '13 at 21:08
  • I did create separate project for entity models please read my question – Nick Kahn Sep 12 '13 at 21:08
  • yes i understand about the ref, DAL ref is in my SERVICE then why should i have DAL ref in Web project? that part i am not understanding – Nick Kahn Sep 12 '13 at 21:09
  • Because you're trying to use a class from your DAL in your web project? `model` is a `IList` and `Requester` is defined in your DAL. – Ant P Sep 12 '13 at 21:10
  • no no i am trying to use Service class from my web project, see my controller how i am trying to access – Nick Kahn Sep 12 '13 at 21:11
  • And what is the type of `model`...? – Ant P Sep 12 '13 at 21:12
  • There is no such thing as an "implicit type." Implicit typing is just shorthand - `model` is still strongly typed to `IList` at compile time, the type is just inferred from the right-hand side of the assignment. `var` is not dynamic. – Ant P Sep 12 '13 at 21:16

1 Answers1

3

If you don't want to reference your DAL from your web project, you need to take the entity models out of your DAL and put them in a separate project.

Create a project called MyProj.Entities and reference this from everywhere that needs it (i.e. all of your other projects). Put Requester and other entity classes inside this project.

Now your web project no longer has to be tightly coupled with your DAL but you can still share your entity classes between them.

Ant P
  • 24,820
  • 5
  • 68
  • 105
  • am I not doing the same? when i say i create a DAL project and in it i have all my entity models classes? I'm confused now... can you throw me few lines of sample code demonstrating? – Nick Kahn Sep 12 '13 at 21:20
  • hmmm... you talking about creating POCO classes? in a separate project? – Nick Kahn Sep 12 '13 at 21:24
  • Yep. Create POCOs in a separate project, return these from your DAL and reference your POCO model project from each layer that needs it. Then you can pass objects between each layer without tightly coupling them. – Ant P Sep 12 '13 at 21:26
  • This is most likely the best solution. It provides a way to abstract your DAL and Service layer, so if you decide to not use Entity Framework you simple just need to update the DAL, but nothing else should really need to be changed because all the entities are consistent. – Erik Philips Sep 12 '13 at 21:42