Skip to main content
.NET Framework 4.6.2+

IVariantsProvider Interface

Implemented by objects that provide a list of view variants available for the specific View, and stores the variant selected by the user.

Namespace: DevExpress.ExpressApp.ViewVariantsModule

Assembly: DevExpress.ExpressApp.ViewVariantsModule.v24.2.dll

Declaration

public interface IVariantsProvider

Remarks

The default implementation of the IVariantsProvider interface used by the View Variants Module is ModelVariantsProvider, that stores view variants in the Application Model. You can create a custom IVariantsProvider implementation and pass it to the ViewVariantsModule.VariantsProvider property.

The following snippet illustrates how to implement an IVariantsProvider that stores View Variants in the database.

public class DatabaseViewVariantsProvider : IVariantsProvider {
    XafApplication application;
    public DatabaseViewVariantsProvider(XafApplication application) {
        Guard.ArgumentNotNull(application, nameof(application));
        this.application = application;
    }
    public VariantsInfo GetVariants(string rootVariantViewId) {
        VariantsInfo result = null;
        if(application.Security.IsAuthenticated) {
            using(IObjectSpace os = application.CreateObjectSpace(typeof(ViewVariantsObject))) {
                ViewVariantsObject variants = os.FirstOrDefault<ViewVariantsObject>(obj => obj.RootViewId == rootVariantViewId);
                if(variants != null && (variants.Items.Count >= 2)) {
                    List<VariantInfo> items = new List<VariantInfo>();
                    foreach(ViewVariantObject variant in variants.Items) {
                        items.Add(new VariantInfo(variant.ID.ToString(), variant.ViewId, variant.Caption));
                    }
                    string currentVariantId;
                    if(variants.CurrentVariant != null) {
                        currentVariantId = variants.CurrentVariant.ID.ToString();
                    }
                    else {
                        currentVariantId = variants.Items[0].ID.ToString();
                    }
                    result = new VariantsInfo(variants.RootViewId, currentVariantId, items);
                }
            }
        }
        return result;
    }
    public void SaveCurrentVariantId(string rootViewId, string currentVariantId) {
        using(IObjectSpace os = application.CreateObjectSpace(typeof(ViewVariantsObject))) {
            ViewVariantObject variant = os.GetObjectByKey<ViewVariantObject>(Guid.Parse(currentVariantId));
            if((variant == null) || (variant.Owner == null)) {
                throw new InvalidOperationException();
            }
            variant.Owner.CurrentVariant = variant;
            os.CommitChanges();
        }
    }
}

The following code illustrates persistent objects used by DatabaseViewVariantsProvider to store View Variants:

[Index(nameof(RootViewId), IsUnique = true)]
[DefaultClassOptions,XafDefaultProperty(nameof(RootViewId))]
public class ViewVariantsObject : BaseObject {
    [RuleUniqueValue]
    public virtual string RootViewId { get; set; }
    public virtual ViewVariantObject CurrentVariant { get; set; }
    public virtual IList<ViewVariantObject> Items { get; set; } = new ObservableCollection<ViewVariantObject>();
}

[XafDefaultProperty(nameof(Caption))]
public class ViewVariantObject : BaseObject {
    [InverseProperty(nameof(ViewVariantsObject.Items))]
    public virtual ViewVariantsObject Owner { get; set; }
    public virtual string ViewId { get; set; }
    public virtual string Caption { get; set; }
}

// Make sure that you use options.UseChangeTrackingProxies() in your DbContext settings.
See Also