So i have a program that is a 3 tiers program, the UI, the BLL (Business Logic Layer) and of course the DAL (Data Access Layer). The UI always speaks to the BLL of which uses the DAL to retrieve data units, assembling them in a format and returning them to the UI.
Since this is for my job i want to make sure that conventions are ALWAYS used (not a big fan of everyone programming their own style wherever they want!). So i thought it would be nice to have something like this.
using (Bll.MyService service = new Bll.MyService()) {
//My Sweet Code
}
BUT!!! If the Bll is located in a namespace that my UI is not, this will not be an option. Here is a more lenghty example of what i mean.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyApp;
//Entry point
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//I want to be able to do a using MyApp and access the Bll objects!
Bll.ApiService service = new Bll.ApiService(); //ERROR LINE
}
}
}
//Data access layer
namespace MyApp.DAL
{
class ApiDataAccessor
{
public int MyVar = 1;
}
}
//The bacon letuce layer of course
namespace MyApp.Bll
{
class ApiService
{
public void MyFunction()
{
//todo: make more awesome
}
}
}
If anyone has any suggestions that would be great! Thanks!
EDIT:
Added the //ERROR LINE to a comment in the code so to make the error obvious. I also found a hack. using Bll = MyApp.Bll which will enforce the Bll.ApiService to be used. I do not know if i like this solution (as it easy to mess up and be angry until i realize that i did not alias the namespace).
EDIT (Again):
There are a few solutions.
-
using Bll = MyApp.Bllat the top and then all objects in that namespace have to be referenced withBll.MyObjectwhich is what we wanted! -
Require fully qualified names.
MyApp.Bll.MyObject. Which is what we did not want (as it can get verbose with large namespaces) -
Just include the namespace up top.
using MyApp.Bll.
In summary, we were hoping to get using MyApp in some way to allow us to reference all those namespaces and their objects like Bll.ThisObject and Dal.ThatObject but instead, if that were a solution desired, the only way to accomplish that would be to include 2 using statements, which are aliased. using Dal = MyApp.Dal and using Bll = MyApp.Bll
Thanks everyone for your help.
Here is the solution
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyApp;
using Bll = MyApp.Bll;
//Entry point
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
//I want to be able to do a using MyApp and access the Bll objects!
Bll.ApiService service = new Bll.ApiService(); // <-- it works.
}
}
}
//Data access layer
namespace MyApp.DAL
{
class ApiDataAccessor
{
public int MyVar = 1;
}
}
//The bacon letuce layer of course
namespace MyApp.Bll
{
class ApiService
{
public void MyFunction()
{
//todo: make more awesome
}
}
}
I think the confusion lies here: you don’t have a namespace called
Bll. You have a namespace calledMyApp.Bll.Regardless of having a
using MyAppdirective, you need to either add ausing MyApp.Blldirective and then just referenceApiServicedirectly in your code, or fully-qualify the class asMyApp.Bll.ApiService.If all you’re after is the ability to refer to the
MyApp.Bllnamespace with the symbolBll, you can alias the namespace:using Bll = MyApp.BllAlso, based on comments on the question and various answers, it’s worth pointing out that the using statement, which provides a convenient way to declare and clean up unmanaged resources, and appears as a block in your code is not the same as the using directive that imports namespaces.