Skip to content

Commit

Permalink
Merge pull request #56 from LogoFX/feature/model-template-2
Browse files Browse the repository at this point in the history
Feature/model template 2
  • Loading branch information
godrose authored May 29, 2020
2 parents 9b0648f + a28168f commit 7a012af
Show file tree
Hide file tree
Showing 44 changed files with 773 additions and 183 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,4 @@ MigrationBackup/
# Unclear
.DS_Store
/generated
/utils/ModifyTool/Properties/launchSettings.json
122 changes: 2 additions & 120 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,130 +26,12 @@ dotnet new logofx-wpf -n <new-solution-name>
Inside solution folder run

```
dotnet new logofx-model -sn <solution name> -n <model name>
dotnet new logofx-model -sn <solution name> -n <model name> [--allow-scripts yes]
```

This command adds `<model name>` model to `<solution name>.Model` project; `I<model name>` contract to `<solution name>.Model.Contracts` project; `<model name>Dto` dto to `<solution name>.Data.Contracts.Dto` project.

Then you need to modify `Mappers\MappingProfile` and `Module` (in `… .Model` project) for setup mapping between the DTO and the model.

In `MappingProfile` add code as in example:

```csharp
CreateDomainObjectMap<…Dto, I…, S…>();
```

Where `` is entity name specified in the parameter `-n`.

In `Module` add code `.AddSingleton<…Mapper>()` to `dependencyRegistrator`, where `` is entity name specified in the parameter `-n`.

### Example

We created the solution `MyApp` (by command `dotnet new logofx-wpf -n MyApp`). Then we need to add entity `MyModel` to this solution. In solution folder run command: `dotnet new logofx-model -sn MyApp -n MyModel`. Then open file `Module.cs` in `MyApp.Model` project. Default content of this file is:

```csharp
using System.Reflection;
using AutoMapper;
using JetBrains.Annotations;
using MyApp.Model.Contracts;
using MyApp.Model.Mappers;
using Solid.Practices.IoC;
using Solid.Practices.Modularity;

namespace MyApp.Model
{
[UsedImplicitly]
internal sealed class Module : ICompositionModule<IDependencyRegistrator>
{
public void RegisterModule(IDependencyRegistrator dependencyRegistrator)
{
dependencyRegistrator
.RegisterAutomagically(
Assembly.LoadFrom(AssemblyInfo.AssemblyName),
Assembly.GetExecutingAssembly());

var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile());
});
var mapper = config.CreateMapper();
dependencyRegistrator
.AddInstance(mapper)
.AddSingleton<SampleMapper>();
}
}
}
```

You must add to `dependencyRegistrator` one more `AddSingleton` instruction.

```csharp
dependencyRegistrator
.AddInstance(mapper)
.AddSingleton<SampleMapper>()
.AddSingleton<MyModelMapper>();
```

Save `Module.cs` changes and then open `Mappers\MappingProfile.cs` in `MyApp.Model` project. Default content of this file is:

```csharp
using System;
using AutoMapper;
using MyApp.Data.Contracts.Dto;
using MyApp.Model.Contracts;

namespace MyApp.Model.Mappers
{
internal sealed class MappingProfile : Profile
{
public MappingProfile()
{
CreateCameraMaps();
}

private void CreateCameraMaps()
{
CreateDomainObjectMap<SampleItemDto, ISampleItem, SampleItem>();
}

private void CreateDomainObjectMap<TDto, TContract, TModel>()
where TModel : TContract
where TContract : class => CreateDomainObjectMap(typeof(TDto), typeof(TContract), typeof(TModel));

private void CreateDomainObjectMap(Type dtoType, Type contractType, Type modelType)
{
CreateMap(dtoType, contractType).As(modelType);
CreateMap(dtoType, modelType);
CreateMap(contractType, dtoType);
CreateMap(modelType, dtoType);
}
}
}
```

You must add another `CreateDomainObjectMap` instruction for registration `MyModel` mapping. For example:

```csharp
public MappingProfile()
{
CreateCameraMaps();
CreateMyModelMaps();
}

private void CreateCameraMaps()
{
CreateDomainObjectMap<SampleItemDto, ISampleItem, SampleItem>();
}

private void CreateMyModelMaps()
{
CreateDomainObjectMap<MyModelDto, IMyModel, MyModel>();
}
```
If you didn't specify `--allow-scripts yes` parameter the template engine ask you to allow template script launch. This script will change `Mappers\MappingProfile` and `Module` (in `… .Model` project) for setup mapping between the DTO and the model.

