Is it a bad idea to expose inheritance hierarchy in namespace structure? - c#

I've got a group of inter-related classes that are all overridden together to create a particular implementation. I'm wondering if it is a good idea to enclose the interrelated subclasses in a namespace.
For example purposes, consider the following namespaces and classes:
namespace Protocol
{
public abstract class Message { }
public abstract class Driver { }
}
namespace Protocol.Tcp
{
public class TcpMessage : Message { }
public class TcpDriver : Driver { }
}
namespace Protocol.Ftp
{
public class FtpMessage : Message { }
public class FtpDriver : Driver { }
}
What is the best way to structure the namespaces? It seems unavoidable to expose the inheritance in the namespace since the base classes don't really belong in either the Protocol.Tcp namespace or the Protocol.Ftp namespace.

I think you are perhaps worrying too much!
Does it make sense logically? Do you know where to find your code within the namespaces?
I would much rather see a codebase like the above with a small number of classes, relevant to the name with a hierarchy, than one large namespace where everything is interrelated..
Remember, namespacing is there for precisely this, to organise your codebase logically
What you have seems logical :)
EDIT:
As an example:
using System.Data;
using System.Data.Sql;
;)

The original tags show that this post is about C# - therefore multiple inheritance is an irrelevancy - you can't multiply inherit in C#.
Maybe you should consider defining some interfaces that define what the basic contracts of a Message and a Driver are and then you may feel a little free-er to use the namespace structure to mimic the technology differences.

If this were me, I would define 2 namespaces:
Protocol
and
Protocol.Driver
Dividing the namespace like this separates your "library code" vs your "executable / test code."
I also create my namespaces to match the directory structure; it will give logic to your programs structure and codefiles. (maybe you already do this...)

Related

Namespace and Classes - Resolving Naming Ambiguity

The issue I'm getting is "namespace is being used as a type" in a C# project. I get why it's happening and I understand what I have to do to fix it. But it makes little sense to me and so the question here is understanding the design of namespaces and classes in C# context.
I'm hoping this question is different enough from others because I'm not asking how to resolve the situation, but rather how to keep logical and relevant naming while dealing with the situation as I describe it.
I have followed MSDN conventions for naming solutions. So I have a solution called:
TesterStories.Voxam
I have two projects within that. One is TesterStories.Voxam.App and looks like this:
namespace TesterStories.Voxam.App
{
TesterStories.Voxam.Parser parser = new TesterStories.Voxam.Parser();
class Program
{
}
}
The problem is in that third line.
Presumably because I have another project called TesterStories.Voxam.Parser, which looks like this:
namespace TesterStories.Voxam.Parser
{
public class Parser
{
}
}
So is it saying that it doesn't like that I have "Parser" in my namespace for the project?
But according to MSDN guidelines, if the Parser is the feature, then that's what I should be calling the namespace: [Company].[Project].[Feature]. The class is Parser because, after all, that's what the class does.
So it seems like what I have to do is this:
namespace TesterStories.Voxam.Parser
{
public class Xyzzy
{
}
}
So here I changed my Parser class to Xyzzy class. Now back in my file that was causing the problem I can do this:
using TesterStories.Voxam.Parser;
namespace TesterStories.Voxam.App
{
class Program
{
Xyzzy parser = new Xyzzy();
}
}
So I get it -- but it means I have to change my class to be something it's not. I realize I could call it AppParser or ParserApp or whatever.
In this context, what do developers tend to do? Do you change your namespace or do you change your class? Even though, in my case, I'm describing the feature as per MSDN guidelines and correctly naming the class based on what it does.)
The namespaces and class names aren't the problem here.
In your original code, you need to designate what class from the TesterStories.Voxam.Parser namespace this 'parser' object should be.
Your original code:
TesterStories.Voxam.Parser parser = new TesterStories.Voxam.Parser();
What class/type in the TesterStories.Voxam.Parser namespace is this supposed to be?
You would need to do:
TesterStories.Voxam.Parser.*Parser* parser = new TesterStories.Voxam.Parser.*Parser*();
(* added for emphasis :) )
Or, like you did in your last code snippet, add a using statement:
using TesterStories.Voxam.Parser;
So that you can do:
Parser parser = new Parser();
Edit - More complete example (for the 'using' suggestion):
namespace TesterStories.Voxam.App
{
using TesterStories.Voxam.Parser;
class Program
{
Parser parser = new Parser();
}
}
namespace TesterStories.Voxam.Parser
{
public class Parser
{
}
}

Is there a way to make use of an abstract class' import in the concrete class?

I have this abstract class:
using TypeLib=some.type.library;
namespace someSpace
{
abstract class Creator
{
abstract public TypeLib.SomeObject createObject();
}
}
Here's a concrete class:
using TypeLib=some.type.library;
namespace someSpace
{
class SpecialCreator:Creator
{
override public TypeLib.SomeObject createObject()
{
doSomethingSpecial();
return new TypeLib.SomeObject();
}
}
}
Because I will want to implement Creator several different ways, all of which require importing some.type.library because they need to return a TypeLib.someObject, is there a way that I can just have the using statement at some high level and have it be inherited by all the implementations? When I didn't include the using statement in SpecialCreator(), it didn't have access to TypeLib.
EDIT: I think this is different than the duplicate directives question. I'm not trying to consolidate different using statements into one master using that I will then add to many classes; instead, I want to put one using statement in a parent class/header file/static class/etc so that it can be accessible to many classes without having to add the same line of code to each class. Let me know if I've misunderstood the duplicate directives question.
It depends. If TypeLib is only used in base class functionality there is no reason to include 'using TypeLib;' in the files that contain the concrete implementations.
If the desired architecture is intended to encapsulate TypeLib in the abstract base class, the need to have the using statement is an indicator that encapsulation has been broken.
Martin Fowler may say the requirement to 'use' TypeLib in all the concrete implementations is the smell of tight coupling between the class tree under the base class and TypeLib. Using an adapter class may reduce the coupling by putting all TypeLib uses into the adapter.
Not unless you define all classes within a single file.
It depends on what you are trying to do.
If you are just trying to avoid having to add this in each and every page of your derived class, your'd be better off using Visual Studio's Export Template (under the File menu). Pick the Item Template and your added template will show up in the Add > New Item List. I think this would be cleanest.
Of course, there is a sneaky way to do avoid the using itself altogether, but I don't think it's good design. Notice I changed the namespace.
namespace some.type.library;
{
class SpecialCreator:Creator
{
override public SomeObject createObject()
{
doSomethingSpecial();
return new SomeObject();
}
}
}
You could also instead, derive a class in your someSpace namespace from TypeLib.SomeObject (no body for the class) and use this new class wherever you were using TypeLib.SomeObject
using is just a syntactic sugar to save you from typing fully qualified type names each time you type them. It's a hint for compiler for places where to look for type names.
So unless you actually use types from TypeLib you don't have to add using statement in each implementation even if some base class of that concrete implementation uses it in some way (i.e. derives from it). And even if your concrete implementation use types from TypeLib you can avoid using statement by specifying fully qualified type names from that library each time.
You could "alias" the type by creating a child class of it for internal use. For example:
namespace Remote.Namespace
{
public class TypeLib
{
public class SomeObject
{
}
}
}
namespace Internal.Namespace
{
public class InternalTypeLibObject : Remote.Namespace.TypeLib.SomeObject
{
}
}
And now you can use InternalTypeLibObject without having to alias the namespace and cast it to TypeLib.SomeObject.
Now for the drawbacks:
If you want to use the constructors in the parent class, you will need to provide mirror constructors and call the base constructor with constructor chaining. Also, if the parent class is sealed, this won't work.
Honestly though you shouldn't be concerned too much about a little typing. Hiding classes like this can add to confusion and should probably be avoided unless you have a reason to extend the type.

Namespace design and class separation

I have problem to find a good namespace design and class order.
Imagine we have the following namespace hierarchy:
MyCompany.Hardware
- ClassA
MyCompany.Hardware.DeviceType1
- ClassB : ClassA
MyCompany.Hardware.DeviceType2
- ClassC : ClassA
MyCompany.Hardware.Reader
- ReaderClassC : Reader
Questions:
Should I put the base classes always in the parent namespace? Or in the child namespace and the concret implementations in the parent?
Should the ReaderClassC be in a separate or in the same namespace MyCompany.Hardware.DeviceType2? Or should I prefer the parent namespace for 'fast' access?
Forget all the namespaces and put all into one (MyCompany.Hardware)?
Hoppefully you understand what I try to get out. It would be great if you can tell me about your namespace design.
As usual it's a matter of taste.
I prefer to use your fourth approach since it's a little cleaner (for me). I mean, if I'm using a Reader then I depend on using that namespace.
Of course you can give a look to the Namespace Naming Guidelines and particularly:
A nested namespace should have a dependency on types in the containing
namespace. For example, the classes in the System.Web.UI.Design depend
on the classes in System.Web.UI. However, the classes in System.Web.UI
do not depend on the classes in System.Web.UI.Design.
This is all just my ad-hoc advice assuming your relatively new to object oriented programming. I think your question touches an area of design that is subject to debate and programming style choices.
That said,
One of the main goals of namespaces is to group up your types so they can easily be reused by other "clients" or programs that reference their containing class library.
With that in mind, you could start out with just one namespace for simplicity and introduce namespaces when they add value.
One place they might not add value in your example is DeviceType1 and DeviceType2. In my experience these would belong in the same namespace.
namespace Acme.Hardware {
class Product
{
public int Price { get; set; }
public bool InStock { get; set; }
}
class Hammer : Product
{
}
class ScrewDriver : Product
{
}
}
As an example of how coding style and namespaces intersect, notice how I used the very generic name Product as opposed to HardwareProduct. I went with a generic name here because I knew that name would be used by an inclusion of using Acme.Hardware or fully qualified Acme.Hardware.Product. In either case, there is contextual information near by.
In general I'd focus on classes first (i.e. their functionality and implementation) and namespaces secondarily, thinking of them as just a useful way to group up classes.
It depends on other definitions - namespaces are useful when you've got lots of types around, so if your, let's say, ClassB is accompanied with lots of stuff related to Hardware1 then it makes perfect sense to have inner namespace for this.
Ideally, you should just follow the logical grouping. You asked about ReaderC, if it relates to DeviceType2 and not to others, then it should not be in parent namespace, and if ClassA is base for all hardware types, then it needs to be in parent namespace as well.

In C#, what is a good way to exclude a class when outputting code from textfile

What I want to do may seem like a weird scenario. Please keep in mind that I need to do this for a Demo project, where I output c# code to the user to teach them how certain controls are coded.
I am given a .cs file and I need to output the contents. There is at least one class in the file, and at most...a lot. I need to output the whole file, EXCEPT one type of class. The specific type of class that I want to prevent being outputted all inherit a certain base class, so they should be easy to distinguish.
Here is an example:
public abstract class A{}
public class B{]
public class C{}
Assume these are the base-types that some of my classes may inherit. I want to prevent outputting all classes that inherit from A. A is probably going to be the only abstract base class so if that can help in anyway, that would be awesome.
Let's say I'm given a file, example.cs:
using System;
using OtherStuff;
namespace blah.blahagain.someotherblah
{
[AttributeOne]
[AttributeTwo]
[AttributeThree]
public class AA: A
{
//stuff
}
public class BB: B
{
//stuff
}
public class CC: C
{
//stuff
}
public class D
{
//stuff
}
}
And the output should be
using System;
using OtherStuff;
namespace blah.blahagain.someotherblah
{
public class BB: B
{
//stuff
}
public class CC: C
{
//stuff
}
public class D
{
//stuff
}
}
The only way I have thought of is brute-force string manipulation. I can't, however, use whitespace as a separator between classes because there is no guarantee if there will even be white space between classes. I will need to keep track of open and closed curly brackets to discover where one class begins and another end. I also need to test for the base class of each class by testing the string tokens before the first {} pair.
Also I need to prevent the attributes of AA from outputted too.
Since there are many brighter minds out there, I am here to ask if there is another simpler/cleaner method for doing what I need.
Thanks for reading!
Edit after YetAnotherUser's answer: The output should be exactly the same as the file, which includes all comments.
Another edit: Instead of answering with certain software or libraries that could do this, I would more prefer algorithms. Maybe regular expressions? I am not good with them so I do not know the extend that they can be used for.
Could you wrap everything you need to exclude with:
#region ExcludeRegion
[AttributeOne]
[AttributeTwo]
[AttributeThree]
public class AA: A
{
//stuff
}
#endregion
See the #region documentation
This should be relatively easy to scan for and exclude. It also gives you the added benefit of showing what you're hiding in the IDE.
You can try to use open-source lib NRefactory. It supports parsing the code into AST which you can modify afterwards and generate output code. It also can retain your comments as shown on their wiki page.
Found it here: An alternative for "CSharpCodeProvider.Parse"
Update: Apparently there's no implementation of CodeDomProvider that supports parsing. Hence this is invalid - I'll keep this post to ensure this is highlighted to someone thinking on same lines.
You can parse the code using CodeDomProvider and then remove the required classes and regenerate the code file.
If it is for educational purpose, this might not exactly fit your needs as generated file might not exactly match with the original source file.
See -
System.CodeDom Namespace
Using the CodeDOM
Microsoft .NET CodeDom Technology - Part 1

The C# namespace and class/sub-class naming conventions when the top namespace contains the base class and inner namespaces contain sub-classes

I'm trying to design a class library for a particular engineering application and I'm trying to ensure that my class & namespace naming conventions make sense.
I have the following situation:
namespace Vehicle{
class Wheel{...} //base class for Wheel objects
class Engine{...} //base class for Engine objects
...
namespace Truck{
class Wheel: Vehicle.Wheel{...} //Truck specific Wheel object
class Engine: Vehicle.Engine{...} //Truck specific Engine object
...
}
namespace Car{
class Wheel: Vehicle.Wheel{...} //Car specific Wheel object
class Engine: Vehicle.Engine{...} //Car specific Engine object
...
}
...
}
The code gets used in ways that all of these classes will need to be referenced from within the same scope. The following situation would be likely:
...
Vehicle.Wheel.DoSomething();
Vehicle.Truck.Wheel.DoSomething();
Vehicle.Car.Wheel.DoSomething();
...
Under these circumstances, am I better off giving the classes more specific names
namespace Car{
class CarWheel: Vehicle.Wheel{...} //Car specific Wheel object
...
}
or leave the naming as shown in the first example and rely on the information that is encoded in the namespace for clarity? Under the latter approach, I assume I would want to utilize alaising for clarity in the code that makes use of this library, corret?
It seems redundent to have:
Vehicle.Car.CarWheel
or
Vehicle.Truck.TruckEngine
but I also want to have very descriptive and specific class names.
Philosophically, what I'm asking is whether or not to include the namespace as a part of the class name when considering if a class name is descriptive enough.
Typically namespaces are pluralized, so as not to collide with class names (e.g. it is likely you would want classes named Vehicle and Car) so I'd be inclined to use namespaces as follows:
namespace Vehicles;
namespace Vehicles.Cars;
namespace Vehicles.Trucks;
As for the names of classes, it would be typical to prefix the class name with the specialization, especially if they are likely to be used together, so you'd end up with something like:
class CarWheel : Wheel
class TruckWheel : Wheel
You can see this type of 'redundancy' everywhere in the .NET Framework, for example in the System.Xml namespace virtually all classes are prefixed with Xml, or in the System.Data.SqlClient namespace most classes are prefixed with Sql. It means that you can import namespaces with the using directive and then not have to fully-qualify class names throughout your code, e.g. which of the following is more readable?
Vehicles.Cars.Wheel wheel = new Vehicles.Cars.Wheel();
or
CarWheel wheel = new CarWheel();
It's obvious what both are doing, but the second is considerably shorter.
Note that if you do include the specialization in the name, then you may find that you don't need all the nested namespaces (.Cars, .Trucks, etc.) which can become painful if they are usually used together, and so every file using them would have to import all the namespaces, e.g.
using Vehicles;
using Vehicles.Cars;
using Vehicles.Trucks;
using Vehicles.SomethingElse;
using Vehicles.YetAnotherThing;
If you find this same stack of using directives is at the top of each file, then collapse the classes down into a single namespace. You typically include all related functionality that is expected to be used together in a single namespace, and only use nested ones for functionality that extends the base namespace but is less frequently used.
I would try to avoid reusing names across different namespaces, particularly if a client may want to use both in the same program.
Do you really need a namespace for Car, Truck etc? All these namespaces sound more like they ought to be classes than namespacese. Perhaps in your real situation it makes more sense though...

Categories

Resources