Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do relative C# namespace reference?

I have namespaces:

MyProject.Core.Db
MyProject.Core.Model

And I have classes:

MyProject.Core.Db.User
MyProject.Core.Model.User

Is it possible something like:

using MyProject.Core;

namespace MyProject.BLL
{
    public class Logic
    {
        public static void DoSomething()
        {
            var userEntity = new Db.User();
            var userModel = new Model.User();
        }
    }
}

I just want to avoid using suffixes in class names (UserModel, UserEntity).

Is it possible to do in somehow in C#?

like image 739
Zelid Avatar asked Sep 18 '25 05:09

Zelid


1 Answers

I don't understand why people say it's not possible. Surely it is possible, you just need to be a bit more specific in the namespaces when you create the target classes (ie you can omit only the common part of the namespace):

namespace MyProject.Core.Db
{
    public class User
    {
    }
}

namespace MyProject.Core.Model
{
    public class User
    {
    }
}

namespace MyProject.BLL
{
    public class Logic
    {
        public static void DoSomething()
        {
            var foo = new Core.Db.User();
            var boo = new Core.Model.User();
        }
    }
}

The way you're avoiding a fully qualified name within BLL is by being inside of a common namespace with the other two.

like image 143
andreister Avatar answered Sep 20 '25 19:09

andreister