一键导入
solid-principles
Use during implementation when designing modules, functions, and components requiring SOLID principles for maintainable, flexible architecture.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Use during implementation when designing modules, functions, and components requiring SOLID principles for maintainable, flexible architecture.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Use when building .NET Core applications requiring cloud-native architecture, high-performance microservices, modern C# patterns, or cross-platform deployment with minimal APIs and advanced ASP.NET Core features.
This skill embodies the principles of "Clean Code" by Robert C. Martin (Uncle Bob). Use it to transform "code that works" into "code that is clean."
Provides guidance for generating comprehensive C# unit tests in .NET projects. Use when writing, scaffolding, or improving unit tests for C# code using MSTest, NUnit, or xUnit frameworks. Covers framework detection, test structure, edge-case analysis, mocking rules, and best practices. DO NOT use for integration tests, end-to-end tests, or non-.NET languages.
Get best practices for XUnit unit testing, including data-driven tests
Use when writing new functions, adding features, fixing bugs, or refactoring by applying TDD principles - write failing tests before implementation code, make them pass, then refactor.
Use when modifying existing files, refactoring, improving code quality, or touching legacy code by applying the Boy Scout Rule to leave code better than you found it.
| name | solid-principles |
| user-invocable | false |
| description | Use during implementation when designing modules, functions, and components requiring SOLID principles for maintainable, flexible architecture. |
| allowed-tools | ["Read","Edit","Grep","Glob"] |
Apply SOLID design principles for maintainable, flexible code architecture.
# BAD - Multiple responsibilities
defmodule UserManager do
def create_user(attrs) do
# Creates user
# Sends welcome email
# Logs to analytics
# Updates cache
end
end
# GOOD - Single responsibility
defmodule User do
def create(attrs), do: Repo.insert(changeset(attrs))
end
defmodule UserNotifier do
def send_welcome_email(user), do: # email logic
end
defmodule UserAnalytics do
def track_signup(user), do: # analytics logic
end
// BAD - Multiple responsibilities
class UserComponent {
render() { /* UI */ }
fetchData() { /* API */ }
formatDate() { /* Formatting */ }
validateInput() { /* Validation */ }
}
// GOOD - Single responsibility
function UserProfile({ user }: Props) {
return <View>{/* UI only */}</View>;
}
function useUserData(id: string) {
// Data fetching only
}
function formatUserDate(date: Date): string {
// Formatting only
}
Ask yourself: "What is the ONE thing this module does?"
Software entities should be open for extension, closed for modification.
public interface IPaymentProvider {
Transaction ProcessPayment(decimal amount, string token);
}
public class StripeProvider : IPaymentProvider {
public Transaction ProcessPayment(decimal amount, string token) {
// Stripe logic
return new Transaction();
}
}
public class PayPalProvider : IPaymentProvider {
public Transaction ProcessPayment(decimal amount, string token) {
// PayPal logic
return new Transaction();
}
}
// Usage
public Transaction Charge(IPaymentProvider provider, decimal amount, string token) {
return provider.ProcessPayment(amount, token);
}
// BAD - Requires modification for new types
public string RenderItem(Item item) {
if (item.Type == "gig") {
return "TaskCard";
} else if (item.Type == "shift") {
return "WorkPeriodCard";
}
// Have to modify this function for new types
return "DefaultCard";
}
// GOOD - Extension through delegates
public delegate string CardRenderer(Item item);
public static Dictionary<string, CardRenderer> renderers = new()
{
{ "gig", item => "TaskCard" },
{ "shift", item => "WorkPeriodCard" } /* Add new types without modifying RenderItem */
};
public string RenderItemFlexible(Item item) {
if (renderers.TryGetValue(item.Type, out var renderer)) {
return renderer(item);
}
return "DefaultCard";
}
Ask yourself: "Can I add new functionality without changing existing code?"
// BAD - Violates LSP
public class PaymentCalculator {
public double CalculateTotal(List<double> items) {
if (items.Count == 0) {
throw new Exception("Empty list not allowed");
}
return items.Sum();
}
}
// GOOD - Honors contract
public class PaymentCalculator {
public double CalculateTotal(List<double> items) {
return items.Sum(); // Returns 0 for empty list
}
}
// BAD - Violates LSP
public class Bird {
public virtual void Fly() { /* flies */ }
}
public class Penguin : Bird {
public override void Fly() {
throw new Exception("Penguins cannot fly"); // Breaks contract
}
}
// GOOD - Correct abstraction
public interface IBird {
void Move();
}
public class FlyingBird : IBird {
public void Move() {
Fly();
}
private void Fly() { /* flies */ }
}
public class SwimmingBird : IBird {
public void Move() {
Swim();
}
private void Swim() { /* swims */ }
}
Ask yourself: "Can I replace this with its parent/interface without breaking behavior?"
Clients should not be forced to depend on interfaces they don't use.
// BAD - Fat interface
public interface IUser {
void Work();
void TakeBreak();
void EatLunch();
void ClockIn();
void ClockOut();
// Not all users need all these
}
// GOOD - Segregated interfaces
public interface IWorkable { void Work(); }
public interface IBreakable { void TakeBreak(); }
public interface ITimeTrackable {
void ClockIn();
void ClockOut();
}
// Implement only what you need
public class ContractUser : IWorkable {
public void Work() { /*...*/ }
// No time tracking needed
}
// BAD - Fat interface
public interface IUser {
void Work();
void TakeBreak();
void ClockIn();
void ClockOut();
void ReceiveBenefits();
// Not all users need all methods
}
// GOOD - Segregated interfaces
public interface IWorkable { void Work(); }
public interface ITimeTrackable {
void ClockIn();
void ClockOut();
}
public interface IBenefitsEligible {
void ReceiveBenefits();
}
// Compose only what you need
public class FullTimeUser : IWorkable, ITimeTrackable, IBenefitsEligible {
public void Work() { }
public void ClockIn() { }
public void ClockOut() { }
public void ReceiveBenefits() { }
}
public class ContractUser : IWorkable, ITimeTrackable {
public void Work() { }
public void ClockIn() { }
public void ClockOut() { }
}
public class TaskUser : IWorkable {
public void Work() { }
}
Ask yourself: "Does this interface force implementations to define unused methods?"
// BAD - Direct dependency
public class UserService {
public User CreateUser(User user) {
var repo = new PostgresRepo(); // Tightly coupled
repo.Insert(user);
return user;
}
}
// GOOD - Depend on abstraction
public interface IRepository {
void Insert(User user);
}
public class UserService {
private readonly IRepository _repo;
public UserService(IRepository repo) {
_repo = repo;
}
public User CreateUser(User user) {
_repo.Insert(user);
return user;
}
}
// BAD - Direct dependency
public class UserManager {
private StripeAPI api = new StripeAPI(); // Tightly coupled
public Transaction ProcessPayment(decimal amount) {
return api.Charge(amount);
}
}
// GOOD - Depend on abstraction
public interface IPaymentAPI {
Transaction Charge(decimal amount);
}
public class UserManager {
private readonly IPaymentAPI _paymentAPI;
public UserManager(IPaymentAPI paymentAPI) {
_paymentAPI = paymentAPI;
}
public Transaction ProcessPayment(decimal amount) {
return _paymentAPI.Charge(amount);
}
}
// Usage
IPaymentAPI stripeAPI = new StripeAPI();
var manager = new UserManager(stripeAPI);
Ask yourself: "Can I swap implementations without changing dependent code?"
boy-scout-rule: Apply SOLID when improving codetest-driven-development: Write tests for each responsibilityelixir-code-quality-enforcer: Credo enforces some SOLID principlestypescript-code-quality-enforcer: TypeScript interfaces support ISP/DIPSOLID is about managing dependencies and responsibilities, not about creating more code.
Good design emerges from applying these principles pragmatically, not dogmatically.