Anyway way to use aliases to share boilerplate code involving different assemblies?

K

Kevin Frey

I'm facing a really annoying dilemma with C# (me being a C++ programmer).

We have a number of assemblies that all possess the same general structure
in terms of their class makeup.

In other words, we might have assembly A1, A2, A3, and each of these
assemblies with have a class called Session, for example. Each assembly
contains these classes in a separate namespace (eg. A1Namespace,
A2Namespace, A3Namespace).

These assemblies are then usually associated with different projects.

We might then have a source file which, aside from requiring a different
"using" statement on account of the above namespace differences, is
identical across each project.

So, we have what is effectively "boilerplate" code that would be identical
amongst the different projects if not for a different using directive.

It annoys me no end that I cannot "externalise" in some way the aliasing of
namespaces so that the build tool defines it, allowing me to retain a single
copy of the boilerplate code.

I thought the extern alias behaviour might help me, but if I use that on a
given assembly I still need to qualify within that alias namespace the
actual inner namespace for the particular assembly (eg.
myexternalias.A1Namespace, myexternalias.A2Namespace, etc).

If the build tool allowed me to define an alias that reference any other
namespace, then that would be a perfect solution for me, but it doesn't, and
so I am now faced with this unnecessary code duplication. In C++ I could
achieve a solution using a generic namespace name (eg. MYNAMESPACE) which
the build tool defined (ie. a build configuration setting, rather than
explicitly in code).

Does anyone know a way around this?

Thanks

Kevin
 
A

Arne Vajhøj

Kevin said:
I'm facing a really annoying dilemma with C# (me being a C++ programmer).

We have a number of assemblies that all possess the same general structure
in terms of their class makeup.

In other words, we might have assembly A1, A2, A3, and each of these
assemblies with have a class called Session, for example. Each assembly
contains these classes in a separate namespace (eg. A1Namespace,
A2Namespace, A3Namespace).

These assemblies are then usually associated with different projects.

We might then have a source file which, aside from requiring a different
"using" statement on account of the above namespace differences, is
identical across each project.

So, we have what is effectively "boilerplate" code that would be identical
amongst the different projects if not for a different using directive.

It annoys me no end that I cannot "externalise" in some way the aliasing of
namespaces so that the build tool defines it, allowing me to retain a single
copy of the boilerplate code.

I thought the extern alias behaviour might help me, but if I use that on a
given assembly I still need to qualify within that alias namespace the
actual inner namespace for the particular assembly (eg.
myexternalias.A1Namespace, myexternalias.A2Namespace, etc).

If the build tool allowed me to define an alias that reference any other
namespace, then that would be a perfect solution for me, but it doesn't, and
so I am now faced with this unnecessary code duplication. In C++ I could
achieve a solution using a generic namespace name (eg. MYNAMESPACE) which
the build tool defined (ie. a build configuration setting, rather than
explicitly in code).

Does anyone know a way around this?

You can not do the same thing as in C++.

You can use reflection yourself or you can use a DI/IoC framework
to do it for you.

I think that would be the .NET way.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top