This is experimental architecture framework for .NET applications. It is built on aspect-based principles involving some CQRS and functional programming approaches. Tecture intensively utilizes C# features, strong typing, lambda expressions, extension methods and generics. Usage experience is very similar to LINQ. The closest project that does somewhat similar is MediatR, but Tecture is wider and covers more problems.
Reinforced.Tecture is available on NuGet along with its dependent packages.
PM> Install-Package Reinforced.Tecture
PM> Install-Package Reinforced.Tecture.Aspects.Orm
PM> Install-Package Reinforced.Tecture.Aspects.DirectSql
PM> Install-Package Reinforced.Tecture.Runtimes.EfCore
PM> Install-Package Reinforced.Tecture.Testing
Get in touch with documentation
Tecture overcomes traditional approaches of .NET application design with following features:
- Explicit and type-safe abstraction of external systems (databases, queues etc);
- Type-based services design that does not require IoC configuration;
- Reads and queries to external systems implemented by extension methods. Repository pattern is not needed;
- Deferred write operations with flexible compile-time restrictions;
- Automated test data capture;
- Cheap and quick infrastructure-free data-driven regression testing;
- Therefore, Mocks/Stubs/Fakes, interfaces and virtual methods are not needed in business logic anymore. Write less, do more;
- Self-explanatory business logic with informational annotations by design;
Below are several pieces of code that uses Tecture:
Define channels and use aspects:
/// <summary>
/// Hi, I'm database communication channel
/// </summary>
public interface Db :
CommandQueryChannel<
Reinforced.Tecture.Aspects.Orm.Command,
Reinforced.Tecture.Aspects.Orm.Query
>
{ }
Create services for business logic and produce commands
/// <summary>
/// I'm orders service. And these are my type parameters (tooling).
/// By using them I say that I can update orders and add order lines
/// </summary> | |
public class Orders : TectureService< Updates<Order>, Adds<OrderLine> >
{
private Orders() { }
/// <summary>
/// And I'm business logic method
/// </summary>
/// <param name="orderId">I consume order id</param>
/// <param name="poductId">and product id</param>
/// <param name="quantity">and also product quantity</param>
public void CreateLine(int orderId, int poductId, int quantity)
{
// I perform queries to the database
var order = From<Db>.Get<Order>().ById(orderId);
// My aspect allows me to add order lines
To<Db>().Add(new OrderLine
{
OrderId = orderId,
ProductId = productId,
Quantity = quantity
}
);
// And only update orders
To<Db>.Update(order)
.Set(x=>x.TotalQuantity, order.TotalQuantity + quantity);
// Also I can invoke other services
Do<Products>().AttachToOrder(order);
}
}
Define queries for your channels
///<summary>
/// I'm entity interface...
///</summary>
public interface IEntity { int Id { get; } }
public static class Extensions
{
///<summary>
/// ...and you don't need repositories anymore to get me by Id
///</summary>
public static T ById<T>(this IQueryFor<T> q, int id) where T : IEntity
{
return q.All.FirstOrDefault(x => x.Id == id);
}
///<summary>
/// Even if you have SQL
///</summary>
public static IEnumerable<Order> GetRecentOrders(this Read<Db> db)
{
return db.SqlQuery<Order>(o =>
$"SELECT * FROM {o} WHERE DATEDIFF(day, {o.UpdatedAt}, GETDATE()) < 30"
).As<Order>();
}
}
// ...
var o = From<Db>().GetRecentOrders();
// ...
Tecture can be easily registered in any IoC container and used from application
public class OrdersController : ApiController
{
// You can inject
private readonly ITecture _tecture;
public OrdersController(ITecture tecture)
{
_tecture = tecture;
}
public ActionResult PerformActionWithOrder(int id)
{
// and use it
_tecture.Do<Orders>().PerformAction(id);
_tecture.Save();
return Ok();
}
}
Trace your business logic and get clear explanation what exactly it does with external systems:
tecture.BeginTrace();
var a = tecture.Do<Orders>().CreateOne("new order");
ctx.Save();
var trace = tecture.EndTrace();
Output.Write(trace.Explain());
/**
* 1. [ ->] Check existing order presence: 'False' obtained
* 2. [ADD] Adding new order to the database
* 3. [<- ] <SAVE>
* 4. [SQL] Re-calculating denormalized items count
* 5. [<- ] <SAVE>
* 6. [ ! ] <END>
*/
Extract test data from the trace and dump it into C# code. Convert tract into validation code. Put them together to get data-driven infrastructure-free unit test
[Fact]
public void Order_Creation_Works_As_Expected()
{
using var c = Case<Order_Creation_Works_As_Expected_TestData>(out ITecture ctx);
var a = ctx.Do<Orders>().CreateOne("test order");
ctx.Save();
c.Validate<Order_Creation_Works_As_Expected_Validation>();
}