Get Started
| Product | Versions |
|---|---|
| .NET Standard |
README
FluentStructures
This project provides a set of fluent APIs to simplify the handling of structs in .NET.
You spent too much time centering stuff with X1 + ((Width1 - Width2) / 2).
Use cases
Setting properties with With...
The extensions methods With... and WithAdditional... provide expressive ways to get a copy of a struct with absolute values or relative value additions.
// (p1)βββββββββββββββββββββββββββββββ(p2)
// β
// β
// β +50px
// β
// β
// (p4)βββββββββ(p3)
// -33px
// traditional
var p1 = new Point(10, 10);
var p2 = new Point(100, p1.Y);
var p3 = new Point(p2.X, p2.Y + 50);
var p4 = new Point(p3.X - 33, p3.Y);
// fluent
p1 = new Point(10, 10);
p2 = p1.WithX(100); // absolute
p3 = p2.WithAdditionalY(50); // relative
p4 = p3.WithAdditionalX(-33);
Emphasizing the intent over simple calculations
This project does not only add fluent extensions to set properties, it also comes with tailored extension methods to emphasize the intent when working with structs. Simple and expressive code instead of calculations that have to be thought through every time they get rediscovered.
The Rectangle (and RectangleF) gets an extension method to find its landmarks (corner stones or the center point) easily ...
// ββββββββββββββββββββββββββββββββββββ
// β β
// β β
// β (p1) β
// β β
// β β
// βββββββββββββββ(p2)βββββββββββββββββ
// traditional
var p1 = new Point(rect.Left + (rect.Width / 2), rect.Top + (rect.Height / 2));
var p2 = new Point(rect.Left + (rect.Width / 2), rect.Bottom);
// fluent
p1 = rect.GetLandmark(ContentAlignment.MiddleCenter);
p2 = rect.GetLandmark(ContentAlignment.BottomCenter);
... while Embed() takes another rectangle or size to be embedded and returns the corresponding bounds depending on the defined alignment.
// ββββββββββββββββββββββββββββββββββββ
// β β
// β ββββββ β
// β β β β <-- 10px padding
// β ββββββ β
// β β
// ββββββββββββββββββββββββββββββββββββ
var cellBounds = new Rectangle(100, 100, 300, 150);
var userImage = new Bitmap(30, 30);
// traditional
var userImageBounds = new Rectangle(
cellBounds.Right - userImage.Width - 10,
cellBounds.Y + ((cellBounds.Height - userImage.Height) / 2),
userImage.Width,
userImage.Height);
// fluent
userImageBounds = cellBounds
.Embed(userImage.Size, ContentAlignment.MiddleRight)
.WithAdditionalLeft(-10);
Scope
This project currently adds extension methods to structs of the System.Drawing namespace like Color, Point, Rectangle and so on. More structs might be added over time, pull requests always welcome.
Downloads by Version
Similar & Alternative Packages
Hand-picked matching NuGet libraries based on tags & ecosystem
Depended On By
Find packages that declare this package as a dependency. NuGet does not provide a reverse-dependency endpoint, so candidates are checked on demand.
Adoption Assessment
Assess this package against its supported frameworks, license, maintenance signals and dependency graph before making it a project standard.
Related Guides & Workflows
Learn how to evaluate, integrate, or audit this package in your .NET project.