# TCDev API Generator - Getting Started

> Here's a small getting started guide for my API Generator Project

2022-03-27 · Tim Cadenbach · https://www.tcdev.de/blog/tcdev-api-generator-getting-started/

---

<p>Hey Folks, as I received a few questions I decided to write a quick getting started guide ...and prolly should give that baby a proper name at some point!</p>
<p>The current state of the project will generate a fully working CRUD API from just a model class, eventually it will evolve in a full "Database direct to API" project similar to Hasura or other options.<br /><br />You can find the docs....in desperate need of an update... here -&gt; <a href="https://www.tcdev.de" rel="follow noopener" target="_blank">https://www.tcdev.de</a></p>
<h3>Getting Started</h3>
<p>Start either a new WebAPI or WebApp project with .NET 6</p>
<p>Download the package via nuget:</p>
<pre class="language-csharp"><code>dotnet add package TCDev.ApiGenerator</code></pre>
<p><br />Add the library to program.cs (or startup.cs if you're using the old way!)</p>
<pre class="language-csharp"><code>builder.Services.AddApiGeneratorServices()
                .AddConfig(NameOfRootNodeInAppSettings)
                or
                .AddConfig(new ApiGeneratorConfig() { ... })
                or
                .AddConfig()</code></pre>
<p>Note: Assembly.GetExecutingAssembly() can be overwritten, just use the assembly where you plan to add your models!</p>
<h3>Optional Step: Migrations</h3>
<p>When using SQLite or SQL you can have automatic migrations enabled, this is useful for development but shouldn't be used once you're done and especially not if you use an<br />existing Database!</p>
<pre class="language-csharp"><code>app.UseApiGenerator();
app.UseAutomaticAPIMigrations(true);</code></pre>
<p></p>
<h3>Build your first API</h3>
<p>Building your first API is as simple as just adding a class to your project, something along these lines:</p>
<pre class="language-csharp"><code>   [Api("/people", ApiMethodsToGenerate.All )]
   public class Person :  IObjectBase&lt;Guid&gt;
   {
      public string Name { get; set; }
      public DateTime Date { get; set; }
      public string Description { get; set; }
      public int Age { get; set; }
      public Guid Id { get; set; }
}</code></pre>
<p>The only requirement is that you have to use the IObjectBase&lt;TEntityType&gt; interface. This tells the library which type your primary key has and is used for various things. This requirement might change but the current versions require this.&nbsp;</p>
<p>To turn your class into an API just add the ApiAttribute as seen above and set the route to whatever you want it to be. (Needs to start with trailing / )<br />The second parameter controls which API Methods should be available.&nbsp;</p>
<p><br />Just start your project now and you'll be greeted by Swagger Docs showing you the docs for your API and you can start using it, it should already work :)</p>
<h3>Configure the Database</h3>
<p>Per default the project uses an InMemory Database Provider, good for development but probably you want to change that quickly. Nothing easier than that :9<br />Add this to your AppSettings:<br /><br /></p>
<pre class="language-csharp"><code>  "Api": {
    "Database": {
      "DatabaseType": "SQL"
    }
  }</code></pre>
<p>And additionally the connection string:</p>
<pre class="language-csharp"><code>  "ConnectionStrings": {
    "ApiGeneratorDatabase": "Server=localhost;database=tcdev_dev_222;user=sa;password=Password!23;"
  },</code></pre>
<p>Make sure the name is "ApiGeneratorDatabase" as this is a requirement right now.&nbsp;</p>
<p>If you start your project again it should now use your database and should have it automatically created.&nbsp;</p>
<p>If you want to have migrations automatically applied just add this to startup:<br /><br /></p>
<pre class="language-csharp"><code>app.UseApiGenerator();
app.UseAutomaticAPIMigrations(true);</code></pre>
<p>This is first of all everything you "have" to do...but theres more you can do</p>
<h3>Configure your api even further</h3>
<p>The lib comes with 2 helper classes you can use to have things cleaner. "Trackable" and "SoftDeletable".&nbsp;</p>
<ul>
<li>Trackable adds two new fields to the database "CreatedAt" and "UpdatedAt" and handles everything automatically.&nbsp;</li>
<li>SoftDeletable does probably what you think it does, items are not deleted but just "marked" as deleted</li>
</ul>
<h4>Customize behaviour</h4>
<p>You can add various interfaces to your class to inject custom functionality, currently called Hooks. For every method theres a Before and After Hook, similar to this:<br /><br /></p>
<pre class="language-csharp"><code>   [Api("/people", ApiMethodsToGenerate.All )]
   public class Person : Trackable, 
      IObjectBase&lt;Guid&gt;,
      IBeforeUpdate&lt;Person&gt;, // Before Update Hook
      IBeforeDelete&lt;Person&gt;, // BeforeDelete Hook

   {</code></pre>
<p>Implementing the interface allows you to intercept whats happening and add custom functionality, like in this example:</p>
<pre class="language-csharp"><code>      public Task&lt;Person&gt; BeforeUpdate(Person newPerson, Person oldPerson)
      {
         newPerson.Age = 333;

         return Task.FromResult(newPerson);
      }</code></pre>
<p>..more to come</p>
<h3>Last but not least, customize database layout</h3>
<p>To customize the table and how your model looks in the database you can use classic EntityFramework functionality.&nbsp;<br />Just add the IEntityTypeConfiguration interface and you can use all the EntityFramework options like here:</p>
<pre class="language-csharp"><code>   [Api("/people", ApiMethodsToGenerate.All )]
   public class Person : Trackable, 
      IObjectBase&lt;Guid&gt;,
      IEntityTypeConfiguration&lt;Person&gt; // Configure Table Options yourself
   {
      public void Configure(EntityTypeBuilder&lt;Person&gt; builder)
      {
         builder.ToTable("MyFancyTableName");
         //....all the other EF Core Options
      }</code></pre>
<p></p>
<p>way more to come....add issues and discussions to github! See you soon</p>
