W
W
WhiteNinja2017-08-11 13:29:41
C++ / C#
WhiteNinja, 2017-08-11 13:29:41

C#: How to correctly access configurations in the domain model?

Good afternoon!
There is an application, for example Bug Tracker, developed according to the principles of DDD (Domain Driven Design).
On the domain model layer (Domain) there is a class:

public class Issue
{
  public int IssueId { get; private set; }
  public string Num { get; private set; }
  
  // ... другие свойства
  
  public DateTime DateClosed { get; private set; } 
  
  private Issue() { } // Для EF
  
  public Issue(string num) : this() 
  {
    Num = num;
  }
  
  public void AcceptToWork()
  {
    // ...
  }
  
  public void Close() 
  {
    if (!DateTime.UtcNow - DateClosed > TimeSpan.FromHours(24)) // Число - "24 часа" хотелось бы перенести в конфигурации
      throw new InvalidOperationException("В данный момент невозможно закрыть задачу");
      
    // ... в противном случае закрывваем задачу
  }
}

To close a task (Close() method), the elapsed time check condition must be met.
The question is that we would like to transfer the value "24 hours" to the configuration file, we use json for this and our own configuration file manager: IConfigurationManager.
Is it correct to "inject" the configuration manager into the entity in the following way:
public class Issue
{
  public int IssueId { get; private set; }
  public string Num { get; private set; }
  
  // ... другие свойства
  
  public DateTime DateClosed { get; private set; } 
  
  private readonly DefaultConfiguration _configuration;
  
  private Issue() // Для EF
  { 
    var configurationManager = IoC.Resolve<IConfigurationManager<DefaultConfiguration>>();
    _configuration = configurationManager.LoadConfiguration();
  } 
  
  public Issue(string num) : this() 
  {
    Num = num;
  }
  
  public void AcceptToWork()
  {
    // ...
  }
  
  public void Close() 
  {
    if (!DateTime.UtcNow - DateClosed > TimeSpan.FromHours(_configuration.PermittedHoursToClose)) // Используем конфигурацию
      throw new InvalidOperationException("В данный момент невозможно закрыть задачу");
      
    // ... в противном случае закрывваем задачу
  }
}

Or is there a better solution?
I simplified the example a little, in fact, the Stateless framework is used to navigate through task statuses and the corresponding conditions when setting it up:
_workflow.Configure(State.InProgress)
  .Permit(Trigger.Cancel, State.Canceled)
  .PermitIf(Trigger.Close, State.Closed, () => (DateTime.UtcNow - DateClosed) > TimeSpan.FromHours(24)); // Число - "24 часа" хотелось бы перенести в конфигурации

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lam0x86, 2017-08-11
@lam0x86

It is better not to keep anything extra in domain entities. In my opinion, it should be done IIssueServicein which to transfer methods AcceptToWork, Closeetc., which will take as input the Issue on which the action is performed.
And then you can configure this IIssueServicewith different managers like IConfigurationManagerand IDateTimeProvider.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question