DevExpress XAF XPO

Date: 2020-01-28
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using MRP.Common;
using System.ComponentModel;

namespace MRP.Model.Masterdata
{
    [NavigationItem("Stamgegevens")]
    [CreatableItem(true)]
    [DefaultProperty("ProductSampleCustomerRetentionPeriod")]
    [ModelDefault("Caption", "Product bewaartermijn per klant")]
    public class ProductSampleCustomerRetentionPeriod : MRPBaseObject
    {
        public ProductSampleCustomerRetentionPeriod(Session session) : base(session) { }

        private Company _company;
        [Association("Company-ProductSampleCustomerRetentionPeriod")]
        [ModelDefault("Caption", "Bedrijf")]
        [RuleRequiredField("RuleRequiredField for ProductSampleCustomerRetentionPeriod.Company", DefaultContexts.Save, "Bedrijf is een verplicht veld.")]
        [VisibleInDetailView(true)]
        public Company Company
        {
            get => _company;
            set => SetPropertyValue(nameof(Company), ref _company, value);
        }

        private Customer _customer;
        [Association("Customer-ProductSampleCustomerRetentionPeriod")]
        [ModelDefault("Caption", "Klant")]
        [RuleRequiredField("RuleRequiredField for ProductSampleCustomerRetentionPeriod.Customer", DefaultContexts.Save, "Klant is een verplicht veld.")]
        [VisibleInDetailView(true)]
        public Customer Customer
        {
            get => _customer;
            set => SetPropertyValue(nameof(Customer), ref _customer, value);
        }

        private ProductSample _productSample;
        [Association("ProductSample-ProductSampleCustomerRetentionPeriod")]
        [ModelDefault("Caption", "Product monster")]
        [DataSourceCriteria("Product.Company.Oid == '@This.Company.Oid'")]
        [RuleRequiredField("RuleRequiredField for ProductSampleCustomerRetentionPeriod.ProductSample", DefaultContexts.Save, "'Product monster' is een verplicht veld.")]
        [VisibleInDetailView(true)]
        public ProductSample ProductSample
        {
            get => _productSample;
            set => SetPropertyValue(nameof(ProductSample), ref _productSample, value);
        }

        private int? _retentionPeriod;
        [ModelDefault("Caption", "Bewaartermijn")]
        [ModelDefault("ToolTip", "Bewaartermijn van de monster analyse in dagen.\nHiervoor wordt de maximale waarde van de bij het product, de klant of het type monster ingestelde bewaartermijn.")]
        [RuleValueComparison("RuleValueComparison for ProductSampleCustomerRetentionPeriod.RetentionPeriod", DefaultContexts.Save, ValueComparisonType.GreaterThan, 0, "De bewaartermijn moet groter dan nul zijn", SkipNullOrEmptyValues = true)]
        public int? RetentionPeriod
        {
            get => _retentionPeriod;
            set => SetPropertyValue(nameof(RetentionPeriod), ref _retentionPeriod, value);
        }
    }
}
        [Association("Company-ProductSampleCustomerRetentionPeriod"), Aggregated]
        [ModelDefault("Caption", "Bewaartermijnen per klant")]
        public XPCollection<ProductSampleCustomerRetentionPeriod> ProductSampleCustomerRetentionPeriods => GetCollection<ProductSampleCustomerRetentionPeriod>(nameof(ProductSampleCustomerRetentionPeriods));
// Available Attributes

[NavigationItem("Stamgegevens")]
[ImageName("EmbeddedResourceName")]
[CreatableItem(true)]
[CreatableItem(false)] // Hides new item buttons
[DefaultProperty("ProductSampleCustomerRetentionPeriod")]
[LookupEditorMode(LookupEditorMode.AllItemsWithSearch)]

[DomainComponent] // Use for non persistent domain model, used in views
[NonPersistent]
[Persistent]
[Persistent("FieldNameOrTableName")]

[Association("Company-ProductSampleCustomerRetentionPeriod")]
[RuleRequiredField("RuleRequiredField for ProductSampleCustomerRetentionPeriod.Company", DefaultContexts.Save, "Bedrijf is een verplicht veld.")]

[Browsable(false)]
[VisibleInListView(false)]
[VisibleInDetailView(false)]

[Key] // Field is Key (Primary Index)
[Key(true)] // Field is Key (Primary Index)
[NoForeignKey] // Disable foreign key generation on a class member
[Size(300)] // Field size/length
[Size(SizeAttribute.Unlimited)] // Blob/Varchar(MAX) fields
[DbType("Time")] // Custom Database Type
[ValueConverter(typeof(TimeSpanToPostgreSQLTimeValueConverter))] // Convert to database type
[NullValue("")] // When value is null, store it as given
[Indexed]
[Indexed(Unique = true)] // Field is unique indexed
[Delayed]
// Indicates that the property's value should not be loaded when a persistent object
// is being loaded. The property will be loaded the first time it is accessed.
[ReadOnly(true)]
[Editable(false)]
[MapInheritance(MapInheritanceType.ParentTable)] // Specifies the type of object-relational inheritance mapping for the class.

