0

We built a framework library for logging, caching, emails, messages, images which uses Entity Framework 6.0. This library is used in multiple applications in our organization.

Whenever an app uses this library, has to follow certain guidelines to implement. The source app has to create <appsetting> values with same name which will used by dll. Dll reads the connection string based on the source web.config name (this connection string name is fixed).

In our new app, we are using .NET Core; but as our Framework library reads values from web.config file and .NET Core doesn't have one, how to use this from an .NET Core application?

2
  • Can you not rewrite the library to have a flexible configuration system and be based on .NET Standard? Should only be a minor refactoring. Commented Apr 23, 2020 at 4:30
  • I know that, if I have to rewrite why will ask this question. Internal we reuire many permissions to rewrite as its used enterprise wide. Let me know if there is way without making any changes Commented Apr 23, 2020 at 5:02

1 Answer 1

1

If you replace your usages of ConfigurationManager.AppSettings["key"] with using IConfiguration.Item["key"], that should be the least-invasive migration path, in my opinion. There are multiple more "fancy" options in .NET Core, but this should be the path of least resistance, IMHO.

If you need the library to work in both .NET Core and .NET Framework applications, I would recommend creating a small wrapper around the configuration, say, e.g.

public interface IAppSettings {
    public string this[string key] { get; }
}

And, then, creating two implementations of it, one for .NET Core, and one for .NET Framework. Something along the lines of

public class WebConfigAppSettings: IAppSettings {
     public string this[string key] => ConfigurationManager.AppSettings[key];
}

and, one for .NET Core, similar to:

public class NetCoreAppSettings: IAppSettings {

    public NetCoreAppSettings(IConfiguration source) => _source = source;

    public string this[string key] => _source[key];
}

Create the implementations outside of your common EF library.

Then, make your EF library classes needing configuration take a dependency on the IAppSettings interface, and use IoC/Dependency Injection to inject the correct implementation in each application using the library.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.