Skip to main content
edited tags
Link
Mathieu Guindon
  • 75.6k
  • 18
  • 195
  • 469
added 1746 characters in body
Source Link
mirind4
  • 309
  • 2
  • 12
public class ContainerMapViewModel
{
    ...public string CompoundId { get; set; }
    public Location Location { get; set; }
    public string FullAddress { get; set; }
    public string PhoneNumber { get; set; }
    public string LayDownDate { get; set; }
    public string LayedDownBy { get; set; }
    public string TakeUpDate { get; set; }
    public string Others { get; set; }
    public Status Status { get; set; }
}
public class ContainerToMapViewModelMappingService : IContainerMappingService<ContainerMapViewModel>
{
    private readonly ContainerDataTransformer _dataTransformer = new ContainerDataTransformer();
    private readonly IDateTime _dateTime; 

    private ContainerStatusGetter _statusGetter;

    public ContainerToMapViewModelMappingService(IDateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public ContainerMapViewModel Map(Container container)
    {
        _statusGetter = new ContainerStatusGetter(_dateTime, container.Order);

        var viewModel = new ContainerMapViewModel()
        {
            CompoundId = _dataTransformer.CreateCompoundId(container),
            Location = container.Location,
            FullAddress = _dataTransformer.CreateFullAddress(container),
            PhoneNumber = _dataTransformer.GetPhoneNumber(container.Order),
            LayDownDate = _dataTransformer.GetLayDownDate(container.Order),
            LayedDownBy = container.LayedDownBy.FirstName,
            TakeUpDate = _dataTransformer.GetTakeUpDate(container.Order),
            Others = _dataTransformer.GetOthers(container.Order),
            Status = _statusGetter.Get()
        };
        
        return viewModel;
    }
}
    
public interface IDateTime
{
    DateTime Now { get; }
}

public class ContainerStatusGetter
{
    private const int NumberOfAllowedDaysToStay = 3;

    private readonly IDateTime _dateTime;
    private readonly Order _order;

    public ContainerStatusGetter(IDateTime dateTime, Order order)
    {
        _dateTime = dateTime;
        _order = order;
    }


    public Status Get()
    {
        //Somereturn business_order logic!= usingnull
 the current date and some properties of Order object of Container ? GetStatus()
            : Status.Idle;
    }

    private Status GetStatus()
    {
        return TakeUpDateIsNotDecidedAndContainerStaysMoreThanAllowedDays() 
            ? Status.Old 
            : IsTakeAble();
    }

    private bool TakeUpDateIsNotDecidedAndContainerStaysMoreThanAllowedDays()
    {
        return _order.TakeUpDate.Equals(null) && (_dateTime.Now - _order.LayDownDate).TotalDays >= NumberOfAllowedDaysToStay;
    }

    private Status IsTakeAble()
    {
        return _dateTime.Now.Equals(_order.TakeUpDate)
            ? Status.Takeable
            : Status.BeingFilled;
    }
}
public class ContainerMapViewModel
{
    ...
    public Status Status { get; set; }
}
public class ContainerToMapViewModelMappingService : IContainerMappingService<ContainerMapViewModel>
{
    private readonly IDateTime _dateTime;
    private ContainerStatusGetter _statusGetter;

