Search code examples
c#architectureinterfaceenums

Dealing with enums when interfaces are in separate project


I have the following projects in a Visual Studio solution for an application:

  • Common - Utility methods and extensions
  • Entities - Rich Domain objects with business logic specific to instances
  • Repositories - Data Repositories
  • DataServices - Thin wrapper to Repositories, contains business logic not specific to an instance
  • Interfaces - All interfaces for entities and repositories

The reason I put the Interfaces into a separate project is avoid circular project references. This allows two projects to reference a common interface avoiding having both reference the project with the concrete implementation.

I've purposely made no project references in the Interfaces project to avoid circular project references. I create an Interface for classes defined in other projects, this allows me to reference the object interface, opposed to the concrete implementation in other interfaces.

So an example would be:

namespace Acme.Entities 
{
   public class Person : IPerson
   {
       string Name { get; set; }
   }
}
namespace Acme.Interfaces
{
    public interface IPerson
    {
        string Name { get; set; }
    }
}

namespace Acme.Interfaces
{
    public interface ITeam
    {
        string Name { get; set; }
        IPerson Leader { get; set; }  
    }
}

The issue I've run into is when an Interface references an enum defined in another project. Without moving the Enums under the Interfaces project, I'm not sure how to reference the Enums without creating project references, for example:

namespace Acme.Entities 
{
   public enum Status
   {
       Unknown =0,
       Active = 1,
       Active = 2    
   }
} 

namespace Acme.Interfaces
{
    public interface IPerson
    {
        string Name { get; set; }
        Acme.Entities.Status ActiveStatus { get; set; }  
    }
}

The Acme.Entities.Status will fail unless I reference the Acme.Entities project, but that will create a circular reference because Acme.Entities references the Interfaces project.


Solution

  • You'll either have to move the enum definition to the Interfaces project or to a separate project that both projects reference.

    I personally would keep them in the same project - having a separate project just for enums seems like overkill.