Skip to main content
Post Undeleted by Engineert
Post Deleted by Engineert
added 769 characters in body
Source Link
Engineert
  • 929
  • 1
  • 6
  • 18

And, an answer of your main question would be Property Injection.

public class Service
    {
        public Service(Context context)
        {
            this.context = context;
        }

        private Dependency1 _dependency1;
        public Dependency1 Dependency1
        {
            get
            {
                if (_dependency1 == null)
                    _dependency1 = Container.Resolve<Dependency1>();

                return _dependency1;
            }
        }
        
        //...
    }

This way you can call all dependencies by Property Injection. But it could be huge number. If so, you can use Constructor Injection for them, but you can set your context by property by checking if it is null.

And, an answer of your main question would be Property Injection.

public class Service
    {
        public Service(Context context)
        {
            this.context = context;
        }

        private Dependency1 _dependency1;
        public Dependency1 Dependency1
        {
            get
            {
                if (_dependency1 == null)
                    _dependency1 = Container.Resolve<Dependency1>();

                return _dependency1;
            }
        }
        
        //...
    }

This way you can call all dependencies by Property Injection. But it could be huge number. If so, you can use Constructor Injection for them, but you can set your context by property by checking if it is null.

Removed static constructor.
Source Link
Engineert
  • 929
  • 1
  • 6
  • 18

By the way, if you use your Service class for long time without disposing, then you can call Initialize method on Static Constructor :

public class Service : IService
{
    private readonly object dependency1;
    private readonly object dependency2;
    private readonly object dependency3;
    private static object _context;

    public Service(
        object dependency1,
        object dependency2,
        object dependency3,
        object context )
    {
        this.dependency1 = dependency1 ?? throw new ArgumentNullException(nameof(dependency1));
        this.dependency2 = dependency2 ?? throw new ArgumentNullException(nameof(dependency2));
        this.dependency3 = dependency3 ?? throw new ArgumentNullException(nameof(dependency3));
        _context = context;

    }
    
    
    static Service()
    {
        Initialize(_context);
    }

    protected static void Initialize(Context context)
    {
        // Initialize state based on context
        // Heavy, long running operation
    }

    //...

}

By the way, if you use your Service class for long time without disposing, then you can call Initialize method on Static Constructor :

public class Service : IService
{
    private readonly object dependency1;
    private readonly object dependency2;
    private readonly object dependency3;
    private static object _context;

    public Service(
        object dependency1,
        object dependency2,
        object dependency3,
        object context )
    {
        this.dependency1 = dependency1 ?? throw new ArgumentNullException(nameof(dependency1));
        this.dependency2 = dependency2 ?? throw new ArgumentNullException(nameof(dependency2));
        this.dependency3 = dependency3 ?? throw new ArgumentNullException(nameof(dependency3));
        _context = context;

    }
    
    
    static Service()
    {
        Initialize(_context);
    }

    protected static void Initialize(Context context)
    {
        // Initialize state based on context
        // Heavy, long running operation
    }

    //...

}

Source Link
Engineert
  • 929
  • 1
  • 6
  • 18

You should not depend your interface to any db context and initialize method. You can do it in concrete class constructor.

public interface IService
{
    void DoSomething();
    void DoOtherThing();
}

public class Service : IService
{
    private readonly object dependency1;
    private readonly object dependency2;
    private readonly object dependency3;
    private readonly object context;

    public Service(
        object dependency1,
        object dependency2,
        object dependency3,
        object context )
    {
        this.dependency1 = dependency1 ?? throw new ArgumentNullException(nameof(dependency1));
        this.dependency2 = dependency2 ?? throw new ArgumentNullException(nameof(dependency2));
        this.dependency3 = dependency3 ?? throw new ArgumentNullException(nameof(dependency3));
       
        // context is concrete class details not interfaces.
        this.context = context;
        
        // call init here constructor.
        this.Initialize(context);
    }

    protected void Initialize(Context context)
    {
        // Initialize state based on context
        // Heavy, long running operation
    }

    public void DoSomething()
    {
        // ...
    }

    public void DoOtherThing()
    {
        // ...
    }
}

By the way, if you use your Service class for long time without disposing, then you can call Initialize method on Static Constructor :

public class Service : IService
{
    private readonly object dependency1;
    private readonly object dependency2;
    private readonly object dependency3;
    private static object _context;

    public Service(
        object dependency1,
        object dependency2,
        object dependency3,
        object context )
    {
        this.dependency1 = dependency1 ?? throw new ArgumentNullException(nameof(dependency1));
        this.dependency2 = dependency2 ?? throw new ArgumentNullException(nameof(dependency2));
        this.dependency3 = dependency3 ?? throw new ArgumentNullException(nameof(dependency3));
        _context = context;

    }
    
    
    static Service()
    {
        Initialize(_context);
    }

    protected static void Initialize(Context context)
    {
        // Initialize state based on context
        // Heavy, long running operation
    }

    //...

}