0

I have a base class BLLContext:

public sealed class BLLContext : IDisposable
{
    private LayersSharedObjects _layersSharedObjects = null;
    internal HttpContext httpContext = HttpContext.Current;

    Authorization _authorization = null;

    public Authorization Authorization
    {
        get
        {
            if (_authorization == null)
            {
                _authorization = new Authorization(this);
            }

            return _authorization;
        }
    }

    public BLLContext()
    {

    }
}

All of my BLL classes has a constructor that should get object of the above class. for example:

public class Authorization : BLLBase
{
    public Authorization(BLLContext bLLContext) : base(bLLContext)
    {
    }
}

Everything was ok until i had to get some information from the Authorization class from an instance of BLLContext. So i added the this part to the BLLContext class:

public Authorization Authorization
{
    get
    {
        if (_authorization == null)
        {
            _authorization = new Authorization(this);
        }
        return _authorization;
    }
}

but because Authorization class has to get BLLContext and i am inside BLLContext, i passed "this" to the Authorization constructor.

Can someone explain if it will case to circular reference?, it seems that its not while checking with .Net Profiler.

What should i do to avoid this but still pass "this" and keep the current structure? The system is working well without any significant problems.

Suneel Kumar
  • 1,650
  • 2
  • 21
  • 31
Omtechguy
  • 3,321
  • 7
  • 37
  • 71
  • No problem with the circular reference. Garbage collectors can handle them correctly – AhmadWabbi Jan 24 '18 at 10:25
  • @AhmadWabbi so why all the time, developers are fighting to solve circulate referencing and say its a mistake ? Can you explain how the GC will handle that? – Omtechguy Jan 24 '18 at 10:26
  • It's not an issue in C#, or in Java. In other languages, it can be. My guess is that the developers you've seen saying it's a mistake are using those other languages. (It can be a design smell even if it's not a GC problem, admittedly.) Basically the .NET GC doesn't use reference counting - and it's reference counting that leads to issues with circular references. – Jon Skeet Jan 24 '18 at 10:29
  • @Omtechguy: one of the reasons for managed, garbage collected languages is to avoid the need for "fighting to solve circulate referencing". Try googling "mark and sweep GC". – vgru Jan 24 '18 at 10:30
  • @Omtechguy Yeah, as Groo said, "Mark and sweep" and "Semi-space copy" algorithms handle circular reference problem. Google them out. As for "why developers are fighting to solve circular referencing", I have no idea why they do that! Even in languages that do not have automatic garbage collection and need manual memory management (like C++), I don't see why circular reference is a problem. – AhmadWabbi Jan 24 '18 at 10:52

0 Answers0