NotificationHubConnectionSettings.cs file to fetch connection string from web.config
public class NotificationHubConnectionSettings
{
public NotificationHubClient Hub { get; set; }
public NotificationHubConnectionSettings()
{
Hub = NotificationHubClient.CreateClientFromConnectionString(ConfigurationManager.AppSettings["Microsoft.Azure.NotificationHubs.ConnectionString"], ConfigurationManager.AppSettings["NotificationHub"]);
}
}
Inside Bootstrapper.cs
using Unity dependency injection nuget
private static IUnityContainer BuildUnityContainer()
{
try
{
var container = new UnityContainer();
container.RegisterType<NotificationHubConnectionSettings>().RegisterType<NotificationHubConnectionSettings>(new HierarchicalLifetimeManager());
return container;
}
catch (Exception)
{
return null;
}
}
In HomeController.cs
want to implement dependency injection -
private readonly NotificationHubClient _hub;
public HomeController(NotificationHubConnectionSettings hub)
{
_hub = hub.Hub;
}
// POST api/register
// This creates a registration id
public async Task<string> Post(string handle = null)
{
string newRegistrationId = null;
if (handle != null)
{
var registrations = await _hub.GetRegistrationsByChannelAsync(handle, 100);
Is this correct way to implement dependency injection?
You should be using a interface type here.
Create a contract for INotificationHubConnectionSettings class in the form of a interface which dictates to your system all public methods and properties available.
public interface INotificationHubConnectionSettings
{
NotificationHubClient Hub { get; set; }
}
Then have your actual NotificationHubConnectionSettings class inherit from this interface;
public class NotificationHubConnectionSettings : INotificationHubConnectionSettings
{
public NotificationHubClient Hub { get; set; }
public NotificationHubConnectionSettings()
{
Hub = NotificationHubClient.CreateClientFromConnectionString(ConfigurationManager.AppSettings["Microsoft.Azure.NotificationHubs.ConnectionString"], ConfigurationManager.AppSettings["NotificationHub"]);
}
}
Now register the interface and class inside of UnityContainer and change your constructor to the following;
private readonly INotificationHubClient _hub;
public HomeController(NotificationHubConnectionSettings hub)
{
_hub = hub.Hub;
}
Always use interfaces for dependency injection.
Related
Is there any way to resolve the instance of a class at the controller level? I would like to override the previous instance created by unity and assign this new value via the controller.
Problem is I am not sure how to access the unity container in the web app controller.
Here is my code:
Repository:
public class UserRepository: IUserRepository
{
private UserInformation _userInfo;
public UserRepository(string headerValue)
{
_userInfo = LoadUserData(headerValue);
}
public UserInformation GetUserInfo()
{
return _userInfo;
}
}
public class UserInformation
{
public string FirstName;
public string LastName;
}
Unity Configuration:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//Some code omitted
config.DependencyResolver = new UnityDependencyResolver(UnityConfig.RegisterComponents());
}
}
public static class UnityConfig
{
public static UnityContainer RegisterComponents()
{
//Unity Configuration
var container = new UnityContainer();
container.RegisterType<IUserRepository, UserRepository>(new InjectionConstructor("DummyHeaderValue"));
return container;
}
}
Controller:
public class CustomerController : ApiController
{
public CustomerController()
{
//Something like this
container.Resolve<UserRepository>(new InjectionConstructor(Request.GetHeader("RealHeaderValueFromHttpRequest")));
}
}
Then I should be able to use the updated UserRepository instance throughout the application.
Any thoughts on how to achieve this?
Edit: As pointed out by #Nkosi I don't have access to Request in controller constructor. So let me rephrase my question again:
How would I initialise UserRepository with UserInformation object which contains details about the current user? The reason I want to do this is that throughout my application I want user details and I don't want to pass User Id from each method
Something like this: From any method throughout application
UserInformation obj = _userRepository().GetUserInfo();
Create an abstraction to get access to the request
public interface IHeaderService {
string RealHeaderValueFromHttpRequest();
}
Its Implementation will have access to the context and request to get the desired functionality
public class HeaderService : IHeaderService {
public string RealHeaderValueFromHttpRequest() {
return HttpContext.Current.Request.Headers["RealHeaderValueFromHttpRequest"];
}
}
The service will now be explicitly injected into the dependent repository
public class UserRepository: IUserRepository {
private readonly IHeaderService headerService;
public UserRepository(IHeaderService headerService) {
this.headerService = headerService;
}
public UserInformation GetUserInfo() {
var headerValue = headerService.RealHeaderValueFromHttpRequest();
var _userInfo = LoadUserData(headerValue);
return _userInfo;
}
//...
}
The repository will then also be explicitly injected into dependent controllers.
public class CustomerController : ApiController {
private readonly IUserRepository repositoty;
public CustomerController(IUserRepository repositoty) {
this.repository = repository;
}
public IHttpActionResult SomeAction() {
//NOTE: Only access user info in a controller action
var userInfo = repository.GetUserInfo();
//... use user info.
}
//...
}
Now all that is left is to make sure all abstractions and their implementations are registered with the dependency container
public static class UnityConfig {
public static UnityContainer RegisterComponents() {
//Unity Configuration
var container = new UnityContainer();
container.RegisterType<IUserRepository, UserRepository>();
container.RegisterType<IHeaderService, HeaderService>();
return container;
}
}
I have the following architecture:
Data
Database Layer
WebAPI
Presentation Layer
Resolver
IoC Register Layer
Services
Business Layer
In WebApiConfig.cs(App_Start) i register the unity container the following way:
// Unity Container Resolver
var container = new UnityContainer();
//Registers the repository interface in Resolver(IoC Register Layer)
var UResolver = new UnityRegisterContainer();
UResolver.RegisterContainer(ref container);
//Configures WebAPI DependecyResolver to use UnityResolver
config.DependencyResolver = new UnityResolver(container);
My Resolver(IoC Register Layer):
public class UnityRegisterContainer
{
public void RegisterContainer(ref UnityContainer container)
{
container.RegisterType<IUnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<IService>(new HierarchicalLifetimeManager());
}
}
Controller:
public static KeyService KeyLibrary{ get; set; }
// GET api/values
[Route("Keys")]
public IEnumerable<KeyDTO> Get()
{
var Keys = KeyLibrary.GetAllKeys();
return Keys;
}
KeyService:
public class KeyService: IService
{
IUnitOfWork UOW { get; set; }
/// <summary>
/// Get all Keys
/// </summary>
/// <returns></returns>
public IEnumerable<KeyDTO> GetAllKeys()
{
return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
}
}
IService
public interface IService
{
}
IUnitOfWork
public interface IUnitOfWork : IDisposable
{
IKeyRepository Keys { get; }
int Complete();
}
How can i inject the class libraries and repositories with unity?
You can use constructor injection and let the DependencyResolver do it job and pass the necessary dependencies to the classes.
public class KeyController : ApiController {
IKeyService keyService;
public KeyController(IKeyService keyService) {
this.keyService = keyService
}
// GET api/values
[Route("Keys")]
public IEnumerable<KeyDTO> Get() {
var Keys = keyService.GetAllKeys();
return Keys;
}
}
public interface IKeyService : IService {
IEnumerable<KeyDTO> GetAllKeys();
}
public class KeyService: IKeyService {
IUnitOfWork UOW;
public KeyService(IUnitOfWork uow) {
this.UOW = uow
}
/// <summary>
/// Get all Keys
/// </summary>
/// <returns></returns>
public IEnumerable<KeyDTO> GetAllKeys() {
return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
}
}
public class UnitOfWork: IUnitOfWork {
public UnitOfWork(IKeyRepository repository) {
Keys = repository;
}
IKeyRepository Keys { get;private set }
public int Complete(){...}
}
Though constructor injection is preferred (and property injection is sometimes not recommended), you can also use the [Dependency] attribute in implementation classes that have dependencies, like this:
public class KeyService: IService
{
// Public setter, private getter, so you can mock and manually assing in Unit Tests
[Dependency]
public IUnitOfWork UOW { private get; set; }
public IEnumerable<KeyDTO> GetAllKeys()
{
return Mapper.Map<IEnumerable<Key>, IEnumerable<KeyDTO>>(UOW.Keys.GetAllKeys());
}
}
See Annotating Objects for Property Injection
How to bind classes with required connection string in constructor using Ninject?
Here are the classes that I am using:
AppService class:
using SomeProject.LayerB;
namespace SomeProject.LayerA;
{
public class AppService
{
private readonly ISomeRepository someRepository;
public LocationManagementService(ISomeRepository someRepository)
{
this.someRepository = someRepository;
}
// other codes ...
}
}
SomeRepository class:
namespace SomeProject.LayerB;
{
public class SomeRepository : ISomeRepository
{
private readonly SomeDbContext context;
public SomeRepository(SomeDbContext context)
{
this.context = context;
}
// other codes ...
}
}
SomeDbContext class:
namespace SomeProject.LayerB;
{
public class SomeDbContext : DbContext
{
public SomeDbContext(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
// other codes ...
}
}
Then, I use a Ninject module containing the following code:
namespace SomeProject.LayerC;
{
public class SomeModule : NinjectModule
{
public override void Load()
{
Bind<ISomeRepository>().To<SomeRepository>();
// My problem is on this part.. I want to provide the connection string on the
// main program, not here on this class.
// Bind<SomeDbContext>().ToSelf().WithConstructorArgument("nameOrConnectionString", "the connection string I want to inject");
}
}
}
Main program:
using SomeProject.LayerA;
using SomeProject.LayerC;
namespace SomeProject.LayerD;
{
public class MainProgram
{
public MainProgram()
{
IKernel kernel = new StandardKernel(new SomeModule());
AppService appService = kernel.Get<AppService>();
}
}
}
NOTE: The only layer that main program can reference is LayerA where AppService class is located and as well as LayerC where the ninject module is found.
Add a Configuration class like this:
public class Config
{
public static string ConnectionString { get; set; }
}
and in your ninject module write this:
Bind<SomeDbContext>().ToSelf()
.WithConstructorArgument("nameOrConnectionString",
c => Config.ConnectionString);
then in your main method you could write following:
public class MainProgram
{
public MainProgram()
{
IKernel kernel = new StandardKernel(new SomeModule());
Config.ConnectionString = "The connection string";
AppService appService = kernel.Get<AppService>();
}
}
Update:
You can use ninject to locate config class also if you don't want use static methods:
class Config2
{
public string ConnectionString { get; set; }
}
in module:
Bind<Config2>().ToSelf().InSingletonScope();
Bind<SomeDbContext>().ToSelf()
.WithConstructorArgument("nameOrConnectionString",
c=>c.Kernel.Get<Config2>().ConnectionString);
in main:
IKernel kernel = new StandardKernel(new SomeModule());
var conf = kernel.Get<Config2>();
conf.ConnectionString = "The connection string";
AppService appService = kernel.Get<AppService>();
I am starting to use Ninject in my MVC5 code-first app. Here's my NinjectWebCommon.cs:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
kernel.Bind<CMSContext>()
.ToSelf()
//.InSingletonScope();
.InRequestScope();
kernel.Bind<IExecutiveRepository>()
.To<ExecutiveRepository>();
kernel.Bind<IExecutiveSectionRepository>()
.To<ExecutiveSectionRepository>();
kernel.Bind<IExecutiveSectionMappingRepository>()
.To<ExecutiveSectionMappingRepository>();
kernel.Bind<IUserRepository>()
.To<UserRepository>();
kernel.Bind<IContentRepository>()
.To<ContentRepository>();
RegisterServices(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
I tried .InSingletonScope() as well as .InRequestScope() but I still get the 'entity object cannot be referenced by multiple instances of IEntityChangeTracker' error.
Here is my Interface:
public interface IExecutiveRepository : IDisposable
{
IEnumerable<Executive> GetExecutives();
Executive GetExecutiveById(int executiveId);
void InsertExecutive(Executive executive);
void UpdateExecutive(Executive executive);
void DeleteExecutive(int executiveId);
void Save();
}
Here is my concrete:
public class ExecutiveRepository : IExecutiveRepository, IDisposable
{
private CMSContext context;
public ExecutiveRepository(CMSContext context)
{
this.context = context;
}
public IEnumerable<Executive> GetExecutives()
{
return context.Executives.ToList();
}
public Executive GetExecutiveById(int id)
{
return context.Executives.Find(id);
}
public void InsertExecutive(Executive executive)
{
context.Executives.Add(executive);
}
public void DeleteExecutive(int executiveId)
{
Executive executive = context.Executives.Find(executiveId);
context.Executives.Remove(executive);
}
public void UpdateExecutive(Executive executive)
{
context.Entry(executive).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Here is the controller(top pertinent part):
public class ExecutiveController : Controller
{
private IExecutiveRepository executiveRepository;
private IUserRepository userRepository;
private IExecutiveSectionRepository executiveSectionRepository;
private IExecutiveSectionMappingRepository executiveSectionMappingRepository;
private IContentRepository contentRepository;
private Ninject.IKernel _kernel = new StandardKernel();
//[Inject]
public ExecutiveController()
{
executiveRepository = _kernel.Get<ExecutiveRepository>();
userRepository = _kernel.Get<UserRepository>();
executiveSectionRepository = _kernel.Get<ExecutiveSectionRepository>();
executiveSectionMappingRepository = _kernel.Get<ExecutiveSectionMappingRepository>();
contentRepository = _kernel.Get<ContentRepository>();
}
...
Not sure what I am doing wrong but upon adding a new 'Executive' it bombs... I do understand it's trying to use separate contexts and that's the problem, but I 'm just not sure how to fix it. Apparently, the line in the NinjectWebCommon.cs class:
kernel.Bind<CMSContext>()
.ToSelf()
//.InSingletonScope();
.InRequestScope();
Is supposed to be the fix, but it isn't...
any ideas/suggestions?
You should be using NUGET package Ninject.Web.Mvc if you aren't already. This configures your application ready to use Ninject, other than your bindings. It looks like you are reasonably familiar with the bindings side of things already from what I can see in your CreateKernel() method.
Once your bindings are in place, you should not be creating Kernels in your controllers, this is because the Ninject.Web.Mvc library configures Ninject to create your controllers for you under the hood. Therefore any dependencies that you add to them should be automatically resolved.
So, you can use constructor injection to resolve your dependencies:
public class ExecutiveController : Controller
{
private IExecutiveRepository ExecutiveRepository;
private IUserRepository UserRepository;
private IExecutiveSectionRepository ExecutiveSectionRepository;
private IExecutiveSectionMappingRepository ExecutiveSectionMappingRepository;
private IContentRepository ContentRepository;
public ExecutiveController(
IExecutiveRepository executiveRepository,
IUserRepository userRepository,
IExecutiveSectionRepository executiveSectionRepository,
IExecutiveSectionMappingRepository executiveSectionMappingRepository,
IContentRepository contentRepository)
{
// Set the field values
this.ExecutiveRepository = executiveRepository,
this.UserRepository = userRepository,
this.ExecutiveSectionRepository = executiveSectionRepository,
this.ExecutiveSectionMappingRepository = executiveSectionMappingRepository,
this.ContentRepository = contentRepository;
}
public ActionResult Index(int id)
{
// Use one of your dependencies...
var executive = this.executiveRepository.GetExecutiveById(id);
}
}
Or you can use the [Inject] attribute which has the same effect:
public class ExecutiveController : Controller
{
[Inject]
public IExecutiveRepository executiveRepository { get; set; }
[Inject]
public IUserRepository userRepository { get; set; }
[Inject]
public IExecutiveSectionRepository executiveSectionRepository { get; set; }
[Inject]
public IExecutiveSectionMappingRepository executiveSectionMappingRepository { get; set; }
[Inject]
public IContentRepository contentRepository { get; set; }
public ExecutiveController()
{
}
public ActionResult Index(int id)
{
// Use one of your dependencies...
var executive = this.executiveRepository.GetExecutiveById(id);
}
}
You're creating a kernel per controller.
InRequestScope only ensures one instance per request per kernel.
So you need to adapt your setup of the kernel so there's only one kernel per web application. See:
Ninject.Web.Mvc
Tutorial
Youtube
This may not answer the question. But I tend to use the IDbContextFactory that EF provides you with and do something like this:
public interface IDefaultContextFactory : IDbContextFactory<CMSContext> {}
public class DefaultContextFactory : IDefaultContextFactory
{
private readonly Lazy<CMSContext> lazyContext = new Lazy<CMSContext>(() => new CMSContext());
public CMSContext Create()
{
return lazyContext.Value;
}
}
Then you just bind that, and when you need the context you can do something like this:
public class ExecutiveRepository : IExecutiveRepository, IDisposable
{
private readonly CMSContext context;
public ExecutiveRepository(IDefaultContextFactory contextFactory)
{
this.context = contextFactory.Create();
}
}
I believe #BatteryBackupUnit is correct, I would also consider using the above pattern for contexts.
This one is a follow up question to Dependency Injection using Unity
So , as a set up I have a CustomConfiguration.cs file which is supposed to populate from a config section in my web.config file
This is the signature for the file
public class CustomConfiguration : ICustomProcessorConfig, IEmailConfig, IReportConfig
{
#region Properties
private CustomProcessorConfig ConfigSection { get; set; }
#endregion
#region Constructors (1)
public CustomConfiguration()
{
ConfigSection = ConfigurationManager.GetSection("customConfiguration") as ConfigSection;
}
#endregion Constructors
#region ICustomConfiguration Members
public string Text { get { return ConfigSection.Text; } }
public string Subject { get { return ConfigSection.Subject; } }
public string SmtpHost { get { return ConfigSection.EmailSettings.SmtpHost; } }
public int SmtpPort { get { return ConfigSection.EmailSettings.SmtpPort; } }
These are the 3 files I have for Email Generation :
public interface IEmailManager
{
void SendEmail(string toAddress, string fromAddress, string subject, string body, bool htmlBody);
}
public interface IEmailConfig
{
string SmtpHost { get; }
int SmtpPort { get; }
}
And Finally I have the Email Manager which inherits the IEmailManager interface
public class EmailManager: IEmailManager
{
#region Constructors (1)
public EmailManager(IEmailConfiguration configuration)
{
CurrentSmtpClient = new SmtpClient
{
Host = configuration.SmtpHost,
Port = configuration.SmtpPort,
Credentials =
new NetworkCredential(configuration.UserName, configuration.Password)
};
}
#endregion Constructors
// send Mail is also implemented
}
Coming back to the previous question I have my set up like :
Container = new UnityContainer();
Container.RegisterType<ICustomConfiguration,CustomConfiguration>(new ContainerControlledLifetimeManager());
Container.RegisterType<IEmailManager, EmailManager>(new ContainerControlledLifetimeManager());
Container.RegisterType<IReportGenerator, ReportGenerator>(new ContainerControlledLifetimeManager());
Configuration = Container.Resolve<ICustomConfiguration>();
Emailer = Container.Resolve<IEmailManager>();
ReportGenerator = Container.Resolve<IReportGenerator>();
I'm getting a ResolutionFailedExceptionsaying The parameter configuration could not be resolved when attempting to call constructor for EmailManager.
I had a different DI setup and I would need the configuration information from IEmailConfig still. Is there a way of going past this ? I need the config information to proceed with sending the email as you can guess with my setup.
Am I binding different repo to my Container ? Or how should I change my EmailManager code ?
You need to register the IEmailConfig interface with the CustomConfiguration class in the container.
Container.RegisterType<IEmailConfig , CustomConfiguration >(new ContainerControlledLifetimeManager());
IEmailConfiguration missing mapping in the unity container. Need to add the concrete class that maps this interface