First of all, I apologize for this thread because it’s not game development related but more application development.
I’m new to Unity and last few hours I’m trying to find a way to reference my existing .NET MVC web project using Unity and reuse my service methods in Unity. Services that I’m trying to reuse are pure backend services that filter, fetch and create objects from/to my local database which is SQL Server.
I’m wondering is there way to do it from Unity or am I supposed to make connection to database from Unity and implement them in Unity as well?
I saw in few threads that there are ways to include .dll files through Assets folder or [DllImport] but in my case I don’t have any .dll, but only .cs and .csproj files because it’s all locally developed.
Unity is built around Mono, not Microsoft’s .NET Framework - Mono is modelled after .NET Framework but the similarity is only on the API side, in terms of completeness there are differences as well as behaviour in some cases. Unity also adopted the .NET Standard but again, .NET Standard is incompatible with .NET Framework.
I don’t know for certain but I would be surprised if Unity’s .NET has native support for MS SQL. The threads that mention copying DLL files likely refer to adding the MS SQL DLLs to your Unity project so that you get to use the classes in those Assemblies. A similar thing is sometimes done to access the Windows.Drawing namespace.
However, the downside is that as soon as you take a DLL from Microsoft’s .NET Framework and put it in your Unity project, that project will only work under Windows (editor and build).
If that limited set of platforms is not an option you need to adapt the existing code by removing all .NET Framework references that are not supported by Unity (Mono or .NET Standard).
Yes, I thought that would be the case but I hoped that I’m wrong and there is some lib or annotation or api that could help us.
So, if I understood you well, the easiest way and perhaps the only one is to make connection and try to fetch data from the SQL using SQLConnection class. Right?
Generally speaking, you don’t want your Unity app (or any other client app) to access a database directly, for a lot of reasons, security and practicality being the two most important ones (you don’t want your users to install an SQL Server instance just to use your app right?)
Since your other project is an ASP.NET MVC one, why don’t you just expose some web API endpoints that your Unity app can access easily via simple HTTP requests?
Now if you really have to access your SQL Server database from Unity, I think Mono supports System.Data.SqlClient.SqlConnection class which allows you to open a connection to a SQL Server database and issue commands/queries, something like this:
// This is why it's a bad idea to directly access a database from a client
var connectionString = "Server=MyServer; Database=DB; User ID=sa; Password=password;";
using (var conn = new System.Data.SqlClient.SqlConnection(connectionString))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
var sql = "SELECT * FROM table";
cmd.CommandText = sql;
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var firstName = (string) reader["FirstName"];
var lastName = (string) reader["LastName"];
Debug.Log($"Name: {firstName} {lastName}");
}
}
}
}
Alternatively, you need to manually grab DLLs from nuget NuGet Gallery | Microsoft.Data.SqlClient 5.2.1 (nuget packages are just zip files with DLLs inside), copy them to your Unity Assets folder, and use Microsoft.Data.SqlClient.SqlConnection class to access your DB. This step would also be necessary if you want to create a shared library (DLL) between your MVC and Unity app that does DB access.
I consider this database access as a serious flaw of the application of course. That’s one the reasons why am I trying to achieve dependency injection in Unity and to reference my existing project. I wasn’t sure if there is more elegant solution than the endpoint exposure.
I will definitely try to make this with endpoint and see what’s gonna happen.
Exposing a Web API to your website/server IS the elegant solution
Also referencing an external library (assembly/DLL) is not dependency injection. It’s just a convenient way to “add more code” to your application without having to manually copy/paste that said code, so the flaw persists, just not anymore in your Unity’s app assembly (DLL) but your MVC assembly (DLL), which will also be shipped with your Unity app…