In MVC4,I inject the Controller by using the Ninject and I want to test the "Index".
Injection of the writing(The controller for injection):
public class NinjectDependencyResolver : IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver()
{
kernel = new StandardKernel();
AddBindings();
}
private void AddBindings()
{
kernel.Bind<IDB>().To<DB>();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
}
The IDB:
public interface IDB
{
IBugTrackRepository iBugTrackRepository { get; }
ICategoryRepository iCategoryRepository { get; }
...
...
IUserRepository iUserRepository { get; }
}
To achieve:
public class DB : IDB
{
public IBugTrackRepository iBugTrackRepository
{
get { return new BugTrackRepository(); }
}
public ICategoryRepository iCategoryRepository
{
get { return new CategoryRepository(); }
}
...
...
public IUserRepository iUserRepository
{
get { return new UserRepository(); }
}
}
To achieve:
public class BugTrackRepository : IBugTrackRepository
{
private DBEntities context = new DBEntities ();
public IQueryable<BugTrack> bugtrack
{
get { return context.BugTrack; }
}
...
//Other database operations...
}
The Controller:
public class HomeController : Controller
{
private IDB repository;
public HomeController(IDB repo)
{
repository = repo;
}
public ActionResult Index()
{
ViewBag.mytitle = "Home Page";
return View();
}
}
The Test Code:
[TestMethod]
public void TestIndex()
{
HomeController controller = new HomeController(??);
ViewResult result = controller.Index() as ViewResult;
Assert.AreEqual("Home Page", result.ViewBag.mytitle);
}
but this test is wrong and I don't konw how to instantiate this Controller.Please help me!Have any questions please leave a message.
Your Home's constructor contains IDB interface, so you need to pass it there.
As you have interface you can use mock objects (https://stackoverflow.com/questions/37359/what-c-sharp-mocking-framework-to-use) to imitate your IDB.
Related
I am trying to implement dependency injection in web api 2 but unable to do so. Getting object reference not set error.
Here is my implementatoin
Model:
public class Product
{
.....
}
Interface:
public interface IProductRepository
{
IEnumerable<Product> GetAll();
.....
}
Interface Implementation:
public class ProductRespository : IProductRepository, IDisposable
{
private ApiContext _context;
public ProductRespository(ApiContext context)
{
this._context = context;
}
public ProductRespository()
{
}
public IEnumerable<Product> GetAll()
{
return _context.Products.OrderBy(o => o.Name);
}
......
}
And Ninjectwebcommon.cs class:
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static void Start(){....}
.....
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
//kernel.Bind<IRepo>().ToMethod(ctx => new Repo("Ninject Rocks!"));
kernel.Bind<ApiContext>().ToSelf().InRequestScope();
kernel.Bind<IProductRepository>().To<ProductRespository>().InRequestScope();
}
}
And finally in my controller ProductController:
public class ProductController : ApiController
{
private IProductRepository _productRepository = null;
public ProductController(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public ProductController()
{
}
public IHttpActionResult GetAllProducts()
{
return Ok(_productRepository.GetAll());
}
}
When getallproducts controller method is hit i get this exception
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I think this is related to your interface in ProductController.
Try like this:
public IProductRepository _productRepository { get; set; }
I had the same problems, but using given codeline helped me / us out.
I want to inject to Hangfire AuthorizationFilter my custom serice and do something with it an i face problem my service is always null.
Owin startup class :
public class Startup
{
public void Configuration(IAppBuilder app )
{
GlobalConfiguration.Configuration
.UseSqlServerStorage("SomeContext")
.UseNinjectActivator(new Bootstrapper().Kernel);
app.UseHangfireDashboard("/hangfire", new DashboardOptions { AuthorizationFilters = new[] { new RestrictiveAuthorizationFilter() } });
app.UseHangfireServer();
}
}
Custom filter :
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
[Inject]
public IUserService _userService { get; set; }
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}
Ninject kernel :
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(x => { x.From(typeof(BookService).Assembly).SelectAllClasses().EndingWith("Service").BindDefaultInterface(); });
kernel.Bind<LibraryDBContext>().ToSelf().InRequestScope();
kernel.Bind<IBackgroundJobClient>().To<BackgroundJobClient>();
}
I don't see my error could you help me ?
You can pass the kernel as an argument to RestrictiveAuthorizationFilter constructor and ask for the IUserService.
public class RestrictiveAuthorizationFilter : IAuthorizationFilter
{
public IUserService _userService { get; set; }
public RestrictiveAuthorizationFilter(IKernel kernel)
{
_userService = kernel.Get<IUserService>()
}
public bool Authorize(IDictionary<string, object> owinEnvironment)
{
//do something with _userService but it is always null
return true;
}
}
I'm using MVC pattern in my application. For each model class I have a controller one. All controller classes have a saveOrUpdate() method. I am wondering if this is enough to create an Interface which defines said method, and then all controller implements it.
Please note that saveOrUpdate() receive a model class as a parameter. So it would be something like UserController#saveOrUpdate(User user), CourseController#saveOrUpdate(Course course), AppleManager#saveOrUpdate(Apple apple).
I think what you need is generic repository which implements generic functionality for a given entity. I've recently started implementing Repository Pattern along with Unit of Work in my MVC projects. Here is how I do that.
MyDbContext.cs:
public class MyDbContext : DbContext
{
public MyDbContext() : base("name=DefaultConnection”)
{
}
public System.Data.Entity.DbSet<User> Users { get; set; }
public System.Data.Entity.DbSet<Course> Courses { get; set; }
}
Unit of Work:
public class UnitOfWork : IUnitOfWork
{
//private variable for db context
private MyDbContext _context;
//initial db context variable when Unit of Work is constructed
public UnitOfWork()
{
_context = new MyDbContext();
}
//property to get db context
public MyDbContext Context
{
//if not null return current instance of db context else return new
get { return _context ?? (_context = new MyDbContext()); }
}
//save function to save changes using UnitOfWork
public void Save()
{
_context.SaveChanges();
}
}
Generic Repository:
public class RepositoryBase<T> : IRepositoryBase<T> where T : class
{
protected readonly IUnitOfWork _unitOfWork;
private readonly IDbSet<T> _dbSet;
public RepositoryBase(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_dbSet = _unitOfWork.Context.Set<T>();
}
public virtual void Save()
{
_unitOfWork.Save();
}
public virtual void Add(T entity)
{
_dbSet.Add(entity);
_unitOfWork.Save();
}
//Similarly you can have Update(), Delete(), GetAll() implementation here
}
Entity Repository inheriting from generic repo:
public class UserRepository:RepositoryBase<User>,IUserRepository
{
public UserRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
{
}
//Here you can also define functions specific to User
}
Controller:
public class UserController : Controller
{
private readonly IUserRepository _dbUserRepository;
public UserController(IUserRepository dbUserRepository)
{
_dbUserRepository = dbUserRepository;
}
// GET: /User/
public ActionResult Index()
{
var users = _dbUserRepository.GetAll();
return View(users.ToList());
}
}
create an interface
interface ISave
{
void Save(object obj);
}
now in your controller implement it.
public class AppleControler : Controller , ISave
{
public void Save(Object obj)
{
//you can cast your object here.
}
}
Option two
interface ISave<T>
{
void Save(T obj);
}
public class AppleControler : Controller , ISave<Apple>
{
public void Save(Apple obj)
{
}
}
I have a Model :
public class Post:
{
public string Description { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public Guid UniqId { get; set; }
public DateTime CreatedDate { get; set; }
public byte[] RowVersion { get; set; }
}
and i have a DataLayer that has EfContext and Iunitofwok pattern
using System.Data.Entity;
namespace Data
{
public interface IUnitOfWork
{
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
int SaveChanges();
}
}
using System.Data.Entity;
using DomainModel;
namespace Data
{
public class EfContext : DbContext, IUnitOfWork
{
public DbSet<Post> Post { get; set; }
public DbSet<Comment> Comments { get; set; }
public DbSet<Category> Categories { get; set; }
public EfContext()
: base("TavanGruop")
{
}
#region IUnitOfWork Members
public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
return base.Set<TEntity>();
}
#endregion
}
}
in another project i have repository
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using Data;
namespace ServiceLayer.ServiceDbSet
{
public class ServiceContext<T> : IServiceDbSet<T> where T : class
{
private readonly IDbSet<T> _dbSet;
public ServiceContext(IUnitOfWork iUnitOfWork)
{
_dbSet = iUnitOfWork.Set<T>();
}
#region IContext<T> Members
public bool TryGet(Func<T, bool> predicate, out T entity)
{
entity = List(predicate).SingleOrDefault();
return entity != null;
}
public T Get(Func<T, bool> predicate)
{
return List(predicate).Single();
}
public virtual List<T> List(Func<T, bool> predicate = null)
{
IEnumerable<T> result = _dbSet.AsEnumerable();
if (predicate != null)
result = result.Where(predicate);
return result.ToList();
}
public T Add(T t)
{
_dbSet.Add(t);
return t;
}
public void Delete(Func<T, bool> predicate)
{
List(predicate).ToList().ForEach(p => _dbSet.Remove(p));
}
public void Delete(T t)
{
_dbSet.Remove(t);
}
#endregion
}
}
and service
using Data;
using DomainModel;
using ServiceLayer.ServiceDbSet;
namespace ServiceLayer.EfServices
{
public class EfPostService : ServiceContext<Post>
{
public EfPostService(IUnitOfWork uow)
: base(uow)
{
}
}
}
and this my services
using Data;
using ServiceLayer.EfServices;
namespace ServiceLayer
{
public class Services
{
private readonly IUnitOfWork _unitOfWork;
private EfPostService _post;
private EfCommentService _comment;
public Services(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public EfPostService Post
{
get { return _post ?? new EfPostService(_unitOfWork); }
set { _post = value; }
}
public EfCommentService Comment
{
get { return _comment ?? new EfCommentService(_unitOfWork); }
set { _comment = value; }
}
}
}
I use tructuremap for dependency injection
using System;
using System.Web.Mvc;
using System.Web.Routing;
using Data;
using StructureMap;
namespace MVCTemplateProject
{
public static class StructuremapMvc
{
public static void InitStructureMap()
{
ObjectFactory.Initialize(x => x.For<IUnitOfWork>().HttpContextScoped().Use(() => new EfContext()));
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}
}
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
}
in base controller i get Iunitofwork
using System.Web.Mvc;
using Data;
using ServiceLayer;
namespace MVCTemplateProject.Controllers
{
public class BaseController : Controller
{
public readonly IUnitOfWork Context;
public readonly Services DataContext;
public BaseController(Data.IUnitOfWork context)
{
Context = context;
DataContext = new Services(context);
}
}
}
and this my home controller
using System.Web.Mvc;
using Data;
using DomainModel;
namespace MVCTemplateProject.Controllers
{
public class HomeController : BaseController
{
public HomeController(IUnitOfWork context)
: base(context)
{
}
public ActionResult Index()
{
return View(DataContext.Post.List());
}
}
}
how can i test index action with moq
i write this test but i get error
public void IndxShouldReturnListOfPage()
{
var posts = new List<Post>
{
new Post {Name = "Test"},
new Post {Name = "Test"},
new Post {Name = "Test"},
new Post {Name = "Test"}
};
var efContext = new Mock<EfContext>();
var mockRepository = new Mock<ServiceLayer.Services>(efContext);
mockRepository.Setup(x => x.Post.List(It.IsAny<Func<Post, bool>>())).Returns(posts);
var controller = new HomeController(efContext.Object);
List<Post> model = controller.DataContext.Post.List();
var result = controller.Index() as ViewResult;
Assert.AreEqual(model.Count, 4);
Assert.AreEqual(string.Empty, result.ViewName);
}
i get it and i test the project
[TestFixture]
public class PostControllerTest
{
#region Setup/Teardown
[SetUp]
public void Setup()
{
UnitOfWork = TestHelper.TestHelper.GetMockUnitOfWork();
PostDbSet = TestHelper.TestHelper.GetMockPostDbSet();
Posts = TestHelper.TestHelper.GetFakePosts();
TagDbSet = TestHelper.TestHelper.GetMockTagDbSet();
CategoryDbSet = TestHelper.TestHelper.GetMockCategoryDbSet();
PostController = new PostController(UnitOfWork.Object, PostDbSet.Object, CategoryDbSet.Object, TagDbSet.Object);
}
#endregion
private Mock<IServiceDbSet<Post>> PostDbSet { get; set; }
private Mock<IServiceDbSet<Tag>> TagDbSet { get; set; }
private Mock<IServiceDbSet<Category>> CategoryDbSet { get; set; }
private PostController PostController { get; set; }
private List<Post> Posts { get; set; }
private Mock<IUnitOfWork> UnitOfWork { get; set; }
[Test]
public void DetailShouldRedirectToHomePageWhenNotFoundPostOrPostIdIs0()
{
PostDbSet.Setup(x => x.Get(It.IsAny<Func<Post, bool>>())).Returns(Posts.Find(post => post.Id == 100));
var redirectToRoutResult =
(RedirectToRouteResult) PostController.Details(100);
Assert.AreEqual("Index", redirectToRoutResult.RouteValues["action"]);
Assert.AreEqual("Home", redirectToRoutResult.RouteValues["Controller"]);
}
[Test]
public void DetailShouldShowPostById()
{
PostDbSet.Setup(x => x.Get(It.IsAny<Func<Post, bool>>())).Returns(Posts.First());
var result = PostController.Details(1) as ViewResult;
var model = result.Model as PostViewModel;
Assert.AreNotEqual(null, model);
Assert.AreEqual("I Can", model.Title);
Assert.AreEqual(string.Empty, result.ViewName);
}
[Test]
public void ListOfStringTagMustConvertToListOfTagCollection()
{
PostViewModel postViewModel = TestHelper.TestHelper.GetFakePostViewModel();
PostController.Create(postViewModel);
//Assert.AreEqual(postViewModel.RawTags[0], postViewModel.RawTags.First().Name);
}
If I have the following setup, how can I configure my container to use the same database, when objects are created in the same context
public class Database { }
public interface IRepository { Database Database { get; } }
public interface IFooRepository : IRepository { }
public interface IBarRepository : IRepository { }
public class FooRepository : IFooRepository
{
public Database Database { get; private set; }
public FooRepository(Database database) { this.Database = database; }
}
public class BarRepository : IBarRepository
{
public Database Database { get; private set; }
public BarRepository(Database database) { this.Database = database; }
}
public class Consumer
{
public IFooRepository fooRepository { get; private set; }
public IBarRepository barRepository { get; private set; }
public Consumer(IFooRepository fooRepository, IBarRepository barRepository)
{
this.fooRepository = fooRepository;
this.barRepository = barRepository;
}
}
[TestClass]
public class ConfigurationTest
{
private IWindsorContainer container;
[TestMethod]
public void SameDatabaseIsUsed()
{
Consumer consumer = container.Resolve<Consumer>();
IFooRepository fooRepository = consumer.fooRepository;
IBarRepository barRepository = consumer.barRepository;
Assert.AreEqual(fooRepository.Database, barRepository.Database); //FAILS
}
[TestMethod]
public void DifferentDatabaseIsUsed()
{
Consumer consumer = container.Resolve<Consumer>();
IFooRepository fooRepository = consumer.fooRepository;
Consumer consumer2 = container.Resolve<Consumer>();
IBarRepository barRepository = consumer2.barRepository;
Assert.AreNotEqual(fooRepository.Database, barRepository.Database); //PASSES
}
[TestInitialize]
public void SetUp()
{
container = new WindsorContainer();
container.Register(
Component.For<Database>().ImplementedBy<Database>().LifeStyle.Transient,
AllTypes.FromThisAssembly()
.BasedOn<IRepository>().WithService.FromInterface()
.Configure(c => c.LifeStyle.Transient),
Component.For<Consumer>().ImplementedBy<Consumer>().LifeStyle.Transient
);
}
}
EDIT:
I have tried to use a custom lifestyle, but i cannot figure out what I can use to detect that i have switched context
public class DatabaseLifestyleManager : AbstractLifestyleManager
{
private CreationContext context;
private Database database;
private Database Database
{
get
{
if (database == null) database = new Database();
return database;
}
set
{
database = null;
}
}
public override object Resolve(CreationContext context)
{
if (this.context!=null && this.context.??? == context.???)
return Database;
else
{
this.context = context;
Database = null;
return Database;
}
}
public override void Dispose()
{
database = null;
context = null;
}
}
......
Component.For<Database>().ImplementedBy<Database>().LifeStyle.Custom(typeof(DatabaseLifestyleManager)
You always get a new instance when requesting a transient component, if it's not what you want don't use the transient lifestyle :-)
Why would you register a transient component, but attempt to resolve the same object depending on some kind of "context"? Most likely the lifestyle is wrong for the situation, and you will be in trouble trying to coerce it into something it's not.
What you want is something like a contextual lifestyle, mentioned in this article.
The below two gists have an implementation for this:
http://gist.github.com/400979
http://gist.github.com/400980
This will allow you do this:
Register(Component.For<Database>().LifeStyle.Scoped())
[TestMethod]
public void SameDatabaseIsUsed()
{
using (container.BeginScope())
{
Consumer consumer = container.Resolve<Consumer>();
IFooRepository fooRepository = consumer.fooRepository;
IBarRepository barRepository = consumer.barRepository;
Assert.AreEqual(fooRepository.Database, barRepository.Database); // YAY!
}
}
Hope this helps!
A contextual lifestyle is included in the Castle.Windsor.Lifestyles contrib project.
I came up with this solution myself by implementing IDisposable, so that I can use a kind of sessionscope for the Database
Would this be a valid way to handle this situation?
All test passes, but there is some added functionality, that must be implemented in all my future consumers of the repositories:
public class Database { }
public interface IRepository : IDisposable { Database Database { get; } }
public interface IFooRepository : IRepository { }
public interface IBarRepository : IRepository { }
public abstract class BaseRepository : IDisposable
{
public BaseRepository(Database database) { this.Database = database; }
public Database Database { get; private set; }
public void Dispose() { Database = null; }
}
public class FooRepository : BaseRepository, IFooRepository
{
public FooRepository(Database database) : base(database) { }
}
public class BarRepository : BaseRepository, IBarRepository
{
public BarRepository(Database database) : base(database) { }
}
public abstract class BaseConsumer : IDisposable
{
public abstract void Dispose();
}
public class Consumer : BaseConsumer
{
public IFooRepository fooRepository { get; private set; }
public IBarRepository barRepository { get; private set; }
public Consumer(IFooRepository fooRepository, IBarRepository barRepository)
{
this.fooRepository = fooRepository;
this.barRepository = barRepository;
}
public override void Dispose()
{
this.fooRepository.Dispose();
this.barRepository.Dispose();
}
}
[TestClass]
public class ConfigurationTest
{
private IWindsorContainer container;
[TestMethod]
public void SameDatabaseIsUsed()
{
IFooRepository fooRepository;
IBarRepository barRepository;
using (Consumer consumer = container.Resolve<Consumer>())
{
fooRepository = consumer.fooRepository;
barRepository = consumer.barRepository;
Assert.AreEqual(fooRepository.Database, barRepository.Database); //FAILS
}
Assert.IsNull(fooRepository.Database);
Assert.IsNull(barRepository.Database);
}
[TestMethod]
public void DifferentDatabaseIsUsed()
{
IFooRepository fooRepository;
IBarRepository barRepository;
using (Consumer consumer = container.Resolve<Consumer>())
fooRepository = consumer.fooRepository;
Assert.IsNull(fooRepository.Database);
using (Consumer consumer2 = container.Resolve<Consumer>())
barRepository = consumer2.barRepository;
Assert.IsNull(barRepository.Database);
}
[TestInitialize]
public void SetUp()
{
container = new WindsorContainer().Register(
Component.For<Database>().ImplementedBy<Database>().LifeStyle.Singleton,
AllTypes.FromThisAssembly()
.BasedOn<IRepository>().WithService.FromInterface()
.Configure(c => c.LifeStyle.Transient),
Component.For<Consumer>().ImplementedBy<Consumer>().LifeStyle.Transient
);
}
}