-
Notifications
You must be signed in to change notification settings - Fork 277
Dependency injection #91
Comments
@RupertAvery yes, that is by design. Only default constructor. The Controller in this regard is just a placeholder, a container, if you will, for actionable methods. You can still achieve what you want though, but not via constructor dependency injection. Something like: On initialization:
In controller:
|
Thanks, however, it kind of breaks the whole point of dependency injection. I'm curious, is this an architectural limitation? Also noticed that the framework creates the controllers only once, whereas in MVC the controllers are created per request. This may have implications on how objects should be instantiated, if created in the constructor. |
I've taken a look at the code and seen how it works, thanks! |
@RupertAvery yw. Chromely is intentionally made super simple for most developers. But also flexible for advanced developers like you. If you want to make it work the way you want this is achievable. The Controller attribute was designed intentionally for that - ControllerPropertyAttribute. So you can leverage the Name and Route properties for that. Instead of creating the controller object on the fly using Activator, you use Name and the Route to access a pre-registered contoller in the Container. So you will have something like:
.. and retrieve the controller object in the RouteScanner. If this is considered a common requirement we can look into implementing it, but I bet you most developers will forget to add unique name and route which may cause collision. |
I understand, it's just that developers may be coming from an MVC background, so it might make sense to follow existing conventions, such as using HttpPost, HttpGet, Route attributes (Is copying an API for the purpose of compatibility a problem?) and also perhaps using .net core's existing IServiceCollection would also help devs looking to port existing code over. |
@RupertAvery actually this is "following existing convention". But not really MVC per se - NancyFx. Nothing here is written in stone, but simplicity is the key word. So I think we should consider changes but still keeping the architecture as simple as possible. |
Sure. I've written a MVC-style approach that only requires ChromelyConfiguration to have an interface, and have most everything in Chromely.CefSharp.WinApi that uses ChromelyConfiguration It's a separate project, right now I've named in Chromely.Mvc, maybe name it Chromely.Restful.Mvc? Or I could name it something else if I can't use Chromely. Initialization looks like this:
So not too different. There are some attributes HttpGet, HttpPost, etc. Controllers can look like this:
So it's only a matter of configuration, no major changes on Chromely side to use this method. Controllers are created on demand (transient) and using the serviceProvder created inside the configuration, so Dependency Injection just works. |
@RupertAvery ChromelyConfiguration is central to how Chromely works. Like you I love to program to interface too, as a matter of principle. But not everything is "better achieved" by interfaces. There was a reason why this approach was chosen. Don't get me wrong, I am not saying it cannot be changed, but I am not seeing a strong reason with what you want to do.
Chromely/src/Chromely.CefSharp.Winapi/Browser/Handlers/CefSharpContextMenuHandler.cs Line 40 in 15343a0
Chromely/src/Chromely.CefSharp.Winapi/Browser/Internals/ChromiumWebBrowserExtension.cs Line 249 in d4486da
But I am ready to learn if there are easier way around that. Why is ChromelyConfiguration a singleton? If a developer wants to have more than one window opened in a process, CefGlue and CefSharp will throw an exception if you register the the same scheme handlers and resource handlers more than once. Also there are cases like the link I sent where changes are easier done by accessing functionalities in a singleton pattern without a huge change to the framework. But the primary reason is the scheme and resource handlers registation. My suggestion for now is to use extension method or inheritance, then gradually we can have bandwidth for this change if need be. |
Thanks for the feedback! I've rewritten it as a separate configuration object, which works out much simpler and avoids changes to Chromely. |
@RupertAvery yw. But you are still going to share that with us, right? The attributes on Controller actions and IOC constructor injection are still something that I think we should have. @Fiahblade @FrankPfattheicher and I can help include that. |
Absolutely. Right now it's a standalone "add-on" to Chromely, as the main entry point is the CefSharpBoundObject which is injected, it was easy to create a new set of handlers without any Chromely code adjustments. Here are the features I've implemented:
Model binding means you can write your methods without ChromelyResponse and ChromelyRequest:
Of course, there is still a lot of work to do with model binding, but it currently works with Get and Post and most basic data types, object graphs and arrays. It's available here https://github.com/RupertAvery/Chromely.Mvc |
@RupertAvery i now added DI support for chromely controllers with commit e77afd1. |
@RupertAvery thanks. I will surely take Chromely.Mvc for a ride! Please will you add Chromely.Mvc to: |
I just realized I don't know how to update another wiki... I tried cloning
locally and pushing, but then I don't have rights.
…On Mon, May 20, 2019 at 7:45 PM mattkol ***@***.***> wrote:
Absolutely. Right now it's a standalone "add-on" to Chromely, ...
It's available here https://github.com/RupertAvery/Chromely.Mvc
@RupertAvery <https://github.com/RupertAvery> thanks. I will surely take
Chromely.Mvc for a ride!
Please will you add Chromely.Mvc to:
#63 <#63>
https://github.com/chromelyapps/Chromely/wiki/A-List-of-Apps-Built-with-Chromely
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#91?email_source=notifications&email_token=AAOSPA37KSDMOB2CC2R3JWTPWKFOXA5CNFSM4HL7HQ42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODVYR25Q#issuecomment-493952374>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAOSPA6Y4YOPCVM7GNBCMD3PWKFOXANCNFSM4HL7HQ4Q>
.
|
@RupertAvery you can add it to #63 and I will move it over. Thanks, |
@mattkol just to let you know I've updated Chromely.Mvc with Chromely 5.0.0.6. |
@RupertAvery thanks for the update. |
How are controllers dependency injected?
I tried creating a SimpleContainer and registering a dependency class and pass it onto the ChromelyConfiguration
But if I put anything in the constructor of my Controller, it never gets instantiated. The controller class is only instantiated if the constructor has no arguments.
The text was updated successfully, but these errors were encountered: