Skip to content

Commit

Permalink
Generate EF Core DbContext Fluent API code to configure POCO (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhoangnl authored Oct 23, 2020
1 parent 8a8f896 commit 5756293
Show file tree
Hide file tree
Showing 39 changed files with 415 additions and 297 deletions.
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Here is a visual representation of the above:
Installation is pretty simple, just copy and paste the following command:

~~~
dotnet tool install --global dotnet-dash --version 0.3.0-alpha
dotnet tool install --global dotnet-dash --version 0.5.0-alpha
~~~

If you have successfully installed Dash, the following command will bring up the help screen:
Expand All @@ -33,7 +33,7 @@ dotnet dash --help
Updating to the latest, or different, version of Dash is equally simple:

~~~
dotnet tool update --global dotnet-dash --version 0.3.0-alpha
dotnet tool update --global dotnet-dash --version 0.5.0-alpha
~~~

## Hello World example
Expand Down
2 changes: 1 addition & 1 deletion docs/model-file/configuration-section.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
### Configuration
### Configuration Section
Inside the `Configuration` object, you define key/value pairs related to code generation.

Here is an example:
Expand Down
80 changes: 80 additions & 0 deletions docs/model-file/data-type-declaration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Data Type Declaration
As you have seen, defining the _Data Type_ of an attribute is pretty straight forward.

Simply use the following syntax:
~~~ JSON
"AttributeName": "[data type]([constraints])"
~~~

where `[constraints]` is _optional_.

An example:
~~~ JSON
"GivenName": "String"
~~~

## Supported Data Types
The following Data Types are supported:

| Dash data type | .NET | SqlServer |
|-----------------|----------|------------------|
| `string` | String | varchar |
| `unicode` | String | nvarchar |
| `int` | Int | int |
| `bool` | Bool | bit |
| `datetime` | DateTime | datetime |
| `guid` | Guid | uniqueidentifier |

For your convenience, the target data type (.NET and SqlServer) is also included.

## Constraints
Dash supports _constraints_ that lets you express the limitations of your Attribute.

### Optional/Nullable
In Dash, all attributes are _required_ or _Non-Nullable_.

Similar to C#, use `?` to specify that an attribute is _optional_ or _Nullable_.

Example:
~~~ JSON
"Nickname": "String?"
~~~

### Maximum Length
To specify that an attribute has a maximum length, use `[length]`.

Example:
~~~ JSON
"Nickname": "String[30]"
~~~

### Default Value
To specify that an attribute has a default value, use `(=='default')`.

Example:
~~~ JSON
"Nickname": "String(=='Foo')"
~~~

In the above example, we specified that the default value for `Nickname` is `Foo`.

### Regular Expression
To use a regular expression as a constraint, use `:[regex]`

Example:
~~~ JSON
"Nickname": "String:[a-zA-Z0-9]{5}"
~~~

### Mixing Constraints
All constraints can be mixed. For instance, you can specify that an attribute is Nullable and has a Maximum Length of 30:

~~~ JSON
"Nickname": "String?[30]"
~~~

You can put the constraints in any order, except for the Regular Expression constraint, which must always be placed as the final constraint.

~~~ JSON
"Nickname": "String(=='Foo')[30]?:[a-zA-Z0-9]"
~~~
11 changes: 6 additions & 5 deletions docs/model-file/introduction.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Introduction
Dash uses the JSON format to describe the domain models. Each JSON file is called a _Dash Model File_.
Dash uses the JSON format to describe the domain models. Each JSON file is called a _(Dash) Model File_.

Model Files are written according to Dash's own JSON specification which, throughout this documentation, is referred to as _Dash JSON_.

Expand Down Expand Up @@ -54,7 +54,7 @@ Entities are defined using key/value pairs directly under the `Model` object, li
In the above example, we defined the entity `Person` with 0 attributes.

## Attributes
An attribute consists of a _Name_ and _[Dash Data Type](attributes.md#dash-data-type)_, and is defined using a key/value pair placed directly under an Entity object, like this:
An attribute consists of a _Name_ and a _[Data Type Declaration](./data-type-declaration.md)_, and is defined using a key/value pair placed directly under an Entity object, like this:

~~~ JSON
{
Expand All @@ -69,12 +69,13 @@ An attribute consists of a _Name_ and _[Dash Data Type](attributes.md#dash-data-

In the above example, we have added the 2 attributes to the `Person` entity:

1. `Name` of the type `string`
1. `Age` of the type `int`
1. `Name` of the data type `string`
1. `Age` of the data type `int`

!!! note

You can add constraints to the Dash Data Type to limit the length, or enforce a [regular expression](to-do). Please visit the [Attributes documentation](attributes.md) to read more.
You can add constraints to the Data Type Declaration to limit the length, or enforce a [regular expression](to-do).
Please visit the [Data Type Declaration](./data-type-declaration.md) to read more about this subject.

!!! important

Expand Down
90 changes: 0 additions & 90 deletions docs/model-file/model-section.md

This file was deleted.

2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ nav:
- Home: index.md
- Model File:
- Introduction: model-file/introduction.md
- Configuration Section: model-file/configuration-section.md
- Entities In-Depth: model-file/entities-in-depth.md
- Data Type Declaration: model-file/data-type-declaration.md
- Seeding data: model-file/seeding-data.md
- Templates:
- Built-in Templates: template/built-in-templates.md
Expand Down
2 changes: 1 addition & 1 deletion src/Dash/src/Dash/Application/SourceCodeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static class SourceCodeServices
public static void Add(IServiceCollection services)
{
services.AddSingleton<ISourceCodeParser, DefaultSourceCodeParser>();
services.AddSingleton<IDataTypeParser, DataTypeParser>();
services.AddSingleton<IDataTypeDeclarationParser, DataTypeDeclarationParser>();
services.AddSingleton<IEntityReferenceValueParser, EntityReferenceValueParser>();
services.AddSingleton<IReservedSymbolProvider, DefaultReservedSymbolProvider>();
}
Expand Down
6 changes: 2 additions & 4 deletions src/Dash/src/Dash/Dash.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Nullable>enable</Nullable>
<Version>0.4.1-alpha</Version>
<Version>0.5.0-alpha</Version>
<PackAsTool>true</PackAsTool>
<ToolCommandName>dotnet-dash</ToolCommandName>
<PackageOutputPath>./nupkg</PackageOutputPath>
<AssemblyName>dotnet-dash</AssemblyName>
<Authors>Huy Hoang</Authors>
<CodeAnalysisRuleSet>..\.sonarlint\dotnet-dash_dashcsharp.ruleset</CodeAnalysisRuleSet>
<Description>Dash is a command-line tool for fast model-driven code generation.</Description>
<PackageReleaseNotes>A default value is added to class properties (generated using the built-in EF poco template) in order to make warning CS8618 go away.

</PackageReleaseNotes>
<PackageReleaseNotes>Generated code now uses EF Fluent API to configure the generated POCO if such contraints are defined in the Model File.</PackageReleaseNotes>
<PackageProjectUrl>https://github.com/dotnet-dash</PackageProjectUrl>
<PackageIcon>packageicon.png</PackageIcon>
<Copyright>Huy Hoang</Copyright>
Expand Down
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/BoolDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class BoolDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => true;
public bool IsString => false;
public bool IsUnicode => false;
}
}
3 changes: 1 addition & 2 deletions src/Dash/src/Dash/Engine/DataTypes/DataTypeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) Huy Hoang. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;
using Dash.Extensions;

namespace Dash.Engine.DataTypes
Expand Down Expand Up @@ -29,7 +28,7 @@ public static IDataType Create(string dashDataType)
}
}

