-
Notifications
You must be signed in to change notification settings - Fork 3
Getting Started [Create a New Business Entity]
Get Codekernel:
You can obtain the Codekernel platform code by either cloning the git repository
git clone https://github.com/ectechno/codekernel.git
or by downloding as a Zip file from https://github.com/ectechno/codekernel/zipball/master
Run the Samples:
Open the solution file codekernel/PlatformCode/Codekernel.sln with VisualStudio and set 'Codekernel.Web' as the startup projet. Build the project and let Nuget packages to be restored.
Now you should be able to run the solution to fireup the Durandal based SPA frontend.
Navigate and Play Around: You may navigate to 'Suppliers' menu item to recieve a list of suppliers from the server. Also 'Products' menu item takes you to a much sophisticated user interface allowing you to perform CRUD operations on products.
Create the model entity:
Assume our application needs to handle Payment as a business Entity. We will first create a Payment.cs Model class under Codekernel.Model\Entities to represent the Patment entity.
public class Payment : IIdentifier
{
// Primary key by EF conventions
public int Id { get; set; }
// It is useful to have a Guid for each entity to be uniquely identified
public Guid GUID { get; set; }
// Used by the EF for identifying concurrency conflicts
public byte[] RowVersion { get; set; }
// Redundent FK for Product association. This helps in asociating to products via the API
public int SupplierId { get; set; }
// FK for the Product association
public Supplier Supplier { get; set; }
// Attributes of the entity
public double Amount { get; set; }
public DateTime CreatedAt { get; set; }
}Most of the functionality in Codekernel assumes your model entities to implement Codekernel.Data.Core.IIdentifier interface. Implementing this interface mandates a common primary key (int in this case) for your entities as well as a Guid field to identify each of the entity uniquly. This Guid comes handy in wide range of cases varying from 'UI operations' to 'Enterprise data center merge'. The RowVersion column is used by Codekernel to detect concurrency conflicts. We will discuss concurrency in detail later.
Setup Data Repositories:
Now we need our Entity Framework based data repository layer to know about the new entity we just created. Let's open our DatabaseContext class (Codekernel.Data\DatabaseContext.cs) and add following line on to it:
public DbSet<Payment> Payments { get; set; }
Thats all you have to do to create a powerful repository based data layer for Payment entity. Generic Repository classes of Codekernel will take care of the rest of the plumbing. At this moment you are able to run the DataLayer integration tests for Payment entity which we will discuss later.
Create a OData Web Controller:
Now its time to work on the Web API layer. To expose the Payment entity as a OData service we need to create a new Controller class extending SimpleCrudController<T>. Create PaymentsController.cs within Codekernel.API\Controllers with following content:
public class PaymentsController: SimpleCrudController<Payment>
{
}You need to let Web API know about the new model class it needs to serve over HTTP. To do this open Codekernel.API\RestApiModelBuilder.cs and add following line to it:
EntitySet<Payment>("Payments");
Test on Browser:
Thats it, now your Payment entity is available as a fully fledged OData endpoint. If you run the solution again to visit http://localhost:1471/rest/Payments, you should see an empty OData set returned as follows:
Let's add some sample data via Seed data management in EF. Open 'SeedSampleData.cs' under 'Codekernel.Data/Seed' and add following lines to insert sample Payment data.
context.Payments.Add(new Payment { GUID = Guid.NewGuid(), CreatedAt = DateTime.Now, Amount = 500, Supplier = context.Suppliers.FirstOrDefault(p => p.Name == "Rubber Duck") });
context.Payments.Add(new Payment { GUID = Guid.NewGuid(), CreatedAt = DateTime.Now, Amount = 339.5, Supplier = context.Suppliers.FirstOrDefault(p => p.Name == "White Cap") });
context.SaveChanges();EF configuration on Codekernel, recreates the database if not existing. At this moment we may simply delete the database file and rerun the application to make the seed data change effective. Go to 'App_Data' folder in 'Codekernel.API', delete the .mdf file and rerun the application to see sample data available on the Payments REST endpoint.
In a next article we will have a look at how to perform CRUD operations over this new Orders REST endpoint.


