Can somebody please explain:
I am using MVC3/C#/Razor to build a project to get used to using MVC.
I am using the inbuilt account controller.
I am storing the account data in my local SQL database using Entity Framework to connect.
How can I easily generate interfaces for EF?
SO FAR I am using the plugin from: http://blog.johanneshoppe.de/2010/10/walkthrough-ado-net-unit-testable-repository-generator/#step1
This allows me to have an interface for my entities already created.
However, I know that I have to change my HomeController arguments to accept either the real repository or a fake one for testing.
I am completely lost!
Have a look at these. They will help and get you started :
http://www.asp.net/entity-framework/tutorials/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application
http://msdn.microsoft.com/en-us/library/gg416511(VS.98).aspx
For dependency injection, you can follow these steps :
Install-Package Ninject.MVC3 with nuget to your ASP.NET MVC 3 project (if your app is on version 3). This will basically do everything.
Then have the following on your controller :
private IMyModelRepository _myrepo;
public HomeController(IMyModelRepository myrepo)
{
_myrepo = myrepo;
}
Go to NinjectMVC3.cs file inside App_Start folder and add the following code to inside RegisterServices method :
private static void RegisterServices(IKernel kernel) {
kernel.Bind<IMyModelRepository>().To<MyModelRepository >();
}
Fire up your app and you should be up and running.
Related
I am trying to implement Identity Authentication using class library with code first in my asp.net MVC application.
At first I created test project with default selection "Individual User Accounts"
Using default templates with identity i found It creates some of the default files/classes like
IdentityModel.cs, IdentityConfig.cs(in APP_Start) & Startup.cs(partial class with anothe file in APP_Start Startup.Auth.cs),
AccountViewModel.cs, ManageViewModel.cs .
After that I searched for how to use identity related classes in Class Library, and using suggestions moved
all of above in class library project Names TestLib.
I have put IdentityModel.cs, IdentityConfig.cs in Model folder in TestLib and Startup in root of TestLib and change respective references in my web project.
I have removed OwinStartupAttribute from Startup.cs in web project and added OwinStartupAttribute to Startup.cs in TestLib.
And all seems to be working well also code first, but here I have some questions whether I am doing things correctly or not.
1) Is it OK to have Startup.cs in class library?
2) At now I have other models for my database in "ApplicationDbContext"
class, which creates table correctly in database.
So
1.Is it OK to use ApplicationDbContext instead of my CustomContext & can I
use this class library with my other application like some
windows application which do not use identity? OR
Should I keep separate context? OR
Is it possible to remove ApplicationDbContext and use my CustomContext?
I am following a tutorial on asp.net web api and mongodb here and on step 4 it talks about dependency injection and adding it to the start.cs in the ConfigureServices() method, however this doesnt seem to exist anymore. My web api templates startup.cs looks something like this...
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
So my question is where do I inject my DataAccess class to my web api project as a service? Thanks in advance.
As requested here is my api structure
under LGR.API is the auto generated folders and classes created by visual studios and starting a LGR.Datamodel is my custom class with my api stuff. Really new here... not sure if this correct at all. Feel free to critique with best practices as necessary
It looks like you've created an ASP.NET application, and your tutorial is for ASP.NET Core. Recreate your project and pick the "ASP.NET Core Web Application" template.
I can't seem to get Ninject to dispose objects in request scope in an ASP.NET MVC application with web API no matter what I do.
What I am doing:
Create a new ASP.NET Web Application with Visual Studio 2013. I select the MVC template and add a Web API to it (it also has ASP.NET Identity in the package by default)
I install Ninject.MVC5 package via nuget (install-package Ninject.MVC5)
I add a the following class to my application:
public class SomeDisposable : IDisposable
{
public void Dispose()
{
System.Diagnostics.Debug.WriteLine("test");
}
}
I add the following binding in NinjectWebCommon RegisterServices method
kernel.Bind().ToSelf().InRequestScope();
I add an object of the type SomeDisposable to the Home controller
public HomeController(SomeDisposable some)
{
}
I run the application and place a breakpoint in the HomeController constructor and the Dispose method of the SomeDisposable class. The controller receives an object (presumably from Ninject), the page loads but the Dispose method is never called.
At this point things are already broken but I add a Web API controller, install Ninject.WebApi packaged and repeat the experiment with the WebAPI controller and I get the same result.
I have read a bunch of questions including this one - Ninject doesn't call Dispose on objects when out of scope and the Ninject documentation https://github.com/ninject/Ninject.Web.Common/wiki/InRequestScope and they both indicate that due to the fact that Ninject.Web.Common includes a registration for the OnePerRequestHttpModule (yes it is there) the disposal should just work but it doesn't. I also tried adding the PerRequest module in the web.config and got an error saying that I can't have this section in integrated mode.
At this point I am lost. I suspect either integrated mode or OWIN have something to do with this but I have no idea how to debug it or what to do to fix it. Any suggestions?
The object not being disposed was a problem in Ninject.Web.Common before version 3.2.2, as discussed here: InRequestScope is failing to dispose objects
As you have been installing Ninject via nuget, you probably installed the oldest supported dependencies. This can be avoided by using:
Install-Packages Ninject.MVC5 -DependencyVersion Highest
Please verify that you are using the current version of all Ninject packages.
I've worked for days getting Identity Framework to work on my database first project. The book that I have appears only to explain how to implement it using database first, but it mentioned OWIN.
I can see from the code first version that we can get an OWIN instance of a database context by calling a static Create method on the database context that returns an instance of itself.
Isn't OWIN compatible with database first models and database contexts? If so, how do I get an instance of my database context using OWIN?
This is how you do it within code first:
Database Context
public class AppIdentityDbContext : IdentityDbContext<AppUser> {
public AppIdentityDbContext() : base("IdentityDb") {}
public static Create() {
return new AppIdentityDbContext();
}
}
Calling from OWIN start class:
app.CreatePerOwinContext<AppIdentityDbContext>(AppIdentityDbContext.Create);
I realised that the OWIN stuff isn't directly related to whether your application uses Entity Framework code first or database first.
The OWIN context is used to get to the UserManager, RoleManager etc. for ASP.NET Identity Framework. The DbContext specified in this context is really for use solely by these classes and not just for general use within your ASP.NET MVC application.
I'm now using a IoC Container to inject my DbContext into my controllers within my projects. (I'm using Ninject, but there are others such as Unity, MEF)
I have the following setup for my project:
MyProject.WebApp (dependent on MyProject.Domain)
MyProject.Domain
Inside my webapp I have the routine setup for couchbase and it works successfully (for example, if i put a simple get set in one of my controllers it works fine), but when I try to do the code in my class lib (MyProject.Domain) it does not connect successfully. I have tried to develop a class in my class lib that looks like this:
public static class CouchbaseManager
{
private static CouchbaseClient _instance;
static CouchbaseManager()
{
_instance = new CouchbaseClient();
}
public static CouchbaseClient Instance { get { return _instance; } }
}
In my Global.asax of my webapp I try to initialize this CouchbaseManager so i can use it in my class library, but no luck. I have added an app.config file to my class lib as well with all the correct settings. Can this be done?
Without knowing more about your specific error it's hard to say. Your class library, if referenced correctly, would live with the web app and so you would not need the app.config, the details in web.config should suffice: the section declaration and the couchbase section populated with your database details.
Check also if the library is setup to use the full .net version as it cannot run under the ".NET Framework Client Profile".
Personaly I would create another project for your data access layer and have your web app and domain library access it, you will have a better design that way and avoid having 2 instances of couchbase client which is not recommended.