how to return guid from a class in the controller
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();
}
}
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) { }
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;
}
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();
}
}
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();
}
}
精彩评论