2

How do I create an external DAL to my MVC project ?

There is a one thing I really do not get. Suppose I have a model in my MVC project. Model's name is a Person {int id, string name}.

Now in my DAL I have a method:

public Person Select(int id) - which loads data from db and returns the Person object.

This works fine if the DAL is a part of the MVC project. However when I create a class library for my external DAL (so I can use it in another application without referencing whole MVC project) it will not know the Person model because all models are hidden inside MVC project.

I can't reference MVC project from DAL because MVC project needs to reference DAL to use it. I'm not interested in Entity Framework I want my own solution (not sure if EF even solves this).

Is there a secret way to do it ? Or is my approach (of passing whole model) wrong and my Select method should return some sort of array or list with attributes ? I believe this is table data gateway concept (not sure), when just attributes are passed not whole objects.

5 Answers 5

2

You need to define your model classes inside of your Data Access Layer project. This way they will be available to both the DAL project (because they are in the project) and the MVC project (because it references the DAL project).

Entity Framework would take care of this but only due to the fact that the generated entity models become part of / same location as the .edmx file that EF would generate.

You should be able to essentially drag/drop your models folder into your DAL. However, take care as your Namespaces will be broken and you will need to update the namespaces in each file. Additionally, you will need to add the appropriate references anywhere in your MVC project in which you use one of your models. For example, if your models are in the Data.Models namespace, you would need the following at the top of your controller:

using Data.Models;
Sign up to request clarification or add additional context in comments.

2 Comments

Surely declaring models as a part of DAL is a way but doesn't it break the MVC pattern ? Just asking...
@Safiron - where you declare your classes doesn't having any affect on the MVC pattern. I could have a project for every single model if I wanted, and outside of a really slowly compiling project, it would still be MVC. Can you explain why you think it would break?
1

Your question states:

"when I create a class library for my external DAL (so I can use it in another application without referencing whole MVC project) it will not know the Person model because all models are hidden inside MVC project."

I will admit that the wording you chose does not make sense to me. If you have a public method on your DAL, the models can't be "hidden" - they have to have the same [public] accessibility as your method. POCO objects should be defined in the most-common assembly in your application so they can be shared throughout. If your code is loading data through a DAL, presumably it is using the models returned by the DAL - so the DAL is the most logical place for those. The basic premise is that you want to keep your projects as simple as possible.

If you have a scenario where parts of the application might never touch the DAL, yet you still want to have your models present, then I recommend one of two strategies:

  1. Create a separate "models" project and reference wherever models are required (best when you have an extensive object model set).
  2. Use the VisualStudio "Add as Link" to import the model files into the project(s) where they are used (if you have relatively few models and few scenarios where they will be used independently of DAL)

2 Comments

Sry for that. it was just a figure of speech. By 'hidden' I did not mean private I meant it is not known in DAL project because it is declared in MVC project. Declaring Models in DAL project is surely a way but I was just not sure if It does not break the MVC pattern.
My understanding of MVC (N-tier architecture) is that you want your models to be ubiquitous. So putting them somewhere that isn't ubiquitous is a violation of the pattern. :)
1

Add model classes in DAL. Create ViewModel project and add class PersonVM with properties of person defined. populate PersonVM in your select method and add the reference of Viewmodel project in DAL((ideally a business layer project) and MVC application. Use Viewmodel class in View instead of model

Comments

0

The whole POCO class needs to be in your DAL. THe only thing that should be in your MVC model is the mapping from DAL object to your View:

    private WorksiteAddress _worksiteToEdit { get; set; }
    public WorksiteAddress WorksiteToEdit 
    {
        get 
        {
            if (_worksiteToEdit != null) return _worksiteToEdit;

            if (WorksiteId != null)
            {
                using (var db = new YourDALContext())
                {
                    _worksiteToEdit = db.WorksiteAddresses.FirstOrDefault(x => x.Id == WorksiteId);
                }
            }

            if(_worksiteToEdit == null) _worksiteToEdit = new WorksiteAddress();

            return _worksiteToEdit;
        }
        set { _worksiteToEdit = value; }
    }

    [Required, DisplayName("Address")]
    public string StreetAddress
    {
        get { return WorksiteToEdit.StreetAddress; }
        set { WorksiteToEdit.StreetAddress = value; }
    }

    [Required]
    public string City
    {
        get { return WorksiteToEdit.City; }
        set { WorksiteToEdit.City = value; }
    }

    [Required]
    public string State
    {
        get { return WorksiteToEdit.State; }
        set { WorksiteToEdit.State = value; }
    }

    [Required, DisplayName("Zip Code")]
    public string ZipCode
    {
        get { return WorksiteToEdit.ZipCode; }
        set { WorksiteToEdit.ZipCode = value; }
    }

The Worksite object in the above example should exists totally in your DAL (including a SaveOrUpdate() method that you call from this model when you need to save it)

2 Comments

What does this accomplish?
1) Prevents data injection by hackers 2) Separation of concerns
-3

-On you DAL project use nuget to add ASP.NET MVC -add Controllers,Models and Views folders on your DAL project -Now reference DAL project to your main project

1 Comment

Why oh why would you install MVC on a non-web, data layer project? Then move the entire project into the DAL in order to reference that back to the MVC project?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.