agentleFS
MBrekhofMBrekhof/xafskills/skills/xaf-search-panels/SKILL.md

xaf-search-panels

Skill4 starsChanged 6 months agoMITView on GitHub

Build configurable advanced-search panels in DevExpress XAF where admins define searchable fields through the UI and the project regenerates C# search DTOs at build time. Use when adding per-entity search popups, criteria-builder panels, or "require search before load" guards on heavy list views. Covers the critical gotcha that runtime Roslyn compilation of search DTOs is incompatible with AddSecuredEFCore, the [DomainComponent] non-persistent DTO pattern, SearchControllerBase<TEntity,TDTO>, CriteriaBuilder, wildcard support, and the PopupWindowShowAction + NonPersistentObjectSpace flow. Triggers: XAF advanced search, search panel, DomainComponent search DTO, NonPersistentObjectSpace popup, PopupWindowShowAction search, CriteriaBuilder XAF, AddSecuredEFCore dynamic types.

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Search DTOs are non-persistent objects marked [DomainComponent].

    line 26
  2. Very specific:

    Use the Criteria dictionary, not a single string.

    line 157
  3. Distinctive:

    All non-null DTO values are AND-ed.

    line 172
  4. Very specific:

    Do not: use Microsoft.CodeAnalysis.CSharp for runtime compilation here — only for string-templating the generated source.

    line 197

≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

XAF Configurable Search Panels

The Critical Gotcha: Runtime Roslyn vs AddSecuredEFCore

Do not compile search DTOs at runtime with Roslyn. AddSecuredEFCore creates change-tracking proxies for every registered entity type at startup, and types compiled later cannot participate in that proxy infrastructure. The secured data layer never sees runtime-compiled types and CreateObject<TSearchDTO>() silently returns an unproxied instance that doesn't round-trip through the UI.

Correct approach: admins configure search panels through the XAF UI (rows in a SearchConfiguration / SearchField entity). A "Save to Project" action generates plain .cs files at design time. The solution is rebuilt and redeployed. Generated DTOs compile normally and participate in EF Core change tracking.

This trades runtime configurability for security-system compatibility. The trade-off is forced — there is no working runtime path.

DTO Pattern (Generated)

Search DTOs are non-persistent objects marked [DomainComponent]. They live alongside generated search controllers.

[DomainComponent]
public class BatchZoeker
{
    // Blazor non-persistent editors need a stable key so values round-trip
    // correctly from the popup back into the DTO instance.
    [Key]
    [Browsable(false)]
    [VisibleInDetailView(false)]
    [VisibleInListView(false)]
    public Guid Id { get; set; } = Guid.NewGuid();

    [VisibleInDetailView(true), VisibleInListView(false)]
    [XafDisplayName("Name")]
    [ImmediatePostData]
    [ToolTip("Supports wildcards: * (any chars), ? (single char)")]
    public virtual string Name { get; set; }

    [VisibleInDetailView(true), VisibleInListView(false)]
    [XafDisplayName("BatchStatus")]
    [ImmediatePostData]
    [UseExactMatch]
    public virtual string BatchStatus { get; set; }

    [VisibleInDetailView(true), VisibleInListView(false)]
    [XafDisplayName("DateCreated (From)")]
    [ImmediatePostData]
    public virtual DateTime? DateCreatedFrom { get; set; }

    [VisibleInDetailView(true), VisibleInListView(false)]
    [XafDisplayName("DateCreated (To)")]
    [ImmediatePostData]
    public virtual DateTime? DateCreatedTo { get; set; }
}

Rules that quietly break otherwise:

  • [Key] Guid Id — Blazor non-persistent editors lose values on round-trip without a stable key. Mark it Browsable(false) so admins don't see it.
  • virtual properties — required even for non-persistent DTOs because XAF wires up change notifications via proxies the same way it does for entities.
  • Nullable types for ranges — DateTime?, decimal? so "not set" is distinguishable from "zero / epoch".
  • [ImmediatePostData] — pushes the value back to the model on each keystroke; without it the user must tab out before the search action sees the value.
  • [UseExactMatch] — custom attribute the CriteriaBuilder uses to skip wildcard expansion for enum-like columns where LIKE is wrong.

SearchControllerBase

One generic base controller does all the work; each entity gets a tiny derived class.