[Persistent("Oid"), Key(true), Browsable(false), MemberDesignTimeVisibility(false)]
[PersistentAlias(nameof(_oid))]


[DesignerCategory("")] // 
[ImmediatePostData]
[DataSourceProperty("VestigingLookup")]

[ModelDefault("ReadOnly", "True")]
[ModelDefault("LookupEditorMode", "AllItemsWithSearch")]
[ModelDefault("RowCount", "1")]
[ModelDefault("ToolTip", "Als dit vinkje aan staat, verwijst de weegbrug applicatie de vrachtwagen naar dit dok als de laadbon bulk artikelen bevat.")]
[ModelDefault("Caption", "Laadbon ladingen")]
[ModelDefault("Index", "0")]
[ModelDefault("EditMask", "dd-MM-yyyy HH:mm")]
[ModelDefault("EditMask", "##########")]
[ModelDefault("Index", "7"), Size(300)]
[ModelDefault("Index", "12"), ImmediatePostData, ModelDefault("ReadOnly", "True"), VisibleInListView(false), VisibleInLookupListView(false)]
[ModelDefault("AllowEdit", "False")]
[ModelDefault("DisplayFormat", "HH:mm")]
[ModelDefault("DisplayFormat", "{0:g}")]
[ModelDefault("DisplayFormat", "###,##0.00")]
[ModelDefault("DisplayFormat", "##########"), ModelDefault("EditMask", "##########")]
[ModelDefault("DisplayFormat", "{0:#,##0.00}"), ModelDefault("EditMask", "#,##0.00")]
//?
[Custom("DisplayFormat", "{0:#,###,##0.00}")] // DevExpress.Xpo.CustomAttribute

[RuleFromIPropertyValueValidator("RuleFromIPropertyValueValidator for Losplaats.Vestiging", DefaultContexts.Save, "U heeft geen rechten voor deze vestiging.", SkipNullOrEmptyValues = false)]
bool IPropertyValueValidator.IsPropertyValueValid(string property, ref string errorMessage)
{
    return true;
}

[XafDisplayName("Te verzenden")]
[DXDisplayName(typeof(ConsumptieInkoopContract), "InkoopSoort Display Name")]

[ListEditor(typeof(Levering), false)]
[ListEditor(typeof(DummyPersistentObject))]
[ToolboxItem(false)]

[Appearance("AppearanceAttribute for VerkoopLaadreferentie", AppearanceItemType.ViewItem, "ShowVerkoopLaadreferentieInListview = True", TargetItems = "*", Context = "ListView", BackColor = "Coral")]

[Localizable(true)]
[Category("Format")]
[Description("Specifies the text that will be displayed in the Validation Error window when the Validation Rule is broken.")]
// To enable sorting, filtering and grouping (in server-side SQL query mode) use a persistent alias
[PersistentAlias("Artikel.Code")]
public string ArtikelCode => ReferenceEquals(Artikel, null) ? "" : Artikel.Code;

[PersistentAlias("Artikel.Artikelgroep.Omschrijving")]
public string Artikelgroep => ReferenceEquals(Artikel, null) ? "" : ReferenceEquals(Artikel.Artikelgroep, null) ? "" : Artikel.Artikelgroep.Omschrijving;

[PersistentAlias("Tarief.Tariefsoort")]
public string Tariefsoort => ReferenceEquals(Tarief, null) ? "" : Tarief.Tariefsoort;


// When a field is not immediately updated or the save button is unavailable use [ImmediatePostData] attribute on e.g. a [NonPersistend] property
[ImmediatePostData]
Artikel _grondmonsterkosten;
[VisibleInListView(false), VisibleInLookupListView(false)]
[DataSourceProperty("ArtikelLookup")]
[ModelDefault("LookupEditorMode", "AllItemsWithSearch")]
public Artikel Grondmonsterkosten
{
	get { return (_grondmonsterkosten); }
	set { SetPropertyValue(nameof(Grondmonsterkosten), ref _grondmonsterkosten, value); }
}

XPCollection<Artikel> _artikellookup;
[Browsable(false)]
public XPCollection<Artikel> ArtikelLookup
{
	get
	{
		_artikellookup?.Reload();
		_artikellookup = new XPCollection<Artikel>(Session)
		{
			Criteria = new BinaryOperator(nameof(Artikel.MultiversAdministratie), this)
		};
		return _artikellookup;
	}
}
namespace DevExpress.Xpo
{
    [MemberDesignTimeVisibility(false)]
    [NonPersistent]
    [OptimisticLocking(true)]
    public abstract class XPBaseObject : PersistentBase, IEditableObject, ICustomTypeDescriptor, IComparable, IXPReceiveOnChangedFromXPPropertyDescriptor
    {
		// From PersistentBase
		public bool IsLoading { get; }
        public bool IsDeleted { get; }
		protected bool IsSaving { get; }
		// From PersistentBase: Events to override
		protected virtual void OnChanged(string propertyName, object oldValue, object newValue);
		protected virtual void OnDeleted();
		protected virtual void OnDeleting();
		protected virtual void OnLoaded();
		protected virtual void OnLoading();
		protected virtual void OnSaved();
		protected virtual void OnSaving();
		
