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.v18.2.dll

Declaration

public interface IVariantsProvider
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, "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.FindObject<ViewVariantsObject>(
                new BinaryOperator("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.Oid.ToString(), variant.ViewId, variant.Caption));
                    }
                    string currentVariantId;
                    if(variants.CurrentVariant != null) {
                        currentVariantId = variants.CurrentVariant.Oid.ToString();
                    }
                    else {
                        currentVariantId = variants.Items[0].Oid.ToString();
                    }
                    result = new VariantsInfo(variants.RootViewId, currentVariantId, items);
                }
            }
        }
        return result;
    }
    public void SaveCurrentVariantId(string rootViewId, string currentVariantId) {
        using(IObjectSpace os = application.ObjectSpaceProvider.CreateObjectSpace()) {
            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:

[DefaultClassOptions, XafDefaultProperty("RootViewId")]
public class ViewVariantsObject : BaseObject {
    public ViewVariantsObject(Session session) : base(session) { }
    [Indexed(Unique = true), RuleUniqueValue]
    public string RootViewId { 
        get { return GetPropertyValue<string>("RootViewId"); } 
        set { SetPropertyValue<string>("RootViewId", value); }
    }
    public ViewVariantObject CurrentVariant { 
        get { return GetPropertyValue<ViewVariantObject>("CurrentVariant"); } 
        set { SetPropertyValue<ViewVariantObject>("CurrentVariant", value); }
    }
    [Association]
    public XPCollection<ViewVariantObject> Items { 
        get { return GetCollection<ViewVariantObject>("Items"); }
    }
}
[XafDefaultProperty("Caption")]
public class ViewVariantObject : BaseObject {
    public ViewVariantObject(Session session) : base(session) { }
    [Association]
    public ViewVariantsObject Owner { 
        get { return GetPropertyValue<ViewVariantsObject>("Owner"); } 
        set { SetPropertyValue<ViewVariantsObject>("Owner", value); }
    }
    public string ViewId { 
        get { return GetPropertyValue<string>("ViewId"); }
        set { SetPropertyValue<string>("ViewId", value); } 
    }
    public string Caption { 
        get { return GetPropertyValue<string>("Caption"); } 
        set { SetPropertyValue<string>("Caption", value); } 
    }
}
See Also