public abstract class SearchControllerBase<TEntity, TSearchDTO>
    : ObjectViewController<ListView, TEntity>
    where TEntity : class
    where TSearchDTO : class
{
    private const string CriteriaKey = "RuntimeAdvancedSearch";
    private const string RequireSearchKey = "RequireSearchBeforeLoad";
    protected virtual int MaxActiveFilters { get; set; } = 20;
    protected virtual bool RequireSearchBeforeLoad => true;

    private PopupWindowShowAction searchAction;

    public SearchControllerBase()
    {
        searchAction = new PopupWindowShowAction(this,
            $"Search_{typeof(TEntity).Name}", PredefinedCategory.View)
        {
            Caption = "Advanced Search",
            ImageName = "Action_Search",
            SelectionDependencyType = SelectionDependencyType.Independent
        };
        searchAction.CustomizePopupWindowParams += CustomizePopup;
        searchAction.Execute += Execute;
    }

    protected override void OnActivated()
    {
        base.OnActivated();
        if (RequireSearchBeforeLoad)
            View.CollectionSource.Criteria[RequireSearchKey] = CriteriaOperator.Parse("1=0");
    }

    private void CustomizePopup(object s, CustomizePopupWindowParamsEventArgs e)
    {
        var os = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(TSearchDTO));
        os.PopulateAdditionalObjectSpaces(Application);  // lets lookups see secured spaces
        var dto = os.CreateObject<TSearchDTO>();
        var detail = Application.CreateDetailView(os, dto);
        detail.ViewEditMode = ViewEditMode.Edit;
        e.DialogController.SaveOnAccept = false;  // non-persistent — nothing to save
        e.View = detail;
    }

    private void Execute(object s, PopupWindowShowActionExecuteEventArgs e)
    {
        if (e.PopupWindowView is DetailView dv) dv.ObjectSpace.CommitChanges();
        var dto = e.PopupWindowViewCurrentObject as TSearchDTO;
        var criteria = CriteriaBuilder.BuildCriteria(dto, MaxActiveFilters);

        View.CollectionSource.Criteria.Remove(RequireSearchKey);
        if (criteria is not null)
            View.CollectionSource.Criteria[CriteriaKey] = criteria;
        else
            View.CollectionSource.Criteria.Remove(CriteriaKey);

        View.CollectionSource.Reload();
        View.Refresh();
    }
}

Per-entity controller:

public class BatchSearchController : SearchControllerBase<Batch, BatchZoeker>
{
    protected override bool RequireSearchBeforeLoad => true;
}

Key Patterns

Require Search Before Load

For heavy list views (millions of rows), setting Criteria["RequireSearchBeforeLoad"] = "1=0" in OnActivated makes the grid render empty until the user runs a search. The action removes the key on execute.

This is more reliable than overriding CollectionSource.AllowLoadCollection — that approach fights XAF's lifecycle.

Multiple Named Criteria

Use the Criteria dictionary, not a single string. Different controllers can stack filters without overwriting each other:

View.CollectionSource.Criteria["RuntimeAdvancedSearch"] = userCriteria;
View.CollectionSource.Criteria["RequireSearchBeforeLoad"] = CriteriaOperator.Parse("1=0");
// XAF ANDs all entries automatically

Wildcard + Exact Match in CriteriaBuilder

CriteriaBuilder (your helper) inspects DTO properties:

  • String without [UseExactMatch] → translate * to %, ? to _, wrap in LIKE.
  • String with [UseExactMatch] → = comparison (status codes, enums).
  • DateTime? From + DateTime? To pair → range; either side null = open-ended.
  • All non-null DTO values are AND-ed.

PopulateAdditionalObjectSpaces

os.PopulateAdditionalObjectSpaces(Application) is required so that lookup editors in the popup (e.g. an "Owner" dropdown that queries ApplicationUser) see the application's secured object spaces. Without this, lookups in the popup return empty.

SaveOnAccept = false

The popup view is non-persistent. Setting SaveOnAccept = false prevents XAF from calling CommitChanges on a NonPersistentObjectSpace, which is a no-op but logs noise. The explicit dv.ObjectSpace.CommitChanges() in the execute handler is what flushes pending edits from the editor into the DTO instance.

Generation Workflow (Out of Scope)

The code generator that turns SearchConfiguration rows into BatchZoeker.cs + BatchSearchController.cs is project-specific. Typical layout:

Search/
├── SearchObjects/      # generated DTOs (e.g. BatchZoeker.cs)
├── SearchControllers/  # generated controllers (e.g. BatchSearchController.cs)
├── Controllers/        # SearchControllerBase, SaveToProjectController
├── Services/           # SearchDtoCompiler (generates source strings)
└── Attributes/         # UseExactMatch, CollectionPath, EntityPath

Generated files are committed to source control. The "Save to Project" action writes them to disk via File.WriteAllText. Next build picks them up.

Do not use Microsoft.CodeAnalysis.CSharp for runtime compilation here — only for string-templating the generated source. Runtime CSharpCompilation.Emit + AssemblyLoadContext is exactly the failure mode that breaks AddSecuredEFCore.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.