		// Other
        public static bool AutoSaveOnEndEdit;
        protected XPBaseObject();
        protected XPBaseObject(Session session);
        protected XPBaseObject(Session session, XPClassInfo classInfo);
        [Browsable(false)]
        public object This { get; }
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [MemberDesignTimeVisibility(false)]
        [Obsolete("Use the XPBaseObject.IsLoading property instead.", true)]
        public bool Loading { get; }
        public event ObjectChangeEventHandler Changed;
        public static void AddChangedEventHandler(object persistentObject, ObjectChangeEventHandler handler);
        public static void AddChangedEventHandler(object persistentObject, IObjectChange handler);
        public static void RaiseChangedEvent(object persistentObject, ObjectChangeEventArgs args);
        public static void RemoveChangedEventHandler(object persistentObject, ObjectChangeEventHandler handler);
        public static void RemoveChangedEventHandler(object persistentObject, IObjectChange handler);
        public void Delete();
        public object Evaluate(CriteriaOperator expression);
        public object Evaluate(string expression);
        public object EvaluateAlias(string memberName);
        public bool Fit(string condition);
        public bool Fit(CriteriaOperator condition);
        public object GetMemberValue(string propertyName);
        public void Reload();
        public void Save();
        public void SetMemberValue(string propertyName, object newValue);
        protected virtual void AfterChangeByXPPropertyDescriptor();
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use the XPBaseObject.OnLoaded method instead.", true)]
        protected virtual void AfterLoad();
        protected virtual void BeforeChangeByXPPropertyDescriptor();
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use the XPBaseObject.OnSaving method instead.", true)]
        protected virtual void BeforeSave();
        protected virtual void BeginEdit();
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use the XPBaseObject.OnLoading method instead.", true)]
        protected virtual void BeginLoad();
        protected virtual void CancelEdit();
        protected virtual XPCollection<T> CreateCollection<T>(XPMemberInfo property);
        protected virtual void DoEndEditAction();
        protected virtual void EndEdit();
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use the XPBaseObject.OnLoaded method instead.", true)]
        protected virtual void EndLoad();
        protected virtual void FireChangedByCustomPropertyStore(XPMemberInfo member, object oldValue, object newValue);
        protected virtual void FireChangedByXPPropertyDescriptor(string memberName);
        protected XPCollection GetCollection(string propertyName);
        protected XPCollection<T> GetCollection<T>(string propertyName) where T : class;
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Obsolete("Use GetPropertyValue<T> instead", true)]
        protected object GetPropertyValueWithDefault(string propertyName, object defaultValue);
        protected override void Invalidate(bool disposing);
        protected void OnChanged(string propertyName);
        protected void OnChanged();
        protected override void OnChanged(string propertyName, object oldValue, object newValue);
        protected override void OnDeleting();
        protected override void OnLoaded();
        protected override void OnSaved();
        protected void RaiseChangeEvent(ObjectChangeEventArgs args);
        protected override void TriggerObjectChanged(ObjectChangeEventArgs args);
    }
}

In-direct linked collection:

private XPCollection<MyItem> _Items;
[ModelDefault("Caption", "Linked MyItems")]
[ModelDefault("ToolTip", "List of items linked to this item either directly or via the subitems.")]
public XPCollection<MyItem> Items
{
    get 
    { 
        if (_Items == null)
        {
            _Items = new XPCollection<MyItem>(
                PersistentCriteriaEvaluationBehavior.InTransaction, 
                Session, 
                new GroupOperator(GroupOperatorType.Or,
                new InOperator(nameof(MyItem.Name), LinkedSubItems),
                new InOperator(nameof(MyItem.Oid), LinkedItems)));
        }

        return _Items;
    }
}

Example of a non-persistent class

[DomainComponent]
[DefaultClassOptions]
[CreatableItem(false)]
[NavigationItem("MenuCategoryName")]
[ModelDefault("Caption", "MenuItemName")]
[ImageName("MenuImageName")]
public class NonPersistentOplossingListViewItem
{
    public NonPersistentOplossingListViewItem()
    {
    }

    [Browsable(false)]
    public Guid? Oid { get; set; }

    public string Titel { get; set; }

    public string TrefWoorden { get; set; }

    [ModelDefault("Caption", "Gekoppelde items's")]
    public string ItemsAsString => string.Join(", ", GekoppeldeItems.Select(x => $"{x.CompleteCode} {x.Omschrijving}"));

    [ModelDefault("DisplayFormat", "{0:g}")]
    [ModelDefault("EditMask", "g")]
    [ModelDefault("AllowEdit", "false")]
    public DateTime Aanmaakdatum { get; set; }
    public Oplossingstype? Type { get; set; }
    public XPCollection<Configuratieitem> GekoppeldeItems { get; set; }
}
32840cookie-checkDevExpress XAF XPO