Chuyển đến nội dung
Skillpedia
Kiểm thử5.8k · #14632

migrate-static-to-wrapper

Skill này làm gì

Skill này chuyển code C# từ lời gọi tĩnh trực tiếp, chẳng hạn đọc thời gian hiện tại hoặc một tệp, sang wrapper được inject hoặc abstraction có sẵn. Dành cho lập trình viên muốn làm code dễ kiểm thử hơn theo các bước nhỏ, có kiểm soát.

Khi nào nên dùng

  • ✓Khi bạn muốn thay DateTime.UtcNow hoặc DateTime.Now bằng TimeProvider trong một dự án.
  • ✓Khi các lời gọi File trực tiếp cần chuyển sang một wrapper hệ thống tệp đã có trong một namespace.
  • ✓Khi một lớp cần constructor injection cho abstraction mới và các test của nó cần bản giả.

Liên kết skill

https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/migrate-static-to-wrapper/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/migrate-static-to-wrapper/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Hãy thay các lời gọi DateTime.UtcNow trực tiếp trong dự án Orders của tôi bằng TimeProvider, và cập nhật test để dùng đồng hồ giả.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent giới hạn công việc trong phạm vi bạn chỉ định, đổi các nơi gọi và constructor để dùng abstraction được inject, rồi cập nhật các test liên quan bằng bản giả.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Migrate Static to Wrapper

Perform mechanical, codemod-style replacement of static dependency call sites with calls to injected wrapper interfaces or built-in abstractions. Operates on a bounded scope (single file, project, or namespace) so migrations can be done incrementally.

When to Use

  • After wrappers have been generated (via generate-testability-wrappers) or built-in abstractions identified
  • Migrating DateTime.UtcNow → TimeProvider.GetUtcNow() across a project
  • Migrating File.* → IFileSystem.File.* across a namespace
  • Adding constructor injection for the new abstraction to affected classes
  • Making a static utility class testable by adding an ambient seam (Step 3) while its existing call sites keep compiling unchanged
  • Incremental migration: one project or namespace at a time
  • Updating affected tests with fakes when the requested migration names the replacement abstraction

When Not to Use

  • No wrapper or abstraction exists yet and one must be designed from scratch (use generate-testability-wrappers first). A built-in abstraction such as TimeProvider or IFileSystem always counts as existing.
  • The user wants to detect statics, not migrate them (use detect-static-dependencies)
  • Migrating between test frameworks (use the appropriate migration skill)
  • The user primarily asks for a deterministic behavior test and has not selected the production seam (use testability-obstacle)

A class that is static, or a project with no DI container, is not a reason to skip this skill — that is exactly what the ambient seam in Step 3 is for. Use it whenever the call sites must keep compiling unchanged.

Inputs

Input Required Description
Static pattern No Infer from the request and discovered call sites (e.g., DateTime.UtcNow, File.ReadAllText)
Replacement abstraction No Infer from the request and existing project abstractions; stop only when no named/existing abstraction is available
Scope No Infer from the requested file/project/namespace, otherwise discover the narrowest relevant workspace scope
Injection strategy No constructor (default), primary-constructor, or ambient

Workflow

Non-negotiable migration boundaries

  • Missing abstraction means stop. If the named interface/package is absent and the request only authorizes call-site replacement, do not add a package, invent a local lookalike interface, or edit production code. Report the exact missing prerequisite and the authorization needed to continue.
  • One source read stays one replacement read. Do not hoist or coalesce calls, even when sharing a captured timestamp looks cleaner.
  • The requested scope is exhaustive and exclusive. Replace every named call in scope and no adjacent member or file.
  • Repository-backed requests require repository work. Start by discovering files from the current workspace. Do not claim the repository is unavailable or ask the user for a path or file contents until workspace-relative discovery found no target. Do not say work was implemented unless the diff proves it.
  • Discovered workspace files must be completed in this turn when permitted. Use a host-native shell reader (sed/cat or Get-Content) only after a confirmed reader availability, transport, or path-normalization failure and only after verifying the canonical path remains inside the current workspace. Stop on content-exclusion, permission/policy, workspace-boundary, or unknown read failures. Use a shell edit fallback only for a confirmed editor availability, transport, or path-normalization failure, never for a stale context, concurrent change, permission/policy denial, or path-boundary error. Before fallback, resolve the canonical path inside the current workspace, freshly read the file, and require an anchored replacement with the expected old text and exact match count; abort if either changed. Then re-open the file, inspect the diff, and validate. Do not ask the user to paste a readable discovered file or report a proposed patch as completed work.

Step 1: Verify prerequisites

Before modifying any code:

  1. Confirm the wrapper/abstraction exists: Check that the interface or built-in abstraction is available in the project. For TimeProvider, verify the target framework is .NET 8+ or Microsoft.Bcl.TimeProvider is referenced. For System.IO.Abstractions, v
Câu hỏi thường gặp

Nó có tạo luôn wrapper không?

Không. Nếu chưa có wrapper, skill tạo wrapper cho kiểm thử nên được chạy trước.

Tôi có thể làm cả solution một lần không?

Skill làm việc trong phạm vi có giới hạn, chẳng hạn một tệp, dự án hoặc namespace, để các thay đổi có thể thực hiện từng bước.

Nó có chỉ tìm các lời gọi tĩnh không?

Không. Việc tìm chúng là một skill phát hiện riêng, còn skill này thực hiện việc thay thế.

Chi tiết

Mô tả gốc của tác giả

ALWAYS USE when asked to migrate, replace, or make testable existing C# static calls with a named wrapper or built-in abstraction: DateTime.UtcNow/Now or DateTimeOffset.UtcNow to TimeProvider/IClock, File.* to IFileSystem or an existing store such as ITextFileStore, and Environment.* to an existing reader such as IEnvironmentReader. Covers scoped files/projects, constructor injection, replacing temp-file or process-environment tests with fakes, "already registered" abstractions, and static classes whose callers/signatures must stay unchanged. Preserves DateTimeKind and call count. DO NOT USE f

repo: dotnet/skills

Độ phổ biến

Sao GitHub
5.8k
Vị trí xếp hạng
#14632
Giấy phép
MIT
Cập nhật
2/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự