Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular References in my C# projects

I have the following situation:

  1. A project MyCompany.MyProject.Domain which contains my domain model, and partial classes (such as Contact).

  2. I want to 'extend' (by partial class, not extension method) my Contact class with a property Slug which will give me a simple URL friendly text representation of first and last name.

  3. I have a string extension method ToSlug() in my Utility project MyCompany.MyProject.Utilities which does exactly what I want in 2).

  4. The problem: My Utility project is already referencing my Domain project which means that I can't get the Domain project to see the Utility project's ToSlug() method without causing circular reference.

I'm not keen on creating another project to solve this, and I really want to keep the Slug logic shared.

How can I solve this?

like image 756
Alex Avatar asked Aug 23 '09 09:08

Alex


People also ask

How do you fix a circular reference error?

To fix the problem, you can move the formula to another cell. Press Ctrl+X to cut the formula, select another cell, and press Ctrl+V to paste it. You can also try one of these techniques: If you just entered a formula, start with that cell and check to see if you refer to the cell itself.

How do I turn off circular references?

On the 'Excel Options' window, go to the 'Formulas' section and tick the 'Enable iterative calculation' box. Click 'OK' to save the changes. After that, you will not get any warning whenever there's a circular reference.

What is C reference circular reference?

Circular reference occurs when two or more interdependent resources cause lock condition. This makes the resource unusable. To handle the problem of circular references in C#, you should use garbage collection. It detects and collects circular references.

How do I turn on circular references?

To allow circular references, in the Menu, select File > Spreadsheet settings. Select the Calculation tab, and then in the Iterative calculation drop down, select On.


1 Answers

Your Utility project referencing your MyCompany.MyProject.Domain seems like a bit of a code smell. I'm assuming here that these are utilities that specifically work on domain objects--if that's the case, then why don't you include MyCompany.MyProject.Utilities within your Domain project (naturally, modifying the namespace accordingly)?

In any case, the normal way to break these kinds of dependencies is to abstract what is required by one project into a set of interfaces, and encapsulate those in a separate assembly. Before doing that though, make sure that what you're doing conceptually is the right thing.

In your particular situation though, consider introducing an interface, viz., INameHolder:

public interface INameHolder
{
    string FirstName { get; set; }
    string LastName { get; set; }
}

Then Contact implements INameHolder. INameHolder exists in another assembly, let's call it MyCompany.MyProject.Domain.Interfaces.

Then your Utilities project references Interfaces (not Domain) and so does Domain, but Interfaces doesn't reference anything--the circular reference is broken.

like image 194
Eric Smith Avatar answered Sep 17 '22 22:09

Eric Smith