Emberfire
Emberfire
CC#
Created by Emberfire on 7/5/2024 in #help
Dependency injection approach for object, used only in a single method
I'm migrating an old ASP.NET 4.7.2 MVC application to ASP.NET Core and wish to learn more of the DI approach, since it's already deeply integrated into the Core framework. However, so far I've encountered some issues utilizing it. I understand DI is supposed to be used across the call chain, with every class having its dependencies injected. But what if you want to use an instance of a class only in a specific method of your current one? Are you supposed to inject it for the whole class, wasting performance on potentially heavy instantiations when it's not necessary? Here's an example;
c#
public class TestClass
{
public TestClass(ILogger<TestClass> logger)
{
}

public void DoSomething()
{
// Do work here
}

public void DoAnotherThing()
{
// Do work here
}

private void CallSomethingFromInnerClass()
{
// Problematic code
new InnerClass().DoInnerClassWork();
}
}

public class InnerClass
{
public InnerClass(ILogger<InnerClass> logger)
{
}

public void DoInnerClassWork()
{
// Do work here
}
}
c#
public class TestClass
{
public TestClass(ILogger<TestClass> logger)
{
}

public void DoSomething()
{
// Do work here
}

public void DoAnotherThing()
{
// Do work here
}

private void CallSomethingFromInnerClass()
{
// Problematic code
new InnerClass().DoInnerClassWork();
}
}

public class InnerClass
{
public InnerClass(ILogger<InnerClass> logger)
{
}

public void DoInnerClassWork()
{
// Do work here
}
}
I have a class that has many methods, each doing its own thing. Assume that class cannot be split into different smaller classes, maybe it's a controller. The inner class expects a logger with its own category. Now, I could instantiate it in the method and pass the existing logger, but that logger would have the wrong category (TestClass instead of InnerClass). I could also inject an IServiceProvider, however as I understand that is an anti-pattern and shouldn't be done when implementing DI. I could also introduce a [FromService] attribute, however that is also an anti-pattern, plus it only works for controller actions called from the environment, not private methods. What is the best approach here? Is the answer really injecting it for the whole class? What if the inner class is an ORM helper, do I need to create a DB connection every time?
27 replies
CC#
Created by Emberfire on 4/4/2023 in #help
❔ Set up Sustainsys.Saml2 configuration in Web.config programmatically
I'm trying to set up Sustainsys' Saml2 integration in an ASP.NET MVC 4 application. However, I need the identity provider options (such as Entity ID, sign-on URL, logout URL, signing certificate, service certificate and federation metadata) to be sourced from a UI I'm also writing. That means these options need to be edited on the fly. I've read into the component's documentation, however I don't see any way to programmatically set these options, except with Owin (which unfortunately won't work for my case). I tried manually creating configuration sections using Sustainsys' own classes, however much of the properties are either protected or outright lack setters. I also tried creating my own configuration sections which would override Sustainsys' ones, but during runtime the XML sections always get deserialized into Sustainsys' classes. I could always edit the Web.config as a plain XML file, but I feel this would not be an elegant solution. Is there any way to set (and subsequently change) the <sustainsys.saml2> tag and everything in it programmatically?
2 replies
CC#
Created by Emberfire on 4/4/2023 in #help
❔ Adding managed module to Web.config programmatically in ASP.NET
Hi everyone! I have a kind-of niche situation in which I am required to programmatically add a managed module (when running under IIS 10.0) to an ASP.NET MVC 4 application. The good news is that we actually want the app to restart, so dynamic loading is not required. Instead, I need to add the module to the application's Web.config file, under configuration/system.webServer/modules. I have tried to implement this in several ways, including opening the config file using Microsoft.Web.Administration, plain old System.Configuration or the ServerManager path. All of these resulted in failure since either the configuration is read-only or not found at all in the case with ServerManager. As a last resort, I used an XmlDocument instance and manually created the needed nodes myself, after which I restart the application manually. However, this seems like the wrong approach to me. Having to open the app's config file as an xml document and edit it seems very "wooden" to me and I suspect it might lead to more problems along the path. Does anyone have any insight on the issue? Something I could have done better? Of course, I can provide code examples of the other methods I tried. Thank you 🙂
2 replies