What to do for elements with the same name

I have a little problem with one of my projects, I have an object called Image that is used to store information about uploaded images in the CMS system that I created.

This hides system.drawing.image

, it wasn't a problem until I had to start doing image manipulation on a page that uses both classes.

Currently, there are declarations, Using Image = myCMS.Entities.Image;

so when calling Image I use my object, but when calling system.drawing.image

I need to typesystem.drawing.image

What is the best approach to take advantage of such problems? As I didn't think about Framework class names when developing this application.

+2


source to share


5 answers


Aside from changing the name of your object, your options are really limited to name-aliasing and / or completely qualifying two colliding names, as you already do.

My personal preference will match the type names perfectly throughout the source. Your current approach is a mix of both aliases and qualifications. You can also alternatively use both types:



using EntityImage = myCMS.Entities.Image;
using DrawingImage = System.Drawing.Image;

      

+4


source


While it hurts a little, the best strategy is to avoid possible name conflicts. In your case, you cannot rename System.Drawing.Image, so if at all possible, consider renaming your image type to something more specific to your object model (perhaps CmsImage or ImageEntity).

This is an explicit rule in the .NET Design Guidelines :



Do not include type names that conflict with any type in the primary namespaces.

Obviously, you don't have to follow these rules, but in general I think they offer good advice.

+1


source


You can use something like:

using CmsImage = myCMS.Entities.Image;
using Image = System.Drawing.Image;

      

so you have a clear distinction between them.

Either you use a fully qualified namespace like you are now, or just rename your own image class to "CmsImage" if that makes sense to you, but I'd go for the first approach.

0


source


My guess is that the only time you need to specify a type name with the type of names is when you instantiate that type. Hence, var

is your friend:

var cmsImage = new myCms.Entities.Image();
var drawingImage = new System.Drawing.Image();

      

The aliases created with using

are very difficult to understand from my experience and they make the code less readable.

0


source


I think it would be better to use them with their fully qualified names including namespaces. This way, when someone or future you look at the code, there will be no confusion.

0


source







All Articles