Propose for your code, a package that has in its code particularities being that the version of the project makes reference to its current model of its two projects ASPNETFULL
and ASPNETCORE
as follows:
Create a Library Class and then edit your file .csproj
and in the content change to:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RuntimeFrameworkVersion>2.1.9</RuntimeFrameworkVersion>
<TargetFrameworks>net46;net461;net47;netstandard2.0;netcoreapp2.1;netcoreapp2.2</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net46'">
<Reference Include="System.Configuration" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net47'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.1'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netcoreapp2.2'">
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>
</Project>
that will make this Library
is compatible with both projects and the main code where you find the connection settings is:
namespace Library
{
public class FindConnection
{
#if NET46 || NET461 || NET47 || NETSTANDARD2_0
public System.Guid FullId { get; } = System.Guid.NewGuid();
public static string StringConnection
{
get
{
return System.Configuration.ConfigurationManager.ConnectionStrings["minhaconnection"].ToString();
}
}
#endif
#if NETCOREAPP2_1 || NETCOREAPP2_2
public System.Guid CoreId { get; } = System.Guid.NewGuid();
public FindConnection (Microsoft.Extensions.Configuration.IConfiguration configuration)
{
StringConnection = configuration.GetSection("ConnectionStrings:minhaconnection").Value;
}
public string StringConnection { get; }
#endif
}
}
in this code will happen a conditional compilation and so the code will fit in the two projects and of course each with its particularity.
To use this Library
add reference to your projects and on ASPNETFULL
use as follows:
string stringConnection = FindConnection.StringConnection;
and in the project ASPNETCORE
use with dependency injection configuring:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<FindConnection>(); // aqui
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
and in your controller
for example:
public class HomeController : Controller
{
public FindConnection FindConnection { get; }
public HomeController(FindConnection findConnection)
{
FindConnection = findConnection;
}
I would do even more in your Repository layer create a constructor with this class and do everything for dependency injection facility found in the ASPNETCORE.
Both are
Aspnet Core
? and the two applications are in the sameProject
?– novic
Have you considered the idea of passing the Connection string per parameter when instantiating the repository? So you leave the code less coupled :)
– Ronaldo Araújo Alves
Some applications are MVC (.Net Framework) and the new API is Aspnet Core. I already have many calls to these repositories and would not like to change all. If there is a way to get through it without having to change the signature of the methods, it would be better.
– Claudio Neto
There is a lot of nonsense in the market dividing the whole project mainly those who are imposing DDD, but, the database layer and its connection need to be independent of the project and in the project you pass the connectivity.
NetFramework
andNetCore
has different peculiarities but, this may be solved with a class where the same seeks the connection depending on the project. You need to exemplify how are today the two projects to get a real idea of the problem– novic
@Virgilionovic then, I have classes like Usuariorepository, for example, that need to be accessed by projects of both types. But in the repository, I simply use: public Static string Connectionstring => Configurationmanager.Connectionstrings["CS"]. Connectionstring;
– Claudio Neto
Netframework is which version?
– novic
@Virgilionovic Framework: 4.6.1 and Core: 2.1
– Claudio Neto