.NET Core Dependency Injection: is it applicable to Wisej?

0
0

Hi,
Recently I worked in a project writing the backend for a mobile application in the form of an ASP.NET Core 3 Web API. I was very pleasantly surprised by the Dependency Injection mechanism of .NET Core, which allowed the injection of all required services in the controller constructors. One of the dependencies was the Entity Framework DbContext.

I was wondering if such an approach can be followed in a Wisej application. There are two points that make me hesitateto use it:

a. The wisej Forms are not sort-living objects like the API Controllers, which get instantiated per request and disposed immediately. This short lifetime made the injection of the DBContext ideal. This will not be the case for Wisej Forms (or Windows Forms for that matter).

b. Injecting in the constructor can be a problem in the case of Usercontrols. Although there can be constructors with parameters, the designer needs and uses a parameterless constructor.

I would love to hear your views about this. Mainly, if there is a point bothering! Luca, Frank, Levie, Tiago…  everybody’s input will be valuable!

All the best,
Alex

  • You must to post comments
0
0

That was fast! Thank you, Luca! It certainly helps, this is what I thought too.
Thanks again and have a peaceful weekend.

Alex

  • You must to post comments
0
0

Dependency Injection and the related concepts are not new to .NET Core. In fact .NET Core doesn’t provide any support for DI. A lot of bloggers get it wrong because they think that .NET Core is the same as ASP.NET Core. For DI in .NET Core you need to use a DI library. Microsoft provides one, but there are many. ASP.NET Core supports DI and it’s mixed in their application builder architeture based on OWIN.

In, lets call it standard .NET or old .NET, dependency injection existed since version 1.1 I believe. It’s nothing else than a coding patterns and a related service container. In .NET (old, standard, core, new) it is based on System.ComponentModel.IServiceProvider. Which is all it is. A central place where you can retrieve and store instances or singletons implementing certain interfaces.

You can either implement your own or use one of the many containers. The designer architecture in Visual Studio (and Wisej) it uses it heavily. I usually create my own in projects where I need it and avoid all the overhead and quirky syntax.

I guess this is a good list: https://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx

In Wisej you can rely on the Application.Session to store session instances.

Adding parameters to constructors is a necessity in .NET Core because those constructors are called on every round trip since those objects are destroyed and recreated every time. Otherwise you’d have to retrieve the provider from the session. It doesn’t make sense in Wisej because the object is persisted so you can keep a reference to the service.

Injecting the DbContext in a controller constructor is a way to preserve the DbContext instance across requests. There is no need to do that in Wisej.

HTH

 

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.