Passing custom object between Android activities in C# - c#

I am creating an android app in VS2012 using Xamarin.Android. I am displaying a custom list in Main screen. I need to pass a custom object(with ID,String,String,String properties) from this Main activity to another when user clicks on list item.
Can anyone please help me with some example?
edit:
I have already tried solution mention in other question
but the problem is I am getting below exception:
This is how I am extracting in second activity
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetSerializableExtra("SelectedItemID");
i is null
and in first activity setting it like this:
Intent intent = new Intent(this, typeof(ReminderDetails));
intent.PutExtra("SelectedItemID", selectedInsurance);
StartActivity(typeof(ReminderDetails));
where class InsuranceReminderBO is defined as
public class InsuranceReminderBO : Java.Lang.Object, Java.IO.ISerializable
I have also tried using IParcelable but in that I got error Creator is not defined
in ICreator or Creator
Following the implementation of Iparcelable on CustomObject
'public class InsuranceReminderBO : Java.Lang.Object, IParcelable
{
public InsuranceReminderBO()
{
}
#region Objects and Properties
private int id;
private String strCompanyName;
private String strPremiumAmount;
private String stDueDate;
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public String Company_Name
{
get { return this.strCompanyName; }
set { this.strCompanyName = value; }
}
public String Premium_Amount
{
get { return this.strPremiumAmount; }
set { this.strPremiumAmount = value; }
}
public String Due_Date
{
get { return this.stDueDate; }
set { this.stDueDate = value; }
}
#endregion
#region IParcelable implementation
// The creator creates an instance of the specified object
private static readonly GenericParcelableCreator<InsuranceReminderBO> _creator
= new GenericParcelableCreator<InsuranceReminderBO>((parcel) => new InsuranceReminderBO(parcel));
[ExportField("CREATOR")]
public static GenericParcelableCreator<InsuranceReminderBO> GetCreator()
{
return _creator;
}
// Create a new SelectListItem populated with the values in parcel
private InsuranceReminderBO(Parcel parcel)
{
ID = parcel.ReadInt();
Company_Name = parcel.ReadString();
Premium_Amount = parcel.ReadString();
Due_Date = parcel.ReadString();
}
public int DescribeContents()
{
return 0;
}
// Save this instance's values to the parcel
public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags)
{
dest.WriteInt(ID);
dest.WriteString(Company_Name);
dest.WriteString(Premium_Amount);
dest.WriteString(Due_Date);
}
// Closest to the 'Java' way of implementing the creator
/*public sealed class SelectListItemCreator : Java.Lang.Object, IParcelableCreator
{
public Java.Lang.Object CreateFromParcel(Parcel source)
{
return new SelectListItem(source);
}
public Java.Lang.Object[] NewArray(int size)
{
return new SelectListItem[size];
}
}*/
#endregion
}
#region GenericParcelableCreator
/// <summary>
/// Generic Parcelable creator that can be used to create objects from parcels
/// </summary>
public sealed class GenericParcelableCreator<T> : Java.Lang.Object, IParcelableCreator
where T : Java.Lang.Object, new()
{
private readonly Func<Parcel, T> _createFunc;
/// <summary>
/// Initializes a new instance of the <see cref="ParcelableDemo.GenericParcelableCreator`1"/> class.
/// </summary>
/// <param name='createFromParcelFunc'>
/// Func that creates an instance of T, populated with the values from the parcel parameter
/// </param>
public GenericParcelableCreator(Func<Parcel, T> createFromParcelFunc)
{
_createFunc = createFromParcelFunc;
}
#region IParcelableCreator Implementation
public Java.Lang.Object CreateFromParcel(Parcel source)
{
return _createFunc(source);
}
public Java.Lang.Object[] NewArray(int size)
{
return new T[size];
}
#endregion
}
#endregion'
I am putting object in intent as
InsuranceReminderBO selectedInsurance = listOfInsurance[e.Position];
Intent intent = new Intent(this, typeof(ReminderDetails));
intent.PutExtra("SelectedItem", selectedInsurance);
And reading in second activity as
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetParcelableExtra("SelectedItem");
but getting i as null.

To coat tail on the servicestack.text solution, you can just download the android DLL's and reference them into your solution. You can use this and add it to your solution, build it separately, as alternatives. https://github.com/ServiceStack/ServiceStack.Text/tree/master/src/ServiceStack.Text.Android
Also I use a couple of methods to convert items back and forth that may be helpful, try
static public string ToJSON(this object item)
{
var myval = JsonSerializer.SerializeToString(item);
return myval;
}
static public T FromJSON<T>(string code)
{
var item = JsonSerializer.DeserializeFromString<T>(code);
return item;
}

There's an article on using IParcelable in Xamarin here.
Personally, I've always just serialised to JSON, passed a string extra and deserialised it on the other end.
If you're using ServiceStack.Text, which I like, you can do something like this:
intent.PutExtra("SelectedItemId", JsonSerializer.SerializeToString(selectedInsurance));
And on the other end:
var insurance = JsonSerializer.DeserializeFromString<InsuranceReminderBO>(Intent.GetStringExtra("SelectedItemId"))
No need to implement Java.Lang.Object, Java.IO.ISerializable

After doing a lot of search on Google finally i found a solution here.
Basically I used StartActivity(intent);

Related

How should I return a generic response and response code from all functions in a .NET MVC app?

I want to be able to return a generic response from function calls in the business layer of my MVC application. Most of the time I see an object create function look like this
public int Create(ICNUser item)
{
return this._repository.Create(item);
}
public void Update(ICNUser item)
{
this._repository.Create(item);
}
In this case the _repository is a repository that wraps entity framework.
This works great for a lot of cases but I want more information to be returned and I want to have a success/failure variable and a response code for why this action failed validation. I want to optionally be able to return the inserted object or a selected object.
An example would be a create user function that returns an email can't be blank error and or a user already exists error and based on the error I show the user a different message.
The problem I'm running into is I want to have unit tests cover all of the possible response codes from a function without me having to go look at the code and try to figure out what the possible return values can be. What I'm doing feels like an anti-pattern. Is there a better way to accomplish all of this?
This is what I have now.
public IGenericActionResponse<ICNUser> Create(ICNUser item)
{
return this._repository.Create(item);
}
public IGenericActionResponse Update(ICNUser item)
{
return this._repository.Update(item);
}
Interfaces
namespace Web.ActionResponses
{
public enum ActionResponseCode
{
Success,
RecordNotFound,
InvalidCreateHash,
ExpiredCreateHash,
ExpiredModifyHash,
UnableToCreateRecord,
UnableToUpdateRecord,
UnableToSoftDeleteRecord,
UnableToHardDeleteRecord,
UserAlreadyExists,
EmailCannotBeBlank,
PasswordCannotBeBlank,
PasswordResetHashExpired,
AccountNotActivated,
InvalidEmail,
InvalidPassword,
InvalidPageAction
}
public interface IGenericActionResponse
{
bool RequestSuccessful { get; }
ActionResponseCode ResponseCode { get; }
}
public interface IGenericActionResponse<T>
{
bool RequestSuccessful { get; }
bool RecordIsNull{get;}
ActionResponseCode ResponseCode { get; }
}
}
implementations
namespace Web.ActionResponses
{
public class GenericActionResponse<T> : IGenericActionResponse<T>
{
private bool _requestSuccessful;
private ActionResponseCode _actionResponseCode;
public T Item { get; set; }
public GenericActionResponse(bool success, ActionResponseCode actionResponseCode, T item)
{
this._requestSuccessful = success;
this._actionResponseCode = actionResponseCode;
this.Item = item;
}
public GenericActionResponse(bool success, ActionResponseCode actionResponseCode)
{
this._requestSuccessful = success;
this._actionResponseCode = actionResponseCode;
this.Item = default(T);
}
public bool RecordIsNull
{
get
{
return this.Item == null;
}
}
public bool RequestSuccessful
{
get
{
return this._requestSuccessful;
}
}
public ActionResponseCode ResponseCode
{
get
{
return this._actionResponseCode;
}
}
}
public class GenericActionResponse : IGenericActionResponse
{
private bool _requestSuccessful;
private ActionResponseCode _actionResponseCode;
public GenericActionResponse(bool success, ActionResponseCode actionResponseCode)
{
this._requestSuccessful = success;
this._actionResponseCode = actionResponseCode;
}
public bool RequestSuccessful
{
get
{
return this._requestSuccessful;
}
}
public ActionResponseCode ResponseCode
{
get
{
return this._actionResponseCode;
}
}
}}
MVC app
public ActionResult ValidateResetHash(string passwordResetHash)
{
IGenericActionResponse result = (IGenericActionResponse)this._userManager.IsValidPasswordResetHash(passwordResetHash);
if (result.RequestSuccessful)
{
Models.PasswordChangeModel model = new Models.PasswordChangeModel();
model.PasswordResetHash = passwordResetHash;
return View("~/Areas/Public/Views/ResetPassword/PasswordChangeForm.cshtml", model);
}
else
{
switch (result.ResponseCode)
{
case ActionResponseCode.RecordNotFound:
{
FermataFish.Models.GenericActionModel responseModel = new FermataFish.Models.GenericActionModel(true, "/Login", "Login", "You have submitted an invalid password reset link.", false);
return View("~/Views/Shared/GenericAction.cshtml", responseModel);
}
case ActionResponseCode.PasswordResetHashExpired:
{
FermataFish.Models.GenericActionModel responseModel = new FermataFish.Models.GenericActionModel(true, "/ResetPassword", "Reset Password", "You have submitted an expired password reset link. You must reset your password again to change it.", false);
return View("~/Views/Shared/GenericAction.cshtml", responseModel);
}
default:
{
FermataFish.Models.GenericActionModel responseModel = new FermataFish.Models.GenericActionModel(true, "/", "Home", "An unknown error has occured. The system administrator has been notified. Error code:" + Enum.GetName(typeof(ActionResponseCode), result.ResponseCode), false);
return View("~/Views/Shared/GenericAction.cshtml", responseModel);
}
}
}
}
The switch statement in your ValidateResetHash response is a tad code smelly. This would suggest to me that you may benefit from the use of a subclassable enum. The subclassable enum would map action response codes or types to return views with models. Here is a compiling example of how to use this.
First some class fills I used to get a compiling example:
public class GenericActionModel
{
private bool v1;
private string v2;
private string v3;
private string v4;
private bool v5;
protected GenericActionModel() {}
public GenericActionModel(bool v1, string v2, string v3, string v4, bool v5)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
}
}
public class ActionResult
{
private GenericActionModel responseModel;
private string v;
public ActionResult(string v, GenericActionModel responseModel)
{
this.v = v;
this.responseModel = responseModel;
}
}
public class PasswordChangeModel : GenericActionModel
{
public object PasswordResetHash
{
get;
set;
}
}
public interface IUserManager
{
Response IsValidPasswordResetHash(string passwordResetHash);
}
Next some infrastructure(framework) classes (I'm using StringEnum base class from the AtomicStack project for the ResponseEnum base class):
public abstract class Response
{
public abstract string name { get; }
}
public class Response<TResponse> : Response where TResponse : Response<TResponse>
{
private static string _name = typeof(TResponse).Name;
public override string name => _name;
}
// Base ResponseEnum class to be used by more specific enum sets
public abstract class ResponseEnum<TResponseEnum> : StringEnum<TResponseEnum>
where TResponseEnum : ResponseEnum<TResponseEnum>
{
protected ResponseEnum(string responseName) : base(responseName) {}
public abstract ActionResult GenerateView(Response response);
}
Here are some sample responses:
public class HashValidated : Response<HashValidated>
{
public string passwordResetHash;
}
public class InvalidHash : Response<InvalidHash> {}
public class PasswordResetHashExpired : Response<PasswordResetHashExpired> {}
public class Unexpected : Response<Unexpected> {}
A sample subclassable enum mapping the sample responses would look something like this:
public abstract class ValidateHashResponses : ResponseEnum<ValidateHashResponses>
{
public static readonly ValidateHashResponses HashOk = HashValidatedResponse.instance;
public static readonly ValidateHashResponses InvalidHash = InvalidHashResponse.instance;
public static readonly ValidateHashResponses PasswordResetHashExpired = PasswordResetHashExpiredResponse.instance;
public static readonly ValidateHashResponses Default = DefaultResponse.instance;
private ValidateHashResponses(string responseName) : base(responseName) {}
protected abstract class ValidateHashResponse<TValidateHashResponse, TResponse> : ValidateHashResponses
where TValidateHashResponse : ValidateHashResponse<TValidateHashResponse, TResponse>, new()
where TResponse : Response<TResponse>
{
public static TValidateHashResponse instance = new TValidateHashResponse();
private static string name = Response<TResponse>.Name;
protected ValidateHashResponse() : base(name) {}
}
protected class HashValidatedResponse : ValidateHashResponse<HashValidatedResponse, HashValidated>
{
public override ActionResult GenerateView(Response response)
{
PasswordChangeModel model = new PasswordChangeModel();
model.PasswordResetHash = ((HashValidated) response).passwordResetHash;
return new ActionResult("~/Areas/Public/Views/ResetPassword/PasswordChangeForm.cshtml", model);
}
}
protected class InvalidHashResponse : ValidateHashResponse<InvalidHashResponse, InvalidHash>
{
public override ActionResult GenerateView(Response response)
{
GenericActionModel responseModel = new GenericActionModel(true, "/Login", "Login", "You have submitted an invalid password reset link.", false);
return new ActionResult("~/Views/Shared/GenericAction.cshtml", responseModel);
}
}
protected class PasswordResetHashExpiredResponse : ValidateHashResponse<PasswordResetHashExpiredResponse, PasswordResetHashExpired>
{
public override ActionResult GenerateView(Response response)
{
GenericActionModel responseModel = new GenericActionModel(true, "/ResetPassword", "Reset Password", "You have submitted an expired password reset link. You must reset your password again to change it.", false);
return new ActionResult("~/Views/Shared/GenericAction.cshtml", responseModel);
}
}
protected class DefaultResponse : ValidateHashResponses
{
public static DefaultResponse instance = new DefaultResponse();
private DefaultResponse() : base("Default") {}
public override ActionResult GenerateView(Response response)
{
GenericActionModel responseModel = new GenericActionModel(true, "/", "Home", "An unknown error has occured. The system administrator has been notified. Error code:" + response.name, false);
return new ActionResult("~/Views/Shared/GenericAction.cshtml", responseModel);
}
}
}
Implementing the SampleController:
public class SampleController
{
private IUserManager _userManager;
public ActionResult ValidateResetHash(string passwordResetHash)
{
Response result = this._userManager.IsValidPasswordResetHash(passwordResetHash);
var resultType = ValidateHashResponses.TrySelect(result.name,ValidateHashResponses.Default);
return resultType.GenerateView(result);
}
}
Tweak the code above to fit your situation.
If you want to allow others to extend the ValidateHashResponses enum, you can make the constructor protected instead of private. They can then extend ValidateHashResponses and add their own additional enum values.
The point of using the subclassable enum, it to take adavantage of the TrySelect method that resolves responses to a specific enum value. Then we call the GenerateView method on the enum value to generate a view.
Another benefit of the enum is that if you need to have other decisions made based on the enum value, you simply add another abstract method to the enum and all value definitions will be forced to implement the new abstract method, unlike traditional enum/switch statement combinations where new enum values are not required to have cases added and where one may forget to revisit all of the switch statements where the enum was used.
DISCLAIMER:
I'm am the author of the AtomicStack project. Feel free to take the Subclassable enum class code from the project if you feel it would suit your needs.
UPDATE:
If you want to inject the response enum, you should create an IResponseHandler adapter interface with a GenerateViewForResponse type method and provide a concrete implementation that consumes the ValidateHashResponses enum.

Sharing session data between frames and support load/restore state

Let's say that I have a list in my main frame in a windows store app with session data. When I click an item a new frame is open where I could edit the data.
How do I properly share session data between the frames, and how do I save and restore session data so that the references between the objects are intact?
I know that I could send an object as parameter when a new frame is created. I also know how to save/restore session data. I just don't how to solve this :).
You can use NavigationService to send data to new frames as mentioned in the option below. it is more like sending keyvalur pairs as uri parameters:
NavigationService.Navigate(new Uri("/Page1.xaml?parameter1=p1&parameter2=p2", UriKind.Relative));
To get value:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string parameterValue = NavigationContext.QueryString["parameter"];
}
http://www.geekchamp.com/tips/how-to-pass-data-between-pages-in-windows-phone-alternatives
Also if you want to share complex objects you might need to create nested viewmodels but if you don't have luxury to nest viewmodels due to nature of views then you might need to create a static app cache to use a medium to persist/share object among frames.
Storing global references to objects isn't all that tricky. You can just have a static holder that does or doesn't have the responsibility to persist them. Oh, something like this:
public interface IViewModel { }
public class ViewModelOne : IViewModel { }
public class ViewModelTwo : IViewModel { }
public class ViewModelThree : IViewModel { }
public static class GlobalObjects
{
private static ViewModelOne viewModelOne = null;
public static ViewModelOne ViewModelOne
{
get { return Get<ViewModelOne>(ref viewModelOne); }
set { Set(ref viewModelOne, value); }
}
private static T Get<T>(ref T storage) where T : IViewModel, new()
{
if (storage != null)
return storage;
try
{
var json = Load(typeof(T).ToString());
return storage = Deserialize<T>(json);
}
catch (Exception)
{
return new T();
}
}
private static void Set<T>(ref T storage, T value) where T : IViewModel
{
if (storage?.Equals(value))
return;
try
{
var json = Serialize(value);
Save(json, typeof(T).ToString());
}
catch (Exception)
{
Save(string.Empty, typeof(T).ToString());
}
}
private static void Save(string value, string key)
{
throw new NotImplementedException();
}
private static string Serialize(object obj)
{
throw new NotImplementedException();
}
private static string Load(string key)
{
throw new NotImplementedException();
}
private static T Deserialize<T>(string obj)
{
throw new NotImplementedException();
}
}
Best of luck!
// Jerry
After some inspiration from Shoaib Shaikh I decided to do a global repository. Please review this cause I barely know what I’m doing :-).
I have three classes. All classes use DataContract so it’s easy to serialize. First PersonViewModel which is pretty straight forward:
[DataContract()]
public class PersonViewModel : BaseViewModel
{
public PersonViewModel(string name)
{
Name = name;
}
#region Name property
[DataMember()]
private string _Name;
public string Name
{
get
{
return _Name;
}
set
{
SetPropertyValue(ref _Name, value, () => Name);
}
}
#endregion
}
Second is the PersonListViewModel . Each PersonViewModel is stored in a global hash table with a string id. This class taking care of the ID:s. By calling RefreshPersonCollection the person list is rebuild from the global objects. Quite clumsy, it’s better to get the persons on request but I’m too tired to solve that right now :-).
[DataContract()]
public class PersonListViewModel : BaseViewModel
{
[DataMember()]
private List<string> PersonIds = new List<string>();
public PersonListViewModel()
{
Persons = new ObservableCollection<PersonViewModel>();
CreateDefaultData();
}
public void CreateDefaultData()
{
for(int i=0; i<3; i++)
{
string personid = Guid.NewGuid().ToString();
string personname = "Person " + personid;
PersonViewModel person = new PersonViewModel(personname);
PersonIds.Add(personid);
Persons.Add(person);
SharedObjects.Instance.Objects[personid] = person;
}
}
public void RefreshPersonCollection()
{
Persons = new ObservableCollection<PersonViewModel>();
foreach (string personid in PersonIds)
{
Persons.Add((PersonViewModel)SharedObjects.Instance.Objects[personid]);
}
}
public ObservableCollection<PersonViewModel> Persons{ get; set; }
}
Third class is my global repository. Also quite straight forward I think. All PersonViewModels and all PersonListViewModel is stored in this repository.
[DataContract()]
public class SharedObjects
{
public static SharedObjects Instance;
public SharedObjects()
{
Objects = new Dictionary<string, object>();
}
public void Init()
{
Objects["mainviewmodel"] = new PersonListViewModel();
}
[DataMember()]
private Dictionary<string, Object> _Objects;
public Dictionary<string, Object> Objects
{
get { return _Objects; }
set { _Objects = value; }
}
}
In my Windows store application I have a SuspensionManager that I have modified it slightly to serialize and deserialize my global repository.
First change was to add my new types to known types to be serialized. I don’t like this, would prefer that the classes could do this themselves somehow (which is possible as far as I understand it).
static SuspensionManager()
{
_knownTypes.Add(typeof(SharedObjects));
_knownTypes.Add(typeof(PersonListViewModel));
_knownTypes.Add(typeof(PersonViewModel));
}
Second change is in SaveAsync to make sure the global data is saved. Just one line was added:
//I added this:
_sessionState["globalobjects"] = SharedObjects.Instance;
// Serialize the session state synchronously to avoid asynchronous access to shared
// state
MemoryStream sessionData = new MemoryStream();
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
serializer.WriteObject(sessionData, _sessionState);
Third change is in RestoreAsync.
// Get the input stream for the SessionState file
StorageFile file = await ApplicationData.Current.LocalFolder.GetFileAsync(sessionStateFilename);
using (IInputStream inStream = await file.OpenSequentialReadAsync())
{
// Deserialize the Session State
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
_sessionState = (Dictionary<string, object>)serializer.ReadObject(inStream.AsStreamForRead());
}
//I added this:
if (_sessionState.ContainsKey("globalobjects"))
SharedObjects.Instance = (SharedObjects) _sessionState["globalobjects"];
This feels manageable but any suggestions for improvements are appreciated :-). Will this work in Windows Phone as well (except for SuspensionManager, but I guess it's something similar on that platform)?

Using session to get/set object properties everytime

I tried searching for this but I'm not even sure how to phrase it for the search.
What I'm attempting to do is have a class that everytime I access it to change it, I'm really getting and setting the value from session.
Here's what I'm trying to do (what I have so far.):
public class example
{
public int prop1 {get;set;}
public static example Instance
{
return (example)(HttpContext.Current.Session["exampleClass"] ?? new example());
}
}
public class main
{
protected void Page_Load(object sender, EventArgs e)
{
example.Instance.prop1 = "aaa"; //stores value into session
txtInput.Text = example.Instance.prop1; //retrieves value from session
}
}
I hope that makes sense on what I am trying to do.
Any help would be appreciated, thank you.
This would be easy to do with generics.
Give this a try.
public class Session
{
public User User
{
get { return Get<User>("User"); }
set {Set<User>("User", value);}
}
/// <summary> Gets. </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="key"> The key. </param>
/// <returns> . </returns>
private T Get<T>(string key)
{
object o = HttpContext.Current.Session[key];
if(o is T)
{
return (T) o;
}
return default(T);
}
/// <summary> Sets. </summary>
/// <typeparam name="T"> Generic type parameter. </typeparam>
/// <param name="key"> The key. </param>
/// <param name="item"> The item. </param>
private void Set<T>(string key, T item)
{
HttpContext.Current.Session[key] = item;
}
}
It looks like you're pretty close, but you don't have anything to actually store the object in session. Try something like this:
public static Example Instance
{
get
{
//store the object in session if not already stored
if (Session["example"] == null)
Session["example"] = new Example();
//return the object from session
return (Example)Session["example"];
}
}
This is basically just a web-friendly implementation of the Singleton Pattern.
using System.Web;
using System.Web.SessionState;
using System.Collections.Generic;
public static class ExampleSession
{
private static HttpSessionState session { get { return HttpContext.Current.Session; } }
public static string UserName
{
get { return session["username"] as string; }
set { session["username"] = value; }
}
public static List<string> ProductsSelected
{
get
{
if (session["products_selected"] == null)
session["products_selected"] = new List<string>();
return (List<string>)session["products_selected"];
}
}
}
and you can use it like so:
public class main
{
protected void Page_Load(object sender, EventArgs e)
{
//stores value into session
ExampleSession.UserName = "foo";
ExampleSession.ProductsSelected.Add("bar");
txtInput.Text = ExampleSession.UserName; //retrieves value from session
}
}
public class example {
public int prop1 { get; set; }
public static example Instance {
var exampleObject = (example)(HttpContext.Current.Session["exampleClass"]
?? new example());
HttpContext.Current.Session["exampleClass"] = exampleObject;
return exampleObject;
}
}
you can optimize it further if needed
If you're looking for a more object oriented way of doing session here is a good way of doing it below.
UserSession Class
[Serializable()]
public class UserSession
{
private CurrentRecord _CurrentRecord;
public CurrentRecord CurrentRecord
{
get
{
if ((_CurrentRecord == null))
{
_CurrentRecord = new CurrentRecord();
}
return _CurrentRecord;
}
set
{
if ((_CurrentRecord == null))
{
_CurrentRecord = new CurrentRecord();
}
_CurrentRecord = value;
}
}
}
Globals Class
public static class Globals
{
public static UserSession TheUserSession
{
get
{
if ((HttpContext.Current.Session["UserSession"] == null))
{
HttpContext.Current.Session.Add("UserSession", new CurrentUserSession());
return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
}
else
{
return (CurrentUserSession)HttpContext.Current.Session["UserSession"];
}
}
set { HttpContext.Current.Session["UserSession"] = value; }
}
}
CurrentRecord class
[Serializable()]
public class CurrentRecord
{
public int id { get; set; }
public string name { get; set; }
}
Usage in code behind
public void SetRecordId(int newId)
{
Globals.TheUserSession.CurrentRecord.id = newId;
}

How to mock a method that returns an int with MOQ

I have a class that does some retrieving of contents, and it has a method that requires some inputs (filters) before retrieving it. One of the "input" calls another method, which basically returning an int, how do I mock it using MOQ? Here's an example:
namespace MyNamespace
{
public class ConfigMetaDataColumns : MyModel
{
public int FieldID { get { return ValueInt("FieldID"); } }
public int OrderId { get { return ValueInt("OrderId"); } }
public string Label { get { return ValueString("Label"); } }
public string FieldName { get { return ValueString("FieldName"); } }
public int IsReadOnly { get { return ValueInt("IsReadOnly"); } }
}
public class GetDataClass
{
protected OpenSQLAccessLayer m_WITObject;
// Input Properties
public string GroupID;
public string PageName;
// Output Properties
/// <summary>
/// Get Config meta data
/// </summary>
/// <returns></returns>
public IEnumerable<ConfigMetaDataColumns> GetConfigMetaData()
{
var requester = new ListRequester<OpenSQL, ConfigMetaDataColumns>(m_WITObject, "Result[0].RowCount", "Result[0].Row[{0}].");
return requester.Items;
}
public void InitRequest()
{
User user = (User)HttpContext.Current.User;
m_WITObject = user.NewService<OpenSQLAccessLayer>();
m_WITObject.SetInput("MultipleResultSets", 1);
m_WITObject.SetInput("ClientID", Utils.GetClientID());
m_WITObject.SetInput("GroupID", GroupID);
m_WITObject.SetInput("PageName", PageName);
m_WITObject.Retrieve();
}
}
}
This is the "GetClientID()" method:
public static int GetClientID()
{
User oUser = (User)HttpContext.Current.User;
int nClientID;
string sClientID = string.Empty;
if (String.IsNullOrEmpty(oUser.Session("clientid")))
{
Client oClient = new Client();
}
oUser = (User)HttpContext.Current.User;
sClientID = oUser.Session("clientid");
//If we couldn't retrieve it, throw exception
if ( string.IsNullOrEmpty(sClientID) || !int.TryParse(sClientID, out nClientID))
{
throw new Exception("No clientid found in user session, client not authenticated, please login from main page");
}
return nClientID;
}
I'm just looking for a way for me to pass in a hard-coded value for the ClientID, and use this to do some unit testing with the GetDataClass class.
Thanks.
You cannot mock a static method. You should use some means of dependency injection. Say you make your GetClientId method part of an interface called IUtils like so:
public interface IUtils
{
int GetClientId();
}
And you have your concrete class Utils implemented as above, but without the method being static (and implementing the interface of course).
You now inject an implementation of your interface into the GetDataClass
class by changing its constructor, like so:
public class GetDataClass
{
private readonly IUtils utils;
public GetDataClass(IUtils utils)
{
this.utils = utils;
}
//SNIP
}
In the InitRequest method you change the call Utils.GetClientID() to this.utils.GetClientId().
You are now ready to instantiate your GetDataClass class with a mock, like so:
var utilsMock = new Mock<IUtils>();
utilsMock.Setup(u => u.GetClientId()).Returns(42);
var getDataClass = new GetDataClass(utilsMock.Object);
getDataClass.InitRequest();
And that's it.

Better way of doing strongly-typed ASP.NET MVC sessions

I am developing an ASP.NET MVC project and want to use strongly-typed session objects. I have implemented the following Controller-derived class to expose this object:
public class StrongController<_T> : Controller
where _T : new()
{
public _T SessionObject
{
get
{
if (Session[typeof(_T).FullName] == null)
{
_T newsession = new _T();
Session[typeof(_T).FullName] = newsession;
return newsession;
}
else
return (_T)Session[typeof(_T).FullName];
}
}
}
This allows me to define a session object for each controller, which is in line with the concept of controller isolation. Is there a better/more "correct" way, perhaps something that is officially supported by Microsoft?
This way other objects won't have access to this object (e.g. ActionFilter). I do it like this:
public interface IUserDataStorage<T>
{
T Access { get; set; }
}
public class HttpUserDataStorage<T>: IUserDataStorage<T>
where T : class
{
public T Access
{
get { return HttpContext.Current.Session[typeof(T).FullName] as T; }
set { HttpContext.Current.Session[typeof(T).FullName] = value; }
}
}
Then, I can either inject IUserDataStorage into controller's constructor, or use ServiceLocator.Current.GetInstance(typeof(IUserDataStorage<T>)) inside ActionFilter.
public class MyController: Controller
{
// automatically passed by IoC container
public MyController(IUserDataStorage<MyObject> objectData)
{
}
}
Of course for cases when all controllers need this (e.g. ICurrentUser) you may want to use property injection instead.
This might be better for what you want. I would just create an extension method that can access your session. The added benefit to the extension method is that you no longer have to inherit from a controller, or have to inject a dependency that really isn't necessary to begin with.
public static class SessionExtensions {
public static T Get<T>(this HttpSessionBase session, string key) {
var result;
if (session.TryGetValue(key, out result))
{
return (T)result;
}
// or throw an exception, whatever you want.
return default(T);
}
}
public class HomeController : Controller {
public ActionResult Index() {
//....
var candy = Session.Get<Candy>("chocolate");
return View();
}
}
http://codingsmith.co.za/a-better-way-of-working-with-httpcontext-session-in-mvc/ (apologies for the colours on my blog was tooling around with themes and just havent fixed it yet)
public interface ISessionCache
{
T Get<T>(string key);
void Set<T>(string key, T item);
bool contains(string key);
void clearKey(string key);
T singleTon<T>(String key, getStuffAction<T> actionToPerform);
}
public class InMemorySessionCache : BaseSessionCache
{
Dictionary<String, Object> _col;
public InMemorySessionCache()
{
_col = new Dictionary<string, object>();
}
public T Get<T>(string key)
{
return (T)_col[key];
}
public void Set<T>(string key, T item)
{
_col.Add(key, item);
}
public bool contains(string key)
{
if (_col.ContainsKey(key))
{
return true;
}
return false;
}
public void clearKey(string key)
{
if (contains(key))
{
_col.Remove(key);
}
}
}
public class HttpContextSessionCache : BaseSessionCache
{
private readonly HttpContext _context;
public HttpContextSessionCache()
{
_context = HttpContext.Current;
}
public T Get<T>(string key)
{
object value = _context.Session[key];
return value == null ? default(T) : (T)value;
}
public void Set<T>(string key, T item)
{
_context.Session[key] = item;
}
public bool contains(string key)
{
if (_context.Session[key] != null)
{
return true;
}
return false;
}
public void clearKey(string key)
{
_context.Session[key] = null;
}
}
i came up with that a few years ago and it works fine. same basic idea as everyone else i guess, why microsoft dont just implement this as standard eludes me.
I generally use this for a session key and then explicitly add objects as needed. The reason for this is it's a clean way to do it and I find that you want to keep the number of objects in session to a minimum.
This particular approach brings together forms authentication and user session into one place so you can add objects and forget about it. The argument could be made that it is a big verbose, but it does prevent any double up and you shouldn't have too many objects in session.
The following can exist in a core library or wherever you want.
/// <summary>
/// Provides a default pattern to access the current user in the session, identified
/// by forms authentication.
/// </summary>
public abstract class MySession<T> where T : class
{
public const string USERSESSIONKEY = "CurrentUser";
/// <summary>
/// Gets the object associated with the CurrentUser from the session.
/// </summary>
public T CurrentUser
{
get
{
if (HttpContext.Current.Request.IsAuthenticated)
{
if (HttpContext.Current.Session[USERSESSIONKEY] == null)
{
HttpContext.Current.Session[USERSESSIONKEY] = LoadCurrentUser(HttpContext.Current.User.Identity.Name);
}
return HttpContext.Current.Session[USERSESSIONKEY] as T;
}
else
{
return null;
}
}
}
public void LogOutCurrentUser()
{
HttpContext.Current.Session[USERSESSIONKEY] = null;
FormsAuthentication.SignOut();
}
/// <summary>
/// Implement this method to load the user object identified by username.
/// </summary>
/// <param name="username">The username of the object to retrieve.</param>
/// <returns>The user object associated with the username 'username'.</returns>
protected abstract T LoadCurrentUser(string username);
}
}
Then implement this in the following class namespaced to the root of your project (I usually put it in a code folder on mvc projects):
public class CurrentSession : MySession<PublicUser>
{
public static CurrentSession Instance = new CurrentSession();
protected override PublicUser LoadCurrentUser(string username)
{
// This would be a data logic call to load a user's detail from the database
return new PublicUser(username);
}
// Put additional session objects here
public const string SESSIONOBJECT1 = "CurrentObject1";
public const string SESSIONOBJECT2 = "CurrentObject2";
public Object1 CurrentObject1
{
get
{
if (Session[SESSIONOBJECT1] == null)
Session[SESSIONOBJECT1] = new Object1();
return Session[SESSIONOBJECT1] as Object1;
}
set
{
Session[SESSIONOBJECT1] = value;
}
}
public Object2 CurrentObject2
{
get
{
if (Session[SESSIONOBJECT2] == null)
Session[SESSIONOBJECT2] = new Object2();
return Session[SESSIONOBJECT2] as Object2;
}
set
{
Session[SESSIONOBJECT2] = value;
}
}
}
FINALLY
The big advantage of explicitly declaring what you want in session is that you can reference this absolutely anywhere in your mvc application including the views. Just reference it with:
CurrentSession.Instance.Object1
CurrentSession.Instance.CurrentUser
Again a little less generic than other approaches, but really really clear what's going on, no other rigging or dependancy injection and 100% safe to the request context.
On another note, the dicionary approaches are cool, but you still end up with strings all over the place to reference stuff. You could rig it with enums or something, but I prefer the strong typing and set and forget of the above approach.
Yes, it's years after this question was asked and there are other ways to do this... but in case anyone else shows up looking for something that combines the approaches above into an appealing one stop shop (at least one that appealed to my team and I...) Here's what we use.
public enum SessionKey { CurrentUser, CurrentMember, CurrentChart, CurrentAPIToken, MemberBanner }
public static class SessionCache {
public static T Get<T>(this HttpSessionStateBase session, SessionKey key)
{
var value = session[key.ToString()];
return value == null ? default(T) : (T) value;
}
public static void Set<T>(this HttpSessionStateBase session, SessionKey key, T item)
{
session[key.ToString()] = item;
}
public static bool contains(this HttpSessionStateBase session, SessionKey key)
{
if (session[key.ToString()] != null)
return true;
return false;
}
public static void clearKey(this HttpSessionStateBase session, SessionKey key)
{
session[key.ToString()] = null;
}
}
Then in your controllers you can do your thing with your session variables in a more strongly typed way.
// get member
var currentMember = Session.Get<Member>(SessionKey.CurrentMember);
// set member
Session.Set<Member>(SessionKey.CurrentMember, currentMember);
// clear member
Session.ClearKey(SessionKey.CurrentMember);
// get member if in session
if (Session.Contains(SessionKey.CurrentMember))
{
var current = Session.Get<Member>(SessionKey.CurrentMember);
}
Hope this helps someone!

Categories