You can prepare an existing MDriven model for .NET development in Visual Studio, generate its C# model code, and include compiled CodeDress code when you upload the project to MDrivenServer.
When to use this workflow
Use this workflow when your MDriven application needs C# code in addition to model logic, for example to call a third-party library or implement logic that is not appropriate for OCL or EAL. CodeDress gives your C# code access to the strongly typed domain model.
Keep model logic in OCL or EAL where it fits. MDriven can inspect OCL expressions for cross-references and model-aware behavior; moving all derivations and rules to C# removes that visibility.
Prerequisites
Before you start, install the following software:
| Requirement | Purpose |
|---|---|
| Visual Studio 2022 or later | Creates, builds, and maintains the .NET project. Download it from Visual Studio. |
| MDriven Visual Studio extension | Opens the MDriven model in Visual Studio and provides Codegen. Download the current extension from MDriven Downloads. |
| MDriven Designer | Saves the existing model in expanded .ecomdl format before it is moved to the Visual Studio project.
|
Use the latest available MDriven Designer and Visual Studio extension. For framework version guidance and the .NET delivery model, see Documentation:Dotnet core.
Create and prepare the project
1. Create a .NET console application
- Start Visual Studio.
- Select Create a new project.
- Create a Console App project.
- Target .NET 6 or later.
- Create the project and confirm that Visual Studio opens the solution.
For an example of starting from an MDriven Framework Visual Studio project template instead, see HowTos:First MDriven Framework project.
2. Add the MDriven package
- In Solution Explorer, right-click the project and select Manage NuGet Packages.
- Search for
MDriven.MVC.Core. - Install the package in the project.
- Restore packages if Visual Studio prompts you to do so.
NuGet packages are the preferred way to deliver versioned MDriven assemblies for modern .NET projects. Do not rely on assemblies from a local installation when a corresponding MDriven NuGet package is available.
3. Save an expanded copy of the model
- Open the model in MDriven Designer.
- Select File → Save As.
- Set Save as type to
ecomdl (*.ecomdl). - Save the expanded model.
The .ecomdl file is the model file that you open and generate from in Visual Studio.
- Copy the
.ecomdlfile to the .NET project directory. - Copy all files related to that model to the same project.
- In Visual Studio, include the copied files in the project if they are not already visible in Solution Explorer.
Place the model files in the project before you generate code. This keeps the generated code and the model used by the build in the same project workflow.
5. Add the Assets_TK folder
Create or copy the model's Assets_TK folder into the project. It must be at the same folder level as the .ecomdl file.
For example, if the model is stored as:
MyApplication/
MyModel.ecomdl
Assets_TK/
then Assets_TK is a sibling of MyModel.ecomdl, not a subfolder of it.
This folder is required when you use CodeDress. The compiled assemblies used by CodeDress are uploaded through Assets_TK.
6. Open the model and generate C# code
- In Visual Studio, open the project's
.ecomdlfile. - Use the MDriven Codegen command to generate C# files for the model classes.
- Save all generated and project changes.
Generate code whenever you change the model structure. Structural changes include adding or changing classes, associations, and state machines.
For example, if you add a Customer class and associate it with Order, run Codegen before building so the generated C# model contains the new types and association members.
7. Build the solution
- Select Build → Build Solution.
- Resolve all build errors before you upload the project.
Build after every CodeDress change as well as after Codegen. Upload uses the latest compiled assembly; editing C# without building does not make the new CodeDress implementation available for upload.
Add CodeDress code
After Codegen, a model method with an empty EAL body can have a C# stub generated for it. Implement the generated stub when the method needs C# behavior.
For example, use a generated C# method when a model operation must call code supplied by a third-party NuGet package. Keep calculations and rules that can be expressed in OCL or EAL in the model where possible.
Upload readiness checklist
Before uploading to MDrivenServer, verify all of the following:
- The project targets .NET 6 or later.
- The
.ecomdlmodel and its related files are in the project. Assets_TKexists beside the.ecomdlfile when using CodeDress.- You ran Codegen after the latest structural model change.
- The solution builds with no errors after the latest C# or CodeDress change.
- The current compiled assemblies are present for upload through
Assets_TK.
Next steps
Once the project builds successfully, upload it to MDrivenServer using your normal Visual Studio-based upload workflow. From this point onward, maintain the model, run Codegen after structural changes, build the solution, and then upload the newly built project.
If you are moving an existing .NET Framework project rather than starting a new project, follow Documentation:Migrating From MDriven .NET Framework to .NET Core. For MDriven Turnkey running on .NET Core, see Documentation:MDriven Turnkey Core.
