Answer the question
In order to leave comments, you need to log in
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("В данный момент невозможно закрыть задачу");
// ... в противном случае закрывваем задачу
}
}
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("В данный момент невозможно закрыть задачу");
// ... в противном случае закрывваем задачу
}
}
_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
It is better not to keep anything extra in domain entities. In my opinion, it should be done IIssueService
in which to transfer methods AcceptToWork
, Close
etc., which will take as input the Issue on which the action is performed.
And then you can configure this IIssueService
with different managers like IConfigurationManager
and IDateTimeProvider
.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question