Abp: Abp.Domain Thread Safe

Created on 9 May 2019  路  4Comments  路  Source: abpframework/abp

public class Enterprise : AuditedAggregateRoot<string>
    {      
        public string Name { get; set; }

        public string AbbreviationName { get; set; }

        public IList<Operator> Operators => new List<Operator>();

        [DisableAuditing]
        public PointF Coordinates { get; set; }

        public virtual void AddOperatorDevice([NotNull] Operator addOperator)
        {
            Check.NotNull(addOperator, nameof(addOperator));

            var operatorItem = Operators.First(p => p.OperatorType == addOperator.OperatorType);

            if (operatorItem == null)
            {
                Operators.Add(addOperator);
                return;
            }

            foreach (var deviceId in addOperator.DeviceIds)
            {
                operatorItem.AddDeviceId(deviceId);
            }
        }
}

public enum OperatorType : byte
    {    
        Telecom,      
        Cmcc,     
        Unicom,
        Autarky
    }

public class Operator : Entity<OperatorType>
    {     
        public OperatorType OperatorType { get; set; }

        public string Name { get; set; }

        public IList<string> DeviceIds => new List<string>();

        public virtual void AddDeviceId(string deviceId)
        {
            DeviceIds.Add(deviceId);
        }

        public virtual void RemoveDeviceId(string deviceId)
        {
            DeviceIds.Remove(deviceId);
        }
    }

AddOperatorDevice need check addOperator.OperatorType have added,if there are two threads post at the same time use EnterpriseAppService ,does this make threads unsafe? such as get operatorItem null all, abp.vNext framework ensure thread safe or need ensure by developer self?this confuses me.I use mongodb as persistence锛宼here is no abp sample like embedding document

question

Most helpful comment

This is not related to the framework, however;

  • Domain entities are not required to be thread safe, they should be accessed by only one thread at a time. You should never share an entity between threads (or web requests).
  • If you want to share an entity between threads, then you care about it, framework has nothing to do. But this is definitely a bad design and avoid it.

All 4 comments

This is not related to the framework, however;

  • Domain entities are not required to be thread safe, they should be accessed by only one thread at a time. You should never share an entity between threads (or web requests).
  • If you want to share an entity between threads, then you care about it, framework has nothing to do. But this is definitely a bad design and avoid it.

@hikalkan thanks,such as conventional controllers,api can be use by many client,is it ensure domain accessed by only one thread at a time?

If you follow normal development, it is already ensured single thread/request access to an entity.

However, there is no nice way of making a class thread-safe by convention without designing the class itself thread-safe. So, will never work on this issue.

@hikalkan thank you, I follow normal development,if it is already ensured single thread/request access to an entity.I have no doubts.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SmallShrimp picture SmallShrimp  路  3Comments

Trojaner picture Trojaner  路  3Comments

hikalkan picture hikalkan  路  3Comments

mehdihadeli picture mehdihadeli  路  3Comments

leonkosak picture leonkosak  路  3Comments