0

Okay this is driving me crazy. I got one almost finished project (which works perfectly) and I wanted to make another one in the same way. The thing is there is a solution with two layers DataAccessLayer and BusinessLogicLayer. Both of these layers have a Model library with all models in the project. I need to convert the model from the first layer to a model of a second layer in the manager library. This works in the finished project I received but I can not manage to make it on mine. The thing is I can't make the necessary references to work like they do on the finished project. The structure is:

BusinessLogicLayer

--Managers

----Users

--Models

----User

DataAccessLayer

--Models

----User

In the Managers project I have a reference added to DataAccessLayer.Models. And inside the Users class I got:

using Library.BusinessLogicLayer.Models;

Now in my project this line is red underlined:

Error CS0234 The type or namespace name 'Models' does not exist in the namespace 'Library.BusinessLogicLayer' (are you missing an assembly reference?)

I am not even sure how and why this works on that original project. But I can't figure it out so it's working right on my project and the structure is the exact same. Anyone have an idea about this?

EDIT: Dunno why I didn't upload this earlier. Here is the structure. https://i.stack.imgur.com/w4JSS.jpg

EDIT2: Since it is not quite understandable I uploaded the whole project on github so you can take a closer look at it. https://github.com/Morsusy2k/Library

And here is the problem: https://i.stack.imgur.com/J4ng6.jpg

Morsus
  • 107
  • 1
  • 16
  • 1
    Make sure you have `Rebuild` your project. https://stackoverflow.com/a/36655884/2946329 – Salah Akbari Oct 06 '17 at 18:08
  • Rebuilt a lot of times even removed bin/obj folders, cleared solutions, rebooting PC, etc.. – Morsus Oct 06 '17 at 18:29
  • Are BusinessLogicLayer and DataAccessLayer just solution folders that have projects in them? – George Birbilis Oct 06 '17 at 18:55
  • maybe you need to do "using Library.DataAccessLayer.Models" assuming you do use such Namespace definition in your source code at your Library.DataAccessLayer.Models assembly, or however you name that project. Is there a chance that you make use of the default namespace setting of the project? Better avoid using such a thing and have explicit namespaces in your code. Can cause issues if you rename stuff and forget to change that one, or if you forget to clean/rebuild after changes – George Birbilis Oct 06 '17 at 18:58
  • You need to update the `namespace some_namespace.Layer.Folder { }` in every file to your current locations wherever VS alerts as namespace not found. – Tiramonium Oct 06 '17 at 19:15

2 Answers2

2

From what you described above and from my understanding, it seems that Managers and Models are two different projects. If that is the case, make sure that you add a reference to BusinessLogicLayer.Models in your BusinessLogicLayer.Managers.

If, on the other hand, you have only two projects BusinessLogicLayer and DataAccessLayer then it could very well mean that Library.BusinessLogicLayer.Models is not the name of the namespace.

UPDATE

From the picture that you added, you might need to add a reference to Library.BusinessLogicLayer.Models.Models. You have a folder named Models and a project named Models. Visual Studio automatically generates namespaces based on the Solution name, Solution folders, project name, folders within project.

There were three issues with your code. The first one is that you are supposed to add a reference to Library.DataAccessLayer.Models and not to Library.BusinessLogicLayer.Models. This is due to the fact that you have User in DataAccessLayer.Models and User2 in BusinessLogicLayer.Models.

The other two issues were with the Map method where you are sending incorrect number of arguments to the constructor (you are missing UserId) and the other issues is with your DateOfBirth and DateJoined being in the wrong order in the same method.

using System;
using System.Collections.Generic;
using System.Linq;
using global::Library.BusinessLogicLayer.Models;
using Library.BusinessLogicLayer.Managers.Properties;
using Library.DataAccessLayer.Models; // <-- Add reference to this

namespace Library.BusinessLogicLayer.Managers
{
    public class Users2
    {
        public IEnumerable<User> GetAll()
        {
            using(DataAccessLayer.DBAccess.Library library = new DataAccessLayer.DBAccess.Library(Settings.Default.LibraryDbConnection))
            {
                return library.Users.GetAll().Select(user => Map(user));
            }
        }

        private User Map(DataAccessLayer.Models.User dbUser)
        {
            if (dbUser == null)
                return null;

            // TODO: Constructor is missing a paremeter. I'll add a temporary one
            int tempUserId = 0;
            User user = new User(tempUserId, dbUser.Name, dbUser.UserName, dbUser.Password, dbUser.Email, dbUser.DateJoined, dbUser.DateOfBirth) // <-- The last two params are in the wrong order
            {
                Id = dbUser.Id
            };

            return user;
        }

        private Library.DataAccessLayer.Models.User Map(User2 user)
        {
            if (user == null)
                throw new ArgumentNullException("user","Valid user is mandatory!");

            return new DataAccessLayer.Models.User(user.Id,user.Name, user.UserName, user.Password, user.Email, user.DateJoined, user.DateOfBirth);
        }
    }
}

Also, regarding the last screenshot that you provided, you do not have Library.BusinessLogicLayer.Models2 namespace. Remove number 2 to get it to work.

As I don't have permission to update your repo with the fixed code, you'll have to fix it manually based on my answer. Otherwise, let me know so that we see how I can push the code back.

Community
  • 1
  • 1
Huske
  • 9,186
  • 2
  • 36
  • 53
  • 1
    I think the issue is that they add reference of Models from BusinessLogicLayer, but then try to use Models from the DataAccessLayer instead – George Birbilis Oct 06 '17 at 19:01
  • @GeorgeBirbilis, yes, this could be the third scenario. – Huske Oct 06 '17 at 19:02
  • Also, exactly that line is error in my code. The program cant find the reference. – Morsus Oct 06 '17 at 19:03
  • a screenshot from the Object Browser to see the namespace might also help - as I was writing at other comment you seem to be adding a reference to the wrong Models library (other one than the namespace you're trying to use) - that is assuming the namespaces used are similar to the structure used for the projects/folders – George Birbilis Oct 06 '17 at 19:07
  • @GeorgeBirbilis I would love for you to see the new update in the main post. If everything else fails until tomorrow I will recreate the whole project from scratch, I hope that will fix this crazy issue. Thank you (: – Morsus Oct 07 '17 at 00:13
  • @Morsus, you were trying to reference incorrect project. Have a look at my answer. – Huske Oct 07 '17 at 06:11
  • Sorry I can't figure it out. I am still kinda new to all of this. I do have a reference to BusinessLogicLayer.Models but it still fails to see it in the using line. As for Map and other methods I figured I would do those later when I fix all this mess. I could send you the collaborator invite if I knew your username or email. – Morsus Oct 07 '17 at 11:39
  • 1
    No, you need to add a reference to `DataAccessLayer.Models`. Yes you can send me an invitation so that I push the changes. – Huske Oct 07 '17 at 11:41
  • 1
    I contacted the guy who wrote the working example. He forgot to mention that I needed to manually edit .csproj and rename one of the references so I could add another.... -.- Thank you for all the trouble. I fixed it. – Morsus Oct 07 '17 at 13:28
0

There are a few things you could try:

  1. This error could be appearing within BusinessLogicLayer because DataAccessLayer failed to build. Try building DataAccessLayer by itself to see if you get a different error.
  2. The references might be "added", but you might not be referencing the correct DLL or version for some reason. Check your .csproj files manually to ensure all references and versions are correct, have the right hint paths, etc. If you have any config files, you should also review them to ensure there are no version conflicts.
  3. In addition to checking the references, it is possible to add if-then and switch case logic inside of the .csproj files. This is an MSBuild feature that Visual Studio doesn't support through its GUI, so you may need to copy/update this logic manually in your current .csproj files if any existed.
  4. Check your default namespaces in project properties to see if they are the same as your old project. If you added new files since you moved to this project, they may have been inadvertently added with the wrong namespace and that namespace may be causing a conflict. You could also try using global::Library.BusinessLogicLayer.Models; to see if that fixes or changes the error message or at least if Intellisense is picking the namespace up.
  5. If that doesn't work, review all of your namespaces in all .cs files to see if you have any that have gone rogue.
  6. Since your Models namespace has the problem and you have 2 of them with the same name, try temporarily renaming one of them (yes, every .cs file in one of the projects) to Models2 to see if it provides a clue (such as the error going away or changing).
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I checked all .csproj and they seem fine. Checked default namespaces (wich I previously copy/pasted) they are all fine too. All classes namespaces are also fine. I also added the global part, same result. The thing is intelisense sees only Managers project and not Models too. – Morsus Oct 06 '17 at 19:59
  • I would love if you would take a look at the second update in the main post. I uploaded the project to git so you could take a better look. Thank you (: – Morsus Oct 07 '17 at 00:16