Mandatory dependency in a class I have a class with some mandatory properties which I set via constructor, like this (properties omitted for sake of simplicity):

    public SqlDependencyEx(
        string connectionString,
        string databaseName,
        string tableName,
        string schemaName = "dbo",
        NotificationTypes listenerType = NotificationTypes.Insert | NotificationTypes.Update | NotificationTypes.Delete,
        bool receiveDetails = true,
        int identity = 1)
    {
        this.ConnectionString = connectionString;
        this.DatabaseName = databaseName;
        this.TableName = tableName;
        this.SchemaName = schemaName;
        this.NotificaionTypes = listenerType;
        this.DetailsIncluded = receiveDetails;
        this.Identity = identity;
    }


I am trying to use SqlDependencyEx class as a dependency in one of the service like this:

    private readonly IMemoryCache _memoryCache;
    private readonly IAlbumRepo _albumRepo;
    private readonly SqlDependencyEx _listener;
    private readonly string _masterTable = "Armada_Album";
    public AlbumService(IMemoryCache memoryCache, IAlbumRepo albumRepo)
    {
        _memoryCache = memoryCache;
        _albumRepo = albumRepo;
        _listener = new SqlDependencyEx(connectionString: "SERVER=localhost;DATABASE=dbsample;User Id=usr;pwd=cE$6adadZ'[:3;", databaseName: "KEMITEKS", tableName: _masterTable);
    }


The problem is, I don't think it is good to creating a new instance of SqlDependencyEx class in AlbumService class, but at the same time, I want to force consumer of the SqlDependencyEx class to use those properties, since they are all mandatory in order to SqlDependencyEx class to work. So, I don't want to remove those properties and instead of using them in a constructor of SqlDependencyEx class, setting them in separate method etc. I am also using a Ninject as a DI. Is there a better code design to solve this problem, or is it good to leave it as it is?
If you are using a dependency injection framework then you are not using it correctly. AlbumService's constructor should have in this case another parameter that is the SqlDependencyEx. You should not be creating it by yourself. You are already injecting the other two services. Why not this one too?

And I'm wondering where you have the _masterTable parameter from? I don't see it to be passed to the AlbumService. It has only two arguments.


_listener = new SqlDependencyEx(
    connectionString: "SERVER=localhost;DATABASE=dbsample;User Id=usr;pwd=cE$6adadZ'[:3;", 
    databaseName: "KEMITEKS", 
    tableName: _masterTable);



Another thing is that the name of the SqlDependencyEx does not match the field. Why do you call it a _listener? It's very confusing.