Question

In a system I'm working on there is a core User table. There are also roles designating various user types and application level access.

To keep the user table succinct, there is going to be a number of tables (or possibly just 1 table) holding the extra fields needed for each user type Profile.

Rather than have a number of methods returning each Profile type, would there be any issue with returning an ExpandoObject from the ProfileService? This would allow a simple FetchProfile(foo userType) function.

e.g:

    public ExpandoObject FetchProfile(UserType userType)
    {
        dynamic x = new ExpandoObject();
        switch(stuff)
        {
            case UserType .Type1:
                //The ExpandoObject will be pulled from DB using Dapper.Net
                return x;
            case UserType .Type2:
                //The ExpandoObject will be pulled from DB using Dapper.Net
                return x;
            default:
                return null;
        }
    }

This ProfileService will be used in multiple levels of the framework (n tier.)

Are there any issues with doing this? Or would it be better to use single classes for each profile type and perhaps use generics to pull out the needed Profile class:

public T FetchProfile<T>(UserType userType) where T : IProfileObject ...

The advantage of the ExpandoObject is that the database could be constructed in such a way that new ProfileTypes can be added when the site is live without needing to add extra ProfileType classes.

Was it helpful?

Solution

The solution I arrived at was quite different to my initial ideas, so will document it here for archival purposes.

I built a form manager which allowed the addition of fields and multi-select fields (to be displayed as select lists, check-box lists etc. depending on user configuration.)

I allowed forms to be associated to roles, with fields shared across multiple forms as not to duplicate data.

This allowed me to simply pull out a form based upon role:

var form = formManager.Fetch(User.Username, Role.Name);

And loop through the form elements to generate the fields needed on user profile pages, the form manager being used to save updated data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top