throw new InvalidOperationException($"Unknown Dash data type '{dashDataType}'");
return new UnknownDataType(dashDataType);
}
}
}
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/DateTimeDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class DateTimeDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => true;
public bool IsBoolean => false;
public bool IsUnicode => false;
public bool IsString => false;
}
}
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/EmailDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class EmailDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
public bool IsUnicode => true;
public bool IsString => true;
}
}
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/GuidDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class GuidDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
public bool IsUnicode => false;
public bool IsString => false;
}
}
4 changes: 2 additions & 2 deletions src/Dash/src/Dash/Engine/DataTypes/IntDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class IntDataType : IDataType
public bool IsNumeric => true;
public bool IsDateTime => false;
public bool IsBoolean => false;

public static IntDataType Default => new IntDataType();
public bool IsUnicode => false;
public bool IsString => false;
}
}
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/StringDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class StringDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
public bool IsUnicode => false;
public bool IsString => true;
}
}
2 changes: 2 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/UnicodeDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public class UnicodeDataType : IDataType
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
public bool IsUnicode => true;
public bool IsString => true;
}
}
17 changes: 17 additions & 0 deletions src/Dash/src/Dash/Engine/DataTypes/UnknownDataType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Dash.Engine.DataTypes
{
public class UnknownDataType : IDataType
{
public UnknownDataType(string unknownDataType)
{
Name = unknownDataType;
}

public string Name { get; }
public bool IsNumeric => false;
public bool IsDateTime => false;
public bool IsBoolean => false;
public bool IsUnicode => false;
public bool IsString => false;
}
}
4 changes: 4 additions & 0 deletions src/Dash/src/Dash/Engine/IDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public interface IDataType
public bool IsDateTime { get; }

bool IsBoolean { get; }

bool IsUnicode { get; }

bool IsString { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace Dash.Engine
{
public interface IDataTypeParser
public interface IDataTypeDeclarationParser
{
DataTypeParserResult Parse(string dataTypeSpecification);
DataTypeDeclarationParserResult Parse(string dataTypeDeclaration);
}
}
Loading

0 comments on commit 5756293

Please sign in to comment.