Updated section Using generic .NET classes through a Facade

Updated wikipage Eiffel for .NET Integration.
	(Signed-off-by:javier).

git-svn-id: https://svn.eiffel.com/eiffel-org/trunk@2416 abb3cda0-5349-4a8f-a601-0c33ac3a8c38
This commit is contained in:
eiffel-org
2023-09-29 19:16:46 +00:00
parent 86e8c94ec5
commit 1d721b0162

View File

@@ -1,4 +1,4 @@
[[Property:modification_date|Fri, 29 Sep 2023 14:45:20 GMT]]
[[Property:modification_date|Fri, 29 Sep 2023 19:16:46 GMT]]
[[Property:publication_date|Wed, 27 Sep 2023 19:33:07 GMT]]
[[Property:title|Eiffel for .NET Integration]]
[[Property:weight|3]]
@@ -63,6 +63,66 @@ As noted above, Eiffel for .NET fully supports the powerful genericity mechanism
===Using generic .NET classes through a Facade===
Currently, Eiffel does not support consuming generics from C# classes. This tutorial demonstrates a workaround for this limitation by creating a Facade for a `List<string>` in C#
====Creating a Facade for a List====
A Facade simplifies access to complex components. In this case, we will create a Facade to manage a list of strings. The Facade will encapsulate the list's functionality and expose a more straightforward interface. Here's how you can do it:
<code>
using System.Collections;
namespace ListOfString;
/// <summary>
/// Facade for a List<string> that encapsulates the list's functionality and exposes a few methods
/// </summary>
public class ListOfString
{
private List<string> _list;
public ListOfString()
{
_list = new List<string>();
}
public void Add(string item)
{
_list.Add(item);
}
public bool Contains(string item)
{
return _list.Contains(item);
}
public void Remove(string item)
{
_list.Remove(item);
}
public IList GetList()
{
return _list.ToList();
}
}
</code>
====Creating a C# Library====
To consume the Facade in Eiffel, we need to create a C# library. I recommend following the tutorial on creating a class library with C# and .NET on Microsofts official site. You can access it [https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio?pivots=dotnet-7-0 here]. This tutorial guides you through the process of creating a class library using C# and .NET.
====Consuming the C# Library from Eiffel====
Finally, we need to consume the C# library from Eiffel.
Open the Eiffel configuration file (.ecf) of your project and add the following entry
<code><assembly name="ListOfString" location="$PATH_CS_LIB\ListOfString.dll"/></code>
====Conclusion====
By creating a Facade for a `List<string>` in C#, we can effectively consume C# generic features in Eiffel. This approach can be extended to other generic types as well.
===Enum types===
Eiffel for .NET supports .NET enum types implicitly. From the point of view of Eiffel, they are just considered as expanded classes. The only difference is in the code generation. Eiffel for .NET cannot declare new enum types yet.
@@ -73,16 +133,7 @@ Eiffel does not have the notion of `byref` argument passing. At the moment, Eiff
==Eiffel Compatibility with .NETCore 6.0 and Above ==
===Limitations with Generics Classes and Features===
Currently, Eiffel does not support the use of `Generics` Classes and `Features` when consuming .NET assemblies.
And the compiler does not yet use .NET generic for code generation
==== Workaround====
In some cases, it's possible to create a C# library to access these features using a `Facade` pattern. However, there are instances where creating a Facade is not feasible, such as with `Avalonia UI` or `EntityFramework` where it is required to use generic to instantiate main components.
===Limitations with "init" Only Setters===
===Limitations with "init" only setters===
Eiffel currently does not support the special `init` property that is only initialized in a block of code as part of the object initialization. More details can be found in the [https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/init C# 9.0 proposal]
@@ -90,7 +141,7 @@ Eiffel currently does not support the special `init` property that is only initi
A potential workaround is to create a C# library that uses a Factory pattern to build the required instance.
===Executing .NET Applications from EiffelStudio: Limitations===
===Executing .NET applications from EiffelStudio: Limitations===
Currently, some types of applications cannot be executed directly from the EiffelStudio IDE. For example Web APIs.
@@ -99,7 +150,7 @@ Currently, some types of applications cannot be executed directly from the Eiffe
As a workaround, you can execute the application from the command line. Use the `dotnet` tool with the following syntax: `dotnet <app_name>`.
===Debugging .NET Applications from EiffelStudio: Limitations===
===Debugging .NET applications from EiffelStudio: Limitations===
At the moment, EiffelStudio does not provide any support for debugging Eiffel .NETCore directly from the IDE.
@@ -108,7 +159,7 @@ At the moment, EiffelStudio does not provide any support for debugging Eiffel .N
A potential solution is to use the C# wrapper project generated by the Eiffel compiler (in W_code or F_code directory). VisualStudio can open this C# wrapper (via the associated `.csproj` file), and thus the associated Eiffel output (dll or exe), and after configuring Debugging profile within VisualStudio , it is possible to debug the code.
===Eiffel .NETCore Types of SDKs: Limitations===
===Eiffel .NETCore types of SDKs: Limitations===
In the current version, Eiffel .NETCore does not support directly different types of SDKs such as `Microsoft.NET.Sdk.Web`. The generated C# wrapper only target `Microsoft.NET.Sdk`.
@@ -117,7 +168,7 @@ In the current version, Eiffel .NETCore does not support directly different type
For other types of applications like Web APIs, you need to manually provide a `.csproj` file with the required SDKs and package dependencies. This allows the application to run and, if necessary, use VisualStudio to debug.
==Publishing the Eiffel Solution: Using the Generated C# Wrapper==
==Publishing the Eiffel solution: using the generated C# wrapper==
To publish the Eiffel solution on current platform or targeting other platform, use the `dotnet` tool on the generated C# wrapper project.