MVC Ninject: How to add NinJect bindings from an MVC Area project - asp.net-mvc-5

I've been using this blog example:
http://blog.longle.net/2012/03/29/building-a-composite-mvc3-application-with-pluggable-areas/
I have the concepts working in my solution. However, I'm trying to figure out a good way only add bindings to the kernel if a user has permissions to access a module/area.
I've read up some on the ServiceLocator but I was trying to stay away from it.
One thing I'm trying just to get things to work is user Contructor injection in the default constructor for a module. It's working but is a hack.
The pattern I'm using, each module project you create an class that inherits from AreaRegistion. When each module project builds, relevant files are copied to the Areas folder of the main web project. Then when the main project loads, reflection is used to load all module assemblies. Then when AreaRegistration.RegisterAllAreas() is called, it detects and loads all the modules with a class that inherits AreaRegistration.
I'd like to figure out an good way to access the Ninject kernel and add bindings in the module class that inherits from AreaRegistration. I would imagine initiating code to add bindings from the RegisterArea() override.
I'm looking for any suggestions on how to do this without resorting to the ServiceLocator.
Any ideas would be greatly appreciated.

For now I've found a better solution and that's to use NinjectModule. In the Plugin class, I'm going to create a class which inherits from NinjectModule. Then setup the pluging bindings in the Load overload.
Then use Kernel.Load in my main app bootstrapper to initialize the Load overloads in all plugin classes which inherit from NinjectModule.

Related

How to unit test non-exported class in UWP component (C++/WinRT)

I'm looking for some guidance on my strategy for unit testing UWP components.
I am developing a UWP component using the C++/WinRT projection. This component contains a helper class that manages communication with a Bluetooth device. I would like to write unit tests for that helper class (BthDevice) without exporting them to the UWP component's interface.
I started by following some guidelines for testing non-exported DLL classes. Specifically, I created a new UWP unit test project. I explicitly added my project's include directory, included BthDevice.h, and added the class' BthDevice.obj to the linker settings. I fought a few linker errors, and now it's falling over because I'm using precompiled headers in the UWP component. I've given up for now.
It feels like this should be a simple task, and that I'm going about it the wrong way. Any guidance is welcome! Thanks.

Entity Framework 6 Code First Migrations using Identity user models in a separate project

Is it possible using EF6 Code First and MVC5 to put all the models, views, and controllers that involve ASP.Identity into its own class library project. So that over multiple web applications you could use that same DLL and already have all the views / controllers / models and be using the same security database for multiple applications?
We have several web applications with separate databases and one security database that handles all of them, and we weren't sure how to keep this model now that we're moving to EF6 Code First and MVC5.
If it is possible could someone point me to a tutorial of something similar or give me a basic outline of steps to go through?
Or if there is a better way to achieve my goal, of having one set of code to handle ASP.NET-Identity security that I can plug that dll into multiple web applications and get the same logic and databases?
Or is this not a good idea in general?
Very open to suggestion and advice. I appreciate it.
Yes it is. We do this with every project that we have. The structure is very simple. Just create a class library project to your solution, add EF to the project, then reference the class library from your main project.
If using Code First Migrations be sure to select the class library project as the default project in the Package Manager console when running migrations or adding migrations.
Here is a pseudo solution structure for your solution
MySolution
- MyWebApp
reference: MyDAL
-MyDAL
reference: EF6
The advantage that I find to this is that you can then reference the "DAL" class library from say a companion console application or windows form application, or a companion website, even in a different solution, and they will use the same code base.
For example:
MySolution
- MyWebApp
reference: MyDAL
- MyDAL
reference: EF6
- MyOtherWebApp
reference: MyDAL
NOTE: Your data context will look for its connection string in the Web.config or App.config in the startup project. NOT the class library. This can be confusing at first... But once you think about how .NET compiles the application together into the final package, it makes sense.
If you're talking about creating one class library for an entire data layer shared between multiple projects, then that's easy enough. You can move all your models, your context, etc. into a class library and run migrations using the class library project. The other projects will just reference that class library and not have migrations of their own.
However, if you're talking about multiple databases and associated data layers, where project Foo has its own models, context and migrations and project Bar has its own models, context and migrations, while the class library has just the IdentityUser and IdentityDbContext, things get a little more complicated. You won't be able to combine any of these contexts. So in your Foo project you'd have to instantiate your context for Foo and your Identity context if you need to work with both. It's not a problem, per se, but it's something to be aware of.