## Azure Devops CI Status
[![Build Status](https://dev.azure.com/LogoFX/cli-dotnet/_apis/build/status/LogoFX.cli-dotnet?branchName=master)](https://dev.azure.com/LogoFX/cli-dotnet/_build/latest?definitionId=1&branchName=master)
Expand Down
8 changes: 6 additions & 2 deletions devops/copy-template-source.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ set targetDir=generated

cd ..\%targetDir%
if exist %templateDir% (
echo Removing %templateDir%
rmdir %templateDir%
)

if %ERRORLEVEL% NEQ 0 (
if %ERRORLEVEL% NEQ 0 (
echo Error removing %templateDir%
goto EXIT
)

xcopy /e /i /y ..\%sourceDir%\%templateDir% .\%templateDir% /exclude:..\devops\excludefiles.txt+..\devops\excludeprojects.txt
xcopy /e /i /y /h ..\%sourceDir%\%templateDir% .\%templateDir% /exclude:..\devops\exclude-common.txt+..\devops\exclude-for-model.txt

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

cd ..\devops

call generate-utils.cmd %1

:EXIT
exit /b %ERRORLEVEL%
11 changes: 8 additions & 3 deletions devops/copy-template.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ if %ERRORLEVEL% NEQ 0 (
REM Copy solution folder

if exist %1 (
echo Removing %1
rmdir %1 /s /q
)
md %1
if %ERRORLEVEL% NEQ 0 (
echo Error removing %1
goto EXIT
)

md %1
if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

xcopy /e /i /y /h ..\templates\%1 .\%1 /exclude:..\devops\excludefiles.txt
xcopy /e /i /y /h ..\templates\%1 .\%1 /exclude:..\devops\exclude-common.txt

if %ERRORLEVEL% NEQ 0 (
goto EXIT
Expand All @@ -41,7 +46,7 @@ if not "%2" == "--use-common" (

REM Copy 'Common' projects

xcopy /e /i /y ..\common .\%1 /exclude:..\devops\excludefiles.txt
xcopy /e /i /y ..\common .\%1 /exclude:..\devops\exclude-common.txt

if %ERRORLEVEL% NEQ 0 (
goto EXIT
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions devops/exclude-for-model.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.sln
.csproj
AppModel.cs
IAppModel.cs
2 changes: 0 additions & 2 deletions devops/excludeprojects.txt

This file was deleted.

24 changes: 24 additions & 0 deletions devops/generate-devops-utils.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
REM Publish UninstallTemplate to \devops\utils folder

set utils=utils

if exist %utils% (
rmdir %utils% /s /q
)

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

cd ..\utils\UninstallTemplate

dotnet publish -c release -o ..\..\devops\%utils%

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

cd ..\..\devops

:EXIT
EXIT /B %ERRORLEVEL%
39 changes: 39 additions & 0 deletions devops/generate-utils.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
set temp=generated
set utils=utils

REM Prepare 'Generated' folder

cd ..
if not exist %temp% (
md %temp%
)

cd %temp%

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

if exist %utils% (
echo Removing %utils%
rmdir %utils% /s /q
)

if %ERRORLEVEL% NEQ 0 (
echo Error removing %utils%
goto EXIT
)

cd ..\utils

cd ModifyTool
dotnet publish -c release -o ..\..\%temp%\%1\%utils%

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

cd ..\..\devops

:EXIT
EXIT /B %ERRORLEVEL%
18 changes: 18 additions & 0 deletions devops/install-template-pack.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,35 @@ call install-package.cmd %package_name% %package_version%
cd ..

if exist output (
echo Removing output
rmdir output /s /q
)

if %ERRORLEVEL% NEQ 0 (
echo Error removing output
goto EXIT
)

if exist bin (
echo Removing bin
rmdir bin /s /q
)

if %ERRORLEVEL% NEQ 0 (
echo Error removing bin
goto EXIT
)

if exist obj (
echo Removing obj
rmdir obj /s /q
)

if %ERRORLEVEL% NEQ 0 (
echo Error removing obj
goto EXIT
)

cd devops

:EXIT
Expand Down
11 changes: 9 additions & 2 deletions devops/install-template.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,30 @@ if %ERRORLEVEL% NEQ 0 (
REM Copy solution folder

if EXIST %1 (
echo Removing %1
rmdir %1 /s /q
)

if %ERRORLEVEL% NEQ 0 (
echo Error removing %1
goto EXIT
)

md %1

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

xcopy /e /i /y /h ..\templates\%1 .\%1 /exclude:..\devops\excludefiles.txt
xcopy /e /i /y /h ..\templates\%1 .\%1 /exclude:..\devops\exclude-common.txt

if %ERRORLEVEL% NEQ 0 (
goto EXIT
)

REM Copy 'Common' projects

xcopy /e /i /y ..\common .\%1 /exclude:..\devops\excludefiles.txt
xcopy /e /i /y ..\common .\%1 /exclude:..\devops\exclude-common.txt

if %ERRORLEVEL% NEQ 0 (
goto EXIT
Expand Down
3 changes: 2 additions & 1 deletion devops/test-specs.cmd
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
cd ..\specs
dotnet test -c Release
dotnet test -c Release
cd ..\devops
Binary file added devops/utils/JetBrains.Annotations.dll
Binary file not shown.
Binary file added devops/utils/System.CodeDom.dll
Binary file not shown.
Binary file added devops/utils/System.Management.dll
Binary file not shown.
Loading

0 comments on commit 7a012af

Please sign in to comment.