BExIS.Dlm.Services.MetadataStructure.MetadataAttributeManager.Delete C# (CSharp) Method

Delete() public method

Tries to delete all the attributes provided in the entities, supposed that they are all persisted and non built-in attributes.
If by deleting the any of the entities any other compound attribute will have less than 2 members, the deletion fails.
public Delete ( IEnumerable entities ) : bool
entities IEnumerable
return bool
        public bool Delete(IEnumerable<MetadataAttribute> entities)
        {
            Contract.Requires(entities != null);
            Contract.Requires(Contract.ForAll(entities, (MetadataAttribute e) => e != null));
            Contract.Requires(Contract.ForAll(entities, (MetadataAttribute e) => e.Id >= 0));
            Contract.Requires(Contract.ForAll(entities, (MetadataAttribute e) => e.IsBuiltIn == false));

            var q = from compound in MetadataCompoundAttributeRepo.Query()
                    from usage in compound.MetadataNestedAttributeUsages
                    // whether the compound is linked to entity and whether deleting entity causes the compound to have less than two members
                    where entities.Contains(usage.Member) && compound.MetadataNestedAttributeUsages.Count() <= 2
                    select compound;
            int cnt = 0;
            if ((cnt = q.Count()) > 0)
            {
                throw new Exception(string.Format("Deletion failed! At least one of the deleting attributes is used in a compound attribute with less than 3 members. Compound attributes should have at least 2 members, invariantly.", cnt));
            }

            using (IUnitOfWork uow = this.GetUnitOfWork())
            {
                IRepository<MetadataAttribute> repo = uow.GetRepository<MetadataAttribute>();

                foreach (var entity in entities)
                {
                    var latest = repo.Reload(entity);
                    repo.LoadIfNot(latest.ExtendedProperties);
                    repo.Delete(latest);
                }
                uow.Commit();
            }
            return (true);
        }

Same methods

MetadataAttributeManager::Delete ( MetadataAttribute entity ) : bool

Usage Example

Exemplo n.º 1
0
        private void createMetadataAttribute()
        {
            //UnitManager um = new UnitManager();
            //Unit km = um.Create("Kilometer", "Km", "This is the Kilometer", "Length", MeasurementSystem.Metric);
            DataTypeManager dtManager = new DataTypeManager();
            DataType dt1 = dtManager.Repo.Get(p => p.Name.Equals("String")).FirstOrDefault();
            if (dt1 == null)
            {
                dt1 = dtManager.Create("String", "A test String", System.TypeCode.String);
            }

            MetadataAttributeManager maManager = new MetadataAttributeManager();

            // for unique name checks USE maManager.MetadataAttributeRepo that is searching both simple and compound attribute names
            var msa1 = maManager.MetadataAttributeRepo.Get(p => p.ShortName.Equals("Simple 1")).FirstOrDefault();
            if (msa1 == null)
            {
                msa1 = new MetadataSimpleAttribute()
                {
                    ShortName = "Simple 1",
                    DataType = dt1,
                };
                maManager.Create((MetadataSimpleAttribute)msa1);
            }
            var msa2 = maManager.MetadataAttributeRepo.Get(p => p.ShortName.Equals("Simple 2")).FirstOrDefault();
            if (msa2 == null)
            {
                msa2 = new MetadataSimpleAttribute()
                {
                    ShortName = "Simple 2",
                    DataType = dt1,
                };
                maManager.Create((MetadataSimpleAttribute)msa2);
            }

            MetadataCompoundAttribute mca1 = (MetadataCompoundAttribute)maManager.MetadataAttributeRepo.Get(p => p.ShortName.Equals("Compound 1")).FirstOrDefault();
            if (mca1 == null)
            {
                mca1 = new MetadataCompoundAttribute()
                {
                    ShortName = "Compound 1",
                    DataType = dt1,

                };
                MetadataNestedAttributeUsage u1 = new MetadataNestedAttributeUsage()
                {
                    Label = "First member",
                    Description = "I am a link between Compound 1 and Simple 1",
                    MinCardinality = 0,
                    MaxCardinality = 2,
                    Master = mca1,
                    Member = msa1,
                };
                mca1.MetadataNestedAttributeUsages.Add(u1);

                MetadataNestedAttributeUsage u2 = new MetadataNestedAttributeUsage()
                {
                    Label = "Second member",
                    Description = "I am a link between Compound 1 and Simple 2",
                    MinCardinality = 0,
                    MaxCardinality = 2,
                    Master = mca1,
                    Member = msa2,
                };
                mca1.MetadataNestedAttributeUsages.Add(u2);

                maManager.Create(mca1);
            }

            maManager.Delete(msa1);
        }