Where do I put my snaplets?

I'm new to Haskell and Snap and I want to write a simple bug-tracking application. I set up a Snap project using $ snap init and it works. The problem is that I don't know how to go any further.
I want to create a snaplet Tickets that uses a database to store bug reports, and use that snaplet in my main web application.
I read the documentation about snaplets but I couldn't figure out where to put their source code (in the /snaplets directory, I suppose? If so, how do I load them?). Could anybody point me in the right direction?
Are you sure you need to create a separate snaplet for Tickets? The snaplet infrasructure is meant for reusable components such as session management, authentication, database access, admin panels, user management, etc.
Are you going to be using your Tickets self-contained module across multiple web applications? If so, then by all means go ahead and create a snaplet.
In any case, it sounds to me like you are at least in part asking how to organize a project with multiple "modules" or "parts" while using snap. I'll try to address this below. Please let me know if you feel I have missed the mark.
Assuming you don't need to create a separate snaplet for Tickets:
For functionality specific to a single site, I think you would be better off creating a few modules and developing the code for the functionality right inside your current application's package and module hierarchy. Here are a few points on how I have been organizing my snap projects of late:
I put my database-related code under modules in MyApp.DB.
namespace. You could have a MyApp.DB.Tickets that contains all
database calls needed for operation on your Tickets module.
I put all UI-related functionality under MyApp.UI. namespace. You
could put a MyApp.UI.Tickets module that contains your Handlers,
your Splices, your Forms and so on.
To handle forms, I use the excellent digestive-functors library. You may find this blog post helpful if you don't know them already.
I usually have a shared UI helper library under MyApp.UI.Helpers where I place common code used across all/most UI modules.
I usually have a shared Form helper library under MyApp.UI.Forms
Any code tangential to the fact that my application exposes a Web UI goes outside the MyApp.UI. namespace. So if my application needs to perform some offline analyses, I may put them under the MyApp.Analysis. namespace.
Once you define and export your Handlers under MyApp.UI.Tickets, you can go to your Site.hs file and wire them into your application at specific routes.
For inspiration on how to handle database connections, you can check out the postgresql-simple snaplet.
However, if you do need to create a snaplet:
A snaplet is simply a stand-alone application that uses the Snap.Snaplet infrastructure, (typically) has its own .cabal file and (typically) is its own Haskell package. Snap then gives you a way to embed or nest this re-usable, self-contained application in yet another snap application. Think a hierarchy of Russian dolls.
Assuming you performed something similar to what I described in the section above, you are now 95% ready to convert to a snaplet. Just name your application snaplet-tickets, expose a SnapletInit using makeSnaplet and use nestSnaplet in another snap application to include this reusable piece of functionality right there.

How can I include transformed dll to an Azure deployment

For my Worker role I run a custom task to modify the dll. However, when I use VS to publish the project to Azure the original binary is deployed and not the modified dll. Why is this happening? How to include the modified binary in the package from within VS
This is with sdk+tools v1.6
A possible workaround that I see, is to extract all the code/logic that requires assmebly modification in a separate class library project. Do the custom post-build modification on the class library.
In your worker add reference to that class library. If it also does not work when you add the reference as a project reference, then just copy the resulted modified assembly in a common external references folder for the solution, and add the reference from there (just browse to the final assembly).
I don't know the nature of your modification, but I think that if you cave all your "private" code in a separate class library it will be enough. As the worker implementation will just be calling a "Run()" or similar method of your class library, inside the Run() method of the RoleEntryPoint.

Create AutoMapper modules like Ninject modules?

It is possible to create automapper like modules (such thoses of ninject) so i can define my mappings from an external library here and include them in a bootstrap class ?
That is exactly what Bootstrapper does. All you have to do is implement IMapCreator and Bootstrapper will find and execute your mapping code at startup time.

Resources