views:

72

answers:

4

Is there a way I can return a Guid with the code below? For example, what I'm passing in to AddSchedule is a class. I want to return an Id from the class so I can so something with it in the controller. How would I change my code to resolve this?

Controller

ModelService.AddSchedule(
                new Schedule
                {
                 Id = Guid.Empty,
                 Start = start,
                 Stop = end
                });

What I want to do with the return Guid

 ModelService.AddScheduleToPerson(
                new Schedule
                {
                 Id = ?,   // get this from above
                 UserId = userid,
                 User = username
                });

Model

public ObjectCreateStatus AddSchedule(Schedule schedule)
    {
        var client = new Services.ConfigurationClient();
        try
        {
            ConfigurationMessage cMsg =
                client.ConfigService.AddSchedule(
                    this.ControllerBase.SessionVariables.Credentials,
                    schedule
                    );
            if (cMsg.Result == ConfigurationResultEnum.Success)
                return ObjectCreateStatus.Success;                
            return ObjectCreateStatus.GeneralError;
        }
        finally
        {
            client.Close();
        }
    }
+1  A: 

If you are looking to return a GUID as well as an ObjectCreateStatus you could consider using an out parameter for it.

 public ObjectCreateStatus AddSchedule(Schedule schedule, out Guid guid) { }
Nathan Taylor
I am not a fan of out parameters in general. I feel that it is a cover up of code smell. If you need to return two disparate values from a function that function probably is doing too much. Not saying never use them. But in this case refactoring the code in question might be a better option.
Foovanadil
It really depends on the circumstance, but generally I agree. `Type.TryParse()` is a good example of a valid use of the out parameter.
Nathan Taylor
@ Nathan Tylor and Everyone -- what I specify above, how do I get the return Guid ... I mean instead of returning "ObjectCreateStatus", I want to return the "Guid".
hersh
A: 

You can use an out parameter. I'm assigning the GUID at the beginning of the method, but you may want to change that logic depending on your requirements. Here's your modified code:

public ObjectCreateStatus AddSchedule(Schedule schedule, out Guid theGuid)
    {
        theGuid = Guid.NewGuid();
        var client = new Services.ConfigurationClient();
        try
        {
            ConfigurationMessage cMsg =
                client.ConfigService.AddSchedule(
                    this.ControllerBase.SessionVariables.Credentials,
                    schedule
                    );
            if (cMsg.Result == ConfigurationResultEnum.Success)
                return ObjectCreateStatus.Success;                
            return ObjectCreateStatus.GeneralError;
        }
        finally
        {
            client.Close();
        }
    }
David Hoerster
+1  A: 

I'd create a wrapper object that can contain both your ObjectCreateStatus and your Guid and return that. Something like this:

public class ObjectCreateResult
{
    public ObjectCreateStatus CreateStatus { get; set; }
    public Guid CreateGuid { get; set; }
}


public ObjectCreateResult AddSchedule(Schedule schedule)
{
    ObjectCreateResult result = new ObjectCreateResult();

    var client = new Services.ConfigurationClient();
    try
    {
        ConfigurationMessage cMsg =
            client.ConfigService.AddSchedule(
                this.ControllerBase.SessionVariables.Credentials,
                schedule
                );
        if (cMsg.Result == ConfigurationResultEnum.Success)
        {
            result.CreateStatus = ObjectCreateStatus.Success;
            result.CreateGuid = Guid.NewGuid(); // Set your actual Guid here
        }
        else
        {
            result.CreateStatus = ObjectCreateStatus.GeneralError;
            result.CreateGuid = Guid.Empty;
        }

    }
    finally
    {
        client.Close();
    }

    return result;
}
Dan Diplo
A: 

Consider making the ObjectCreateStatus a class:

public class ObjectCreateStatus {
  public bool WasSuccessful { get; private set; }
  public Guid ScheduleId { get; private set; }

  ctor(ConfigurationResultEnum result, Guid guid) {
    WasSuccessful = result == ConfigurationResultEnum.Success;
    ScheduleId = guid
  }
}

public ObjectCreateStatus AddSchedule(Schedule schedule)
{
    var client = new Services.ConfigurationClient();
    try
    {
        ConfigurationMessage cMsg =
            client.ConfigService.AddSchedule(
                this.ControllerBase.SessionVariables.Credentials,
                schedule
                );
        return new ObjectCreateStatus(cMsg.Result, cMsg.Guid(?))
    }
    finally
    {
        client.Close();
    }
}
flq