Building A Full Stack Web Application – Part 4: The Framework

This post is a continuation in a series about building a full stack web application.  Previous posts in the series:

Let’s do an easy one.  The framework is a class library project that contains interfaces and POCOs that represent the business operations and data.  Each interface is a contract to which each implementation must adhere.  The POCOs (or DTOs, or property bags) are simple classes, whose job it is to transfer data to and from the business operations.

Interfaces

For Stocks Tracker, I want to define some operations that can be performed by callers.  For example, I want a caller to be able to retrieve a stock by either integer key ID, or by a unique ticker value:

public interface IStocks
{
  /// <summary>
  /// Asynchronously queries a stock for the given ID.
  /// </summary>
  /// <param name="stockId">The stock ID.</param>
  /// <returns>A <see cref="StockRecord"/> object, or null if not found.</returns>
  Task<StockRecord> GetStockAsync(int stockId);

  /// <summary>
  /// Asynchronously queries a stock for the given ticker symbol.
  /// </summary>
  /// <param name="tickerSymbol">The stock ticker symbol.</param>
  /// <returns>A <see cref="StockRecord"/> object, or null if not found.</returns>
  Task<StockRecord> GetStockAsync(string tickerSymbol);
}

Any implementations of this interface will have to return an asynchronous operation that resolves to a stock matching the passed in parameter.

POCOs

Data is a different story.  I don’t want to return a “live” object, that is wired to Entity Framework for some caller to monkey around with.  Instead, the return object will be a class of primitive types (integers and strings, and perhaps other POCO classes), representing data in a format that makes sense to the operation being performed.  Another benefit of these simple classes is that they are easily serializable by whatever protocol is being used to call the business operation:

public class StockRecord : IEquatable<StockRecord>
{
  /// <summary>
  /// Gets and sets the stock record id value.
  /// </summary>
  public int StockRecordId { get; set; }

  /// <summary>
  /// Gets and sets the ticker symbol value.
  /// </summary>
  public string TickerSymbol { get; set; }

  /// <summary>
  /// Gets and sets the stock name value.
  /// </summary>
  public string Name { get; set; }
}

That’s pretty much it. It’s good practice to keep your business layer interfaces and data classes in a reusable project, that can be reused for multiple implementations.
Next, we’ll see the framework in action at the API layer.

Leave a comment