Craig
Craig

Reputation: 18684

Variable available to multiple projects

I have an ASP.Net application designed with the UI layer connecting to a Service layer, connecting to a business layer and down to the data access layer.

The classes for the layers have constructors, which accept a UserId (int). This is mainly used for logging, and things like "LastUpdateUser and CreateUser" storage.

I am working across time zones, and in my User table, I have a TimeZoneId. In the UI level, this is stored within the session state and I can then manipulate dates based on their timezone.

I could create a method in my service layer, "GetTimezoneByUser" and pass it a userId, whcih then goes down the layers to the data access layer, and gets the Timezone info.

Problem I am having is that in my data transfer object, I have some logic, which uses time based info:

public class PersonDto
{
    public int Id { get; set; }
    public string Firstname { get; set; }
    public string Surname { get; set; }
    public decimal GrossSalary { get; set; }
    public string Email { get; set; }
    public bool Deleted { get; set; }
    public List<PersonNonAvailibilityDto> NonAvailibility { get; set; } 
    public string FullName
    {
        get { return string.Format("{0} {1}", Firstname, Surname); }
    }
    public string AvailableToday
    {
        get
        {
            if (NonAvailibility != null)
            {
                var i = NonAvailibility.FirstOrDefault(t => DateTime.UtcNow.Date <= t.EndDate && DateTime.UtcNow.Date >= t.StartDate);
                if (i != null) return i.NonAvailibilityType ;
                return "";
            }
            return "";
        }
    }

What I think I need is some sort of class, that's accessable to all my layers, which has a public TimeZoneId, and if it's null, go grab it from the database via the data accessor classes. But then I need to reference this project in my data accessors.

Is it OK to reference projects in the DTO classes? They're meant to be lightweight. Is there another 'state' where I can store the timezone, which is accessable to all the layers? The last, and worst option is to pass the ZimeZoneID with the UserId - the way I do on all my constructors. But that seems a bad idea.

Upvotes: 1

Views: 207

Answers (1)

BartoszKP
BartoszKP

Reputation: 35891

The problem lies within the fact that you're DTO class contains business logic. Move the code to the layer which operates on the appropriate abstraction level and calculate the "AvailableToday" property value. The DTO object should be a plain data object.

Upvotes: 4

Related Questions