Question

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#?

Was it helpful?

Solution

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.

OTHER TIPS

What you're trying to achieve is not possible. The closest thing you will get is a using alias directive which looks like this:

using User = Myproject.Core.Db.User;

This will remove the need to fully qualify the path for Myproject.Core.Db.User. You will still need to specify the fully qualified path for at least one of the classes, though. You could create another alias for the other type as Servy demonstrated but at this point I would just rename the classes.

I think the real solution here is to give your classes more descriptive identifiers.

C# does support relative namespace references.

In your case, that means if you're in the namespace MyProject.Core, you can references your classes as Db.User and Model.User. But if you're in the namespace MyProject.BLL, you have to include the Core prefix (Core.Db.User and Core.Model.User).

If that's not good enough for your and you don't want to change your namespace structure, your best choice is probably to add usings to all files that use the types in question.

using DbUser = MyProject.Core.Db.User;
using ModelUser = MyProject.Core.Model.User;

You can add an alias for the one class that you don't import the namespace of:

using MyProject.Core.Db;
using ModelUser = MyProject.Core.Model.User;

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

In C# it's not possible to use the example that's shown; it's simply not a supported feature.

One thing you can do, and we probably should do a lot more, is to use namespaces relative to the current namespace. To do this, just move your usings inside the namespace declaration. It doesn't fix your stated problem, but it's the only way of referencing that lets you move folders around with impunity.

namespace MyProject.Core{
    using Db;
    using Model;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top