    public ContainerToMapViewModelMappingService(IDateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public ContainerMapViewModel Map(Container container)
    {
        _statusGetter = new ContainerStatusGetter(_dateTime, container.Order);

        var viewModel = new ContainerMapViewModel()
        {
            ...,
            Status = _statusGetter.Get()
        };
        
        return viewModel;
    
public interface IDateTime
{
    DateTime Now { get; }
}

public class ContainerStatusGetter
{
    private readonly IDateTime _dateTime;
    private readonly Order _order;

    public ContainerStatusGetter(IDateTime dateTime, Order order)
    {
        _dateTime = dateTime;
        _order = order;
    }


    public Status Get()
    {
        //Some business logic using the current date and some properties of Order object of Container
    }
public class ContainerMapViewModel
{
    public string CompoundId { get; set; }
    public Location Location { get; set; }
    public string FullAddress { get; set; }
    public string PhoneNumber { get; set; }
    public string LayDownDate { get; set; }
    public string LayedDownBy { get; set; }
    public string TakeUpDate { get; set; }
    public string Others { get; set; }
    public Status Status { get; set; }
}
public class ContainerToMapViewModelMappingService : IContainerMappingService<ContainerMapViewModel>
{
    private readonly ContainerDataTransformer _dataTransformer = new ContainerDataTransformer();
    private readonly IDateTime _dateTime; 

    private ContainerStatusGetter _statusGetter;

    public ContainerToMapViewModelMappingService(IDateTime dateTime)
    {
        _dateTime = dateTime;
    }

    public ContainerMapViewModel Map(Container container)
    {
        _statusGetter = new ContainerStatusGetter(_dateTime, container.Order);

        var viewModel = new ContainerMapViewModel()
        {
            CompoundId = _dataTransformer.CreateCompoundId(container),
            Location = container.Location,
            FullAddress = _dataTransformer.CreateFullAddress(container),
            PhoneNumber = _dataTransformer.GetPhoneNumber(container.Order),
            LayDownDate = _dataTransformer.GetLayDownDate(container.Order),
            LayedDownBy = container.LayedDownBy.FirstName,
            TakeUpDate = _dataTransformer.GetTakeUpDate(container.Order),
            Others = _dataTransformer.GetOthers(container.Order),
            Status = _statusGetter.Get()
        };
        
        return viewModel;
    }
}
    
public interface IDateTime
{
    DateTime Now { get; }
}

public class ContainerStatusGetter
{
    private const int NumberOfAllowedDaysToStay = 3;

    private readonly IDateTime _dateTime;
    private readonly Order _order;

    public ContainerStatusGetter(IDateTime dateTime, Order order)
    {
        _dateTime = dateTime;
        _order = order;
    }


    public Status Get()
    {
        return _order != null
            ? GetStatus()
            : Status.Idle;
    }

    private Status GetStatus()
    {
        return TakeUpDateIsNotDecidedAndContainerStaysMoreThanAllowedDays() 
            ? Status.Old 
            : IsTakeAble();
    }

    private bool TakeUpDateIsNotDecidedAndContainerStaysMoreThanAllowedDays()
    {
        return _order.TakeUpDate.Equals(null) && (_dateTime.Now - _order.LayDownDate).TotalDays >= NumberOfAllowedDaysToStay;
    }

    private Status IsTakeAble()
    {
        return _dateTime.Now.Equals(_order.TakeUpDate)
            ? Status.Takeable
            : Status.BeingFilled;
    }
}
removed thanks, added ASP in text
Source Link
t3chb0t
  • 44.7k
  • 9
  • 85
  • 191

I am working on an digital map application using .NET Core 2.0. I am using the standard DI framework of ASP.NET Core. On the map different containers (the ones for transfering building and waste materials) are displayed. Every container has a status, which can be Old, Takeable, BeingFilled or Idle. Let's see first the corresponding enums: (do not care about the code parts relating to JsonConverter...)

My question is the following: Is it a good way to inject the IDateTime into my mapping service then pass it to my ContainerStatusGetter? As far as I can judge there is a code-smell here, because the IDateTime is only used in the ContainerStatusGetter. Well I could only inject the IDateTime into my ContainerStatusGetter but the problem is, that it also needs the Order object which comes from Container domain object, so it is not possible to set up this dependency in the ConfigureServices method in Startup.cs. What do you guys think, what is the best way to do this? Do you have any other recommendation regarding to my code? Maybe shall I use some other DI framework like Autofac in order to support other DI pattern like method-DI? I read that it can be also a good option, if my class can exist without that dependency. Thanks in advance to everyone!

I am working on an digital map application using .NET Core 2.0. I am using the standard DI framework of .NET Core. On the map different containers (the ones for transfering building and waste materials) are displayed. Every container has a status, which can be Old, Takeable, BeingFilled or Idle. Let's see first the corresponding enums: (do not care about the code parts relating to JsonConverter...)

My question is the following: Is it a good way to inject the IDateTime into my mapping service then pass it to my ContainerStatusGetter? As far as I can judge there is a code-smell here, because the IDateTime is only used in the ContainerStatusGetter. Well I could only inject the IDateTime into my ContainerStatusGetter but the problem is, that it also needs the Order object which comes from Container domain object, so it is not possible to set up this dependency in the ConfigureServices method in Startup.cs. What do you guys think, what is the best way to do this? Do you have any other recommendation regarding to my code? Maybe shall I use some other DI framework like Autofac in order to support other DI pattern like method-DI? I read that it can be also a good option, if my class can exist without that dependency. Thanks in advance to everyone!

I am working on an digital map application using .NET Core 2.0. I am using the standard DI framework of ASP.NET Core. On the map different containers (the ones for transfering building and waste materials) are displayed. Every container has a status, which can be Old, Takeable, BeingFilled or Idle. Let's see first the corresponding enums: (do not care about the code parts relating to JsonConverter...)

My question is the following: Is it a good way to inject the IDateTime into my mapping service then pass it to my ContainerStatusGetter? As far as I can judge there is a code-smell here, because the IDateTime is only used in the ContainerStatusGetter. Well I could only inject the IDateTime into my ContainerStatusGetter but the problem is, that it also needs the Order object which comes from Container domain object, so it is not possible to set up this dependency in the ConfigureServices method in Startup.cs. What do you guys think, what is the best way to do this? Do you have any other recommendation regarding to my code? Maybe shall I use some other DI framework like Autofac in order to support other DI pattern like method-DI? I read that it can be also a good option, if my class can exist without that dependency.

edited tags
Link
mirind4
  • 309
  • 2
  • 12
Loading
Source Link
mirind4
  • 309
  • 2
  • 12
Loading