I have a small MVC application that connects to a single MYSQL database. I had it setup with Ninject to bind the connectionString during the application startup. The code looked like this:
Global.asax.cs:
protected void Application_Start()
{
...
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
NinjectControllerFactory.cs:
public class NinjectControllerFactory : DefaultControllerFactory
{
...
private class EriskServices : NinjectModule
{
public override void Load()
{
// Bind all the Repositories
Bind<IRisksRepository>().To<MySql_RisksRepository>()
.WithConstructorArgument("connectionString",
ConfigurationManager.ConnectionStrings["dbcMain"]
.ConnectionString);
}
}
}
Today my requirements have changed and I have to now support multiple databases. I would like to have each database connection string defined in the web.config file, like how it was before. The user selects which database they want to connect to during the application login.
What would be the easiest way to bind my repositories after the login? I'm assuming I would need to code the database binding in the login controller.
I am kind of a newbie to Ninject so any examples would be much appreciated!
As always, Thanks for the time and help!
.
I would probably Bind the repository to a Ninject.Activation.IProvider, and then create your own provider that pulls the connectionString from Session
Bind<IRisksRepository>().ToProvider<SessionConnectionProvider>();
then...
public class SessionConnectionProvider : Ninject.Activation.IProvider
{
#region IProvider Members
public object Create( Ninject.Activation.IContext context )
{
// use however you're accessing session here
var conStr = session.ConnectionString;
return new MySql_RisksRepository( conStr );
}
public Type Type
{
get { return typeof( IRisksRepository ); }
}
#endregion
}
Related
How can I use one DbContext with multiple application?
I have a WCF application (Net TCP binding) interface and implementation works fine with the DbContext. There is a need for API from the same application and I don't want to enable Http Binding on the WCF because of configuration and I have so many contracts. so I decided to import the service into asp.net core 2 via DI it works fine but works connect to Db via DbContext always returning null.
DB Context:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options){}
public AppDbContext()
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(#"Server=.\;Database=Database;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
}
Service implementation
public partial class GeneralService : IGeneralService, IDisposable
{
protected readonly AppDbContext Db = new AppDbContext();
public void Dispose()
{
Db.Dispose();
}
}
Asp.net core Start Up
public void ConfigureServices(IServiceCollection services)
{
const string connection = #"Server=.\;Database=Database;Trusted_Connection=True;MultipleActiveResultSets=true";
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connection));
services.AddSingleton<IGeneralService,GeneralService>();
services.AddMvc()
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver());
}
what am I doing wrong, what can I do I really don't want to use Proxy
connect to Db via DbContext always returning null.
I think that might be down to the fact that you're creating the DB context directly in the service class. You can/should inject your DbContext into your service instead. Something like:
public partial class GeneralService : IGeneralService, IDisposable
{
protected readonly AppDbContext Db;
public GeneralService(AppDbContext db)
{
Db = db;
}
// ... etc...
}
Further, since you're providing a connection string to the db in your Startup.cs you don't need the OnConfiguring method in your db context.
Finally, services shouldn't be singletons if they're using EF. See this answer which recommends the Request scope.
I'm trying to use SimpleInjector 2.7.3 (IoC container) within an Asp.Net MVC + Web API application.
I've had a couple of problems trying to set it up for both MVC and Web API on the same project until I found this link:
http://methoddev.com/blg/let-s-talk-software/310/simple-injector-in-asp-net-mvc-webapi
After following the link's example, here's what I got:
One of my Web API controllers:
public class UserController : BaseApiController
{
private readonly IUserService service;
public UserController(IUserService userService)
{
// I should point that IUserService is being injected correctly here
this.service = userService;
}
public IHttpActionResult Post(CreateUserRequest request)
{
return Ok();
}
}
The problem happens when I try to execute the Post operation. The CreateUserRequest class itself has a dependency.
public class CreateUserRequest : IValidatableObject
{
private readonly IValidator<CreateUserRequest> validator;
public CreateUserRequest(IValidator<CreateUserRequest> _validator)
{
// _validator is not being injected, I'm getting null here
validator = _validator;
}
public string SomeProperty { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
// My validation logic here must call the validator injected
// when the object was created.
return null;
}
}
I should point that IValidator is an interface from the FluentValidator package.
Anyway, when CreateUserRequest is instantiated the validator is null, which means it's not being injected.
When I'm creating the SimpleInjector Container I can see the type correctly registered, so I don't think that is a problem.
I did the following change to CreateUserRequest class:
public class CreateUserRequest : IValidatableObject
{
private readonly CreateUserRequestValidator validator;
// Changed here to the concrete class
public CreateUserRequest(CreateUserRequestValidator _validator)
{
validator = _validator;
}
// ...
}
So, I changed the interface to a concrete class and I'm still receiving a null there.
The only thing I can imagine is that this is somehow related to the custom dependency resolver suggested by the aforementioned link. I needed to use that in order to have the same dependency resolution logic for both MVC and Web API. Here's the code:
public class SimpleInjectorDependencyResolver : System.Web.Mvc.IDependencyResolver,
System.Web.Http.Dependencies.IDependencyResolver,
System.Web.Http.Dependencies.IDependencyScope
{
public SimpleInjectorDependencyResolver(Container container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.Container = container;
}
public Container Container { get; private set; }
public object GetService(Type serviceType)
{
if (!serviceType.IsAbstract && typeof(IController).IsAssignableFrom(serviceType))
{
return this.Container.GetInstance(serviceType);
}
return ((IServiceProvider)this.Container).GetService(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return this.Container.GetAllInstances(serviceType);
}
IDependencyScope System.Web.Http.Dependencies.IDependencyResolver.BeginScope()
{
return this;
}
object IDependencyScope.GetService(Type serviceType)
{
return ((IServiceProvider)this.Container).GetService(serviceType);
}
IEnumerable<object> IDependencyScope.GetServices(Type serviceType)
{
return this.Container.GetAllInstances(serviceType);
}
void IDisposable.Dispose()
{
}
}
I don't really know a lot of the plumbing behind MVC and Web API (specially the custom dependency resolver feature), so, I'm really stuck on this one.
I appreciate any help figuring that out. Thanks.
--UPDATE--
In addition to the answer given by Steven, I would like to leave a link to whoever falls into the same problem. It's a great resource:
https://brettedotnet.wordpress.com/2014/07/16/web-api-and-interface-parameters/
The reason why your view model object isn't auto-wired by Simple Injector is because both MVC and Web API don't build view model objects using the IDependencyResolver. So creating a special dependency resolver won't work. If you want to let your view models to be auto-wired, you will have to override the default model binder in MVC and Web API.
But I urge you not to do this. In my opinion, a model binder should just do data conversion and a view model should be a plain DTO. Although it is fine to mark view models with validation attributes, letting them have behavior using services that might even trigger any database communication is a big no-no in my book. This can complicate development tremendously.
This however means that this validator should be injected elsewhere. Without making any changes to your architecture, this basically means you will have to inject that validator in the controller instead:
public class UserController : BaseApiController
{
private readonly IUserService service;
private readonly IValidator<CreateUserRequest> validator;
public UserController(IUserService userService,
IValidator<CreateUserRequest> validator)
{
this.service = userService;
this.validator = validator;
}
}
Obviously this can easily complicate your controllers with extra dependencies and logic, but that's because validation is a cross-cutting concern that you would like to probably keep out of your controllers.
If you try to address this, you will eventually end up with a message passing architecture such as described here.
I'm using Ninject 3.0 to inject service layer data access classes into my controllers. I would like to add the client's domain user ID to these classes at runtime, but cannot figure out what approach I should use. Currently my NinjectModule looks something like this:
public class NinjectBindModule : NinjectModule
{
public override void Load()
{
Bind<ISomeRepo>().To<SomeRepo>();
}
}
My question, in two parts really is:
Should I use WithConstructorArgument to get the user ID into SomeRepo, or something else (property?). Can I even do this in the bind module, or does it have to be done at the kernel or controller level?
What method should I use to retrieve the client's domain user ID? I don't think I can use the Controller.User property at the kernel level or in the bind module, can I?
public class NinjectBindModule : NinjectModule
{
public override void Load()
{
Bind<ISomeRepo>().To<SomeRepo>();
Bind<IPrincipal>()
.ToMethod(ctx => HttpContext.Current.User)
.InRequestScope();
}
}
and then:
public class SomeRepo : ISomeRepo
{
private readonly IPrincipal _principal;
public SomeRepo(IPrincipal principal)
{
_principal = principal;
}
... some methods that will have access to the principal
}
I'm using ninject as my IoC and I wrote a role provider as follows:
public class BasicRoleProvider : RoleProvider
{
private IAuthenticationService authenticationService;
public BasicRoleProvider(IAuthenticationService authenticationService)
{
if (authenticationService == null) throw new ArgumentNullException("authenticationService");
this.authenticationService = authenticationService;
}
/* Other methods here */
}
I read that Provider classes get instantiated before ninject gets to inject the instance. How do I go around this? I currently have this ninject code:
Bind<RoleProvider>().To<BasicRoleProvider>().InRequestScope();
From this answer here.
If you mark your dependencies with [Inject] for your properties in your provider class, you can call kernel.Inject(MemberShip.Provider) - this will assign all dependencies to your properties.
I do not understand this.
I believe this aspect of the ASP.NET framework is very much config driven.
For your last comment, what they mean is that instead of relying on constructor injection (which occurs when the component is being created), you can use setter injection instead, e.g:
public class BasicRoleProvider : RoleProvider
{
public BasicRoleProvider() { }
[Inject]
public IMyService { get; set; }
}
It will automatically inject an instance of your registered type into the property. You can then make the call from your application:
public void Application_Start(object sender, EventArgs e)
{
var kernel = // create kernel instance.
kernel.Inject(Roles.Provider);
}
Assuming you have registered your role provider in the config. Registering the provider this way still allows great modularity, as your provider implementation and application are still very much decoupled.
I'm in my first time with DDD, so I'm begginer! So, let's take it's very simple :D
I developed an application using asp.net mvc 2 , ddd and nhibernate. I have a domain model in a class library, my repositories in another class library, and an asp.net mvc 2 application. My Repository base class, I have a construct that I inject and dependency (my unique ISessionFactory object started in global.asax), the code is:
public class Repository<T> : IRepository<T>
where T : Entidade
{
protected ISessionFactory SessionFactory { get; private set; }
protected ISession Session
{
get { return SessionFactory.GetCurrentSession(); }
}
protected Repository(ISessionFactory sessionFactory)
{
SessionFactory = sessionFactory;
}
public void Save(T entity)
{
Session.SaveOrUpdate(entity);
}
public void Delete(T entity)
{
Session.Delete(entity);
}
public T Get(long key)
{
return Session.Get<T>(key);
}
public IList<T> FindAll()
{
return Session.CreateCriteria(typeof(T)).SetCacheable(true).List<T>();
}
}
And After I have the spefic repositories, like this:
public class DocumentRepository : Repository<Domain.Document>, IDocumentRepository
{
// constructor
public DocumentRepository (ISessionFactory sessionFactory) : base(sessionFactory)
{ }
public IList<Domain.Document> GetByType(int idType)
{
var result = Session.CreateQuery("from Document d where d.Type.Id = :IdType")
.SetParameter("IdType", idType)
.List<Domain.Document>();
return result;
}
}
there is not control of transaction in this code, and it's working fine, but, I would like to make something to control this repositories in my controller of asp.net mvc, something simple, like this:
using (var tx = /* what can I put here ? */) {
try
{
_repositoryA.Save(objA);
_repositoryB.Save(objB);
_repositotyC.Delete(objC);
/* ... others tasks ... */
tx.Commit();
}
catch
{
tx.RollBack();
}
}
I've heared about NHibernateUnitOfWork, but i don't know :(, How Can I configure NHibernateUnitOfWork to work with my repositories ? Should I change the my simple repository ? Sugestions are welcome!
So, thanks if somebody read to here! If can help me, I appretiate!
PS: Sorry for my english!
bye =D
Session is NHibernate's unit of work. But you can always create your own abstraction of it.
using (var tx = Session.BeginTransaction) { ...
There is an excellent library called NCommon (source) that provides a great UnitOfWork implementation built right in. Version 1.1 allows you to do something like:
public class Foo
{
private readonly IRepository<Stuff> _repository;
public Foo(IRepository<Stuff> repository)
{
_repository = repository;
}
public void DoSomething()
{
using (var scope = new UnitOfWorkScope())
{
_repository.Save(a);
scope.Commit();
}
}
}
It integrates with the latest NHibernate and even uses NHibernate.Linq to provide some powerful querying features. You have little or nothing to build yourself and it works great out of the box.
Edit:
I elaborated on my example to show the full recommended way to use NCommon in a project with dependency-injection.
You can make the Session on your Repository a public property. Then you can do the following:
using(var tx = _repository.Session.BeginTransaction())
On a somewhat related note, this should all be inside of a service layer, not in your controller. Then the controller should have references to your services.