© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
3 replies
palapapa

❔ Is it a good idea to use a "processed model" in addition to the model bound by ASP.NET itself?

First of all, I wanted to do this because I wanted to avoid writing a custom model binder.
Let's say I want to get a string from the query string in a request, and convert that string into
DotNet.Glob
DotNet.Glob
(which requires calling
Glob.Parse(string)
Glob.Parse(string)
). If I don't want to convert it manually in the action method, the other option is to write a custom model binder, but I don't want to do that because it seems like a hassel and the documentation isn't good about how to properly implement one. So I came up with the following constuct:
interface IProcessedModel<Model>
{
    public static abstract ProcessFrom(Model model);
}

class MyModel
{
    public string Path { get; set; }
}

class MyProcessedModel : IProcessedModel<MyModel>
{
    public Glob Glob { get; private set; }
    public static ProcessFrom(MyModel myModel)
    {
        Glob = Glob.Parse(myModel.Path);
    }
}
interface IProcessedModel<Model>
{
    public static abstract ProcessFrom(Model model);
}

class MyModel
{
    public string Path { get; set; }
}

class MyProcessedModel : IProcessedModel<MyModel>
{
    public Glob Glob { get; private set; }
    public static ProcessFrom(MyModel myModel)
    {
        Glob = Glob.Parse(myModel.Path);
    }
}

Then in my action method, I can just do:
MyProcessedModel processedModel = MyProcessedModel.ProcessFrom(model); // model is the original model bound by ASP.NET
MyProcessedModel processedModel = MyProcessedModel.ProcessFrom(model); // model is the original model bound by ASP.NET

This seems to work fine. Is this a good idea instead of a custom model binder?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,828Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Is it a good idea to store CSS class as a property in my C# model?
C#CC# / help
2y ago
Is it good to use Join in linq?
C#CC# / help
3y ago
✅ is it a good disposable class?
C#CC# / help
3y ago
Is using a blazor component from a service a good idea?
C#CC# / help
2y ago