9

In Visual Studio 2008 using C#, what is the best way to share code across multiple classes and source files?

Inheritance is not the solution as the classes already have a meaningful hierarchy.

Is there some neat feature that's like a C include file that let's you insert code anywhere you want in another class?

EDIT:

ok, i guess we need a concrete example...

There are several hundred classes in the domain with a well thought out class heirarchy. Now, many of these classes need to print. There is a utility printer class that handles the printing. Let's say there are 3 different print methods that are dependent on the class that is being printed. The code that calls the print method (6 lines) is what I'm trying to avoid copying and pasting across all the different client class pages.

It'd be nice if people wouldn't assume they knew more about the domain that the op - especially when they specifically mention techniques that don't fit...

mson
  • 7,762
  • 6
  • 40
  • 70
  • 1
    So you're basically looking for mix-ins in C#? – dtb Dec 10 '09 at 03:53
  • 2
    Could you show us the 6 lines which you want to duplicate? –  Dec 10 '09 at 03:54
  • i'm not sure what a mix-in is – mson Dec 10 '09 at 03:55
  • A mix-in is a set of methods that can be "mixed" into another class without that other class having to derive from a class with the methods. I think the closest you can get in C# are extension methods. Have a look at these! – dtb Dec 10 '09 at 03:58
  • thanks dtb - those are the closest to what i've been looking for. the problem is that the calling method looks at some internal object values prior to calling, but i may be able to work around this. please post an answer so i can mark it. – mson Dec 10 '09 at 04:02
  • There are already some answers mentioning extension methods (there are no mix-ins in C#). But extension methods can only access public members of an extended class. Post some examples of your calling methods so we can see how to work around this problem in a clean way. – dtb Dec 10 '09 at 04:10

10 Answers10

9

If you have functionality that you use frequently in classes that represent very different things, in my experience that should fall into just a few categories:

  • Utilities (e.g. string formatting, parsing, ...)
  • Cross-cutting concerns (logging, security enforcement, ...)

For utility-type functionality you should consider creating separate classes, and referencing the utility classes where needed in the business class.

public class Validator
{
  public bool IsValidName(string name);
}

class Patient
{
  private Validator validator = new Validator();
  public string FirstName
  {
     set
     {
         if (validator.IsValidName(value)) ... else ...
     }
  }
}

For cross-cutting concerns such as logging or security, I suggest you investigate Aspect-Oriented Programming.

Regarding the PrintA vs. PrintB example discussed in other comments, it sounds like an excellent case for the Factory Pattern. You define an interface e.g. IPrint, classes PrintA and PrintB that both implement IPrint, and assign an instance of IPrint based on what the particular page needs.

// Simplified example to explain:

public interface IPrint 
{ 
   public void Print(string); 
}

public class PrintA : IPrint
{
   public void Print(string input)
   { ... format as desired for A ... }
}

public class PrintB : IPrint
{
   public void Print(string input)
   { ... format as desired for B ... }
}

class MyPage
{
   IPrint printer;

   public class MyPage(bool usePrintA)
   {
      if (usePrintA) printer = new PrintA(); else printer = new PrintB();
   }

   public PrintThePage()
   {
      printer.Print(thePageText);
   }
}
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • +1 for AOP/IoC. Managed Extensibility Framework (MEF) is a very good choice since it will be part of .Net 4.0 framework. http://mef.codeplex.com/ http://ayende.com/Blog/archive/2008/09/25/the-managed-extensibility-framework.aspx – Diadistis Dec 10 '09 at 04:20
  • +1 This is also how I would implement it. If Inheritance doesnt suit then the next choice would be to look into Aggregation. – Leigh S Dec 10 '09 at 04:32
  • @Diadistis: Have you used MEF/have an opinion? I have most recently been working in the Java/Spring environment and have not had a chance to try out MEF. – Eric J. Dec 10 '09 at 04:44
7

You can't just load in code that you'd like to have added into a class in C# via a preprocessor directive like you would in C.

You could, however, define an interface and declare extension methods for that interface. The interface could then be implemented by your classes, and you can call the extension methods on those classes. E.g.

public interface IShareFunctionality { }

public static class Extensions
{
    public static bool DoSomething(this IShareFunctionality input)
    {
        return input == null;
    }
}

public class MyClass : Object, IShareFunctionality
{
    public void SomeMethod()
    {
        if(this.DoSomething())
            throw new Exception("Impossible!");
    }
}

This would allow you to reuse functionality, but you cannot access the private members of the class like you would be able to if you could, say, hash include a file.

We might need some more concrete examples of what you want to do though?

Khanzor
  • 4,830
  • 3
  • 25
  • 41
  • 1
    Brilliant! I have two classes, very similar functionality but different base classes. Shared methods I factored out into extension methods as you suggested. – PepitoSh Jul 03 '18 at 03:11
3

A C# utility class will work. It acts like a central registry for common code (or like the VB.NET Module construct) - it should contain code that's not specific to any class otherwise it should have been attached to the relevant class.

You don't want to start copying source code around if you don't have to because that would lead to code update problems considering the duplication.

As long as the source doesn't need to retain state, then use a static class with static method.

static public class MySharedMembers {
    static public string ConvertToInvariantCase(string str)  {
        //...logic
    }
    // .... other members
}
John K
  • 28,441
  • 31
  • 139
  • 229
  • 3
    This creates a hard dependency, is difficult to mock for testing and likely violates the single responsibility principle. – jason Dec 10 '09 at 03:19
  • 1
    I'm not suggesting much different than (e.g. Microsoft's RegEx class) that arguably cannot be anything but a hard dependency and violates the single responsibility principle. The author didn't specify what needs to be reused so this suggestion is as valid as another. Certainly with tidbits and fragments we don't need to be blinded by patterns or speculate on expertise. – John K Dec 10 '09 at 04:38
2

If the classes are in the same namespace, there's no need for an include analog. Simply call the members of the class defined in the other function.

If they're not in the same namespace, add the namespace of the classes you want to use in the usings directives and it should work the same as above.

I'm confused by the question: it seems you need to work on your basic OO understanding.

antik
  • 5,282
  • 1
  • 34
  • 47
2

Checkout extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx

Jerry Fernholz
  • 685
  • 5
  • 11
2

I don't know of a way to include portions of files but one thing we do frequently is to add an existing file and "link" it from its current location. For example, we have an assemblyInfo.cs file that every project refers to from a solution directory. We change it once and all the projects have the same info because they're referring to the same file.

Otherwise, suggestions about refactoring "common" routines in a common.dll are the best thing I've come up with in .Net.

No Refunds No Returns
  • 8,092
  • 4
  • 32
  • 43
1

I am not sure exactly what you mean by a "meaningful" structure already, but this sounds like a place where you could use base class implementation. Though not as "verbose" as C++ multiple inheritance, you might get some benefit out of using chained base class implementation to reuse common functions.

You can preserve class hierarchy, at least visually and override behavior as needed.

GrayWizardx
  • 19,561
  • 2
  • 30
  • 43
  • what base class would you use when your classes are invoice, employee, registration, patient, car, office, room, and 300 other classes? – mson Dec 10 '09 at 03:31
  • Base classes dont necessarily have to be "PersonBase", etc. They can be "PersistableBase", etc. In C++ we would have a number of class prototypes we would import, and aggregate together, in C# we have to do this through single lineage inheritance and interfaces (for pure OOP). The language allows us to also use partial classes, extension methods (above 2.0), etc. It really depends on your object graph. Is there a reason why you cannot recompose objects to use the other techniques mentioned? – GrayWizardx Dec 10 '09 at 04:43
1

Pull out the repetitive code into services. The repetitive code is a clue that there might be some room for refactoring.

For example, create a "PrintingService" which contains the logic needed to print. You can then have the classes that need to print have a dependency on this service (either via the constructor or a parameter in a method which requires the service).

Another tip i have along these lines is to create interfaces for base functionality and then use the interfaces to code against. For example, i had bunch of report classes which the user could either fax, email, or print. Instead of creating methods for each, i created a service for each, had them implement an interface that had a single method of Output(). I could then pass each service to the same method depending on what kind of output the user wanted. When the customer wanted to use eFax instead of faxing through the modem, it was just a matter of writing a new service that implemented this same interface.

Josh
  • 1,001
  • 9
  • 18
  • the post states that there already is a utility printer class... the code used to call the method is repeated across all the client classes - this is what i want to not copy and paste. btw - the calling client code has the object look at itself and determine the request to the print utility based on the state of the object and credentials. your suggestion is rudimentary and indicative that you did not read the post. – mson Dec 10 '09 at 07:18
  • well I still enjoyed reading it just now for educational value – Proclyon Dec 15 '10 at 14:22
0

To be honest I can't think of anything like includes in Visual C#, nor why you would want that feature. That said, partial classes can do something like it sounds what you want, but using them maybe clashes against your "classes already have a meaningful hierarchy" requirement.

Coxy
  • 8,844
  • 4
  • 39
  • 62
  • let's say i've got print method A and print method B. on some pages i want print method A on other pages i want print method B. It is helpful for the methods to be static as the method is dependent on the code in the object. right now, i'd have to copy paste the correct method on the appropriate pages... there must be a better way. – mson Dec 10 '09 at 03:22
  • 2
    @mson - Invest more time in your hierarchy. It sounds like what you need is to rework your inheritance structure so objects that use printA are all derived from a common member and the same is true for objects that use printB – antik Dec 10 '09 at 03:25
  • 1
    @mson It sounds like you might want a Printer class, which has both methods, either of which you can call as needed. – Lucas Wilson-Richter Dec 10 '09 at 03:27
  • Sorry, but I'm still not seeing why you couldn't implement both printA() and printB() as methods in some utility class and then call the correct method from various pages as appropriate? – Coxy Dec 10 '09 at 03:27
0

You have many options, TT, extension method, delegate, and lambda

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
Dennis C
  • 24,511
  • 12
  • 71
  • 99