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?