Method to access/set a session object in C# .net - c#

I currently have the following code:
public MyObject SessionStore
{
get
{
if (Session["MyData"] == null)
Session["MyData"] = new MyObject();
return (MyData) Session["MyData"];
}
set
{
Session["MyData"] = (MyObject) value;
}
}
I access it using SessionStore.ThePropertyIWant
I set it using SessionStore = SessionStore
This works; but is there a better way of accomplishing the same thing?

You don't need to cast in the setter, and can make the getter more concise :
public MyData SessionStore
{
get { return (MyData)(Session["MyData"]) ?? new MyData(); }
set { Session["MyData"] = value; }
}

SessionStore is fine but you could end up with alot of properties. I tend to add protected properties to base bases and access from there.
eg:
/// <summary>
/// Gets or Sets the Current Order Line
/// </summary>
protected OrderLine CurrentOrderLine
{
get
{
if (Session["CurrentOrderLine"] == null)
{
Session["CurrentOrderLine"] = new OrderLine(this.CurrentOrder);
}
return Session["CurrentOrderLine"] as OrderLine;
}
set
{
Session["CurrentOrderLine"] = value;
}
}
then it would appear as a property on your page if you inheriet from it.

Related

How to get a property value of a class defined inside of another class through a reflection

I have a MerchantWSBO and MerchantWSVO classes.
MerchantWSBO has a property of a type of MerchantWSVO.
I need to get a value of the property of a MerchantWSVO.
So, I have a code defining both classes(classes are coming through a WebReference from a 3rd party)
public MerchantWSBO {
private MerchantWSVO overviewField;
[System.Xml.Serialization.XmlElementAttribute(IsNullable=true)]
public MerchantWSVO overview {
get {
return this.overviewField;
}
set {
this.overviewField = value;
}
}
}
public MerchantWSVO{
private System.Nullable<bool> discoverRetainedField;
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool discoverRetainedSpecified {
get {
return this.discoverRetainedFieldSpecified;
}
set {
this.discoverRetainedFieldSpecified = value;
}
}
}
I have the following method where I need to get the property value of dicoverRetained using reflection:
private string ClassToXML(Object classObject)
{
MerchantTest mt = new MerchantTest();
if(classObject is MerchantWSBO)
{
classObject.GetType().GetProperty("overviewField").GetValue(new MerchantWSVO, null);
mt.overview.discoverRetained = //need to get the value
}
var myString = new System.IO.StringWriter();
var serializer = new XmlSerializer(classObject.GetType());
serializer.Serialize(myString, classObject);
return myString.ToString();
}
Based on a parameter classObject which in this case can be of two types, I need to get a value from a property.
How can I do that?
You don't need reflection at all, simply cast the object to the correct type. Pattern matching helps here.
if(classObject is MerchantWSBO wsbo)
{
Console.WriteLine(wsbo.overview.discoverRetained);
}
or on older C# versions:
var wsbo = classObject as MerchantWSBO;
if(wsbo != null)
{
Console.WriteLine(wsbo.overview.discoverRetained);
}

c# GroupPrincipal.FindByIdentity finds the group, but when using GetMembers getting error - there is no such object on the server

this code used to work fine for the past year,
now it is still working, but i have only 4 groups that generate this error...
the code is simple:
using (var context = new PrincipalContext(ContextType.Domain, domName))
{
foreach (string grp in myGroups)
{
using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.Name, grp))
{
PrincipalSearchResult<Principal> usersList;
usersList = group.GetMembers(true);
int usersListCount = usersList.Count();
}}}
when these specific groups come to search , i get the group and can see its description in the group object variable, but when getting its members i get an error massage :
base: "There is no such object on the server.\r\n"
ErrorCode: -2147016656
again,this happens only with 4 specific groups from the same domain, and same OU.
this just started a few days ago without me changing anything, not permissions, nothing in the code, very strange...
any ideas ?
When I encountered this problem I could not have an empty group. I had to produce "best possible" results while the network people were working to resolve the "foreign SID" issue.
I know it is a lot extra but it satisfied the auditors so maybe it will help you. This is what I did:
Precursor: I had already built a class that held all the properties of the AD Entity.
Got a list of users and all their group memberships.
Wrapped the call to get members in a try... catch and when this error occurred I inserted a "Group Membership" property of "Error Retrieving members"
When I had iterated through all the Groups I grabbed a list of all groups that had the error message as a group member then queried the Users list to get a list of all the users who were members of that group.
Then inserted Property records with the found users names.
Since this answer is more about solution structure I will only give a very brief outline of the classes used. While far from elegant it gave me a reusable container that was easy to understand and share and provided a solution that was durable across several networks. It probably lacks in many ways but it passes test #1 - it worked.
public class ADPropEntry : IComparable<ADPropEntry>
{
#region Properties
public string Name { get { return _name; } set { _adName = value; SetPropVals(_adName); } }
public string Value { get { return _v; } set { _v = value; DoValConversion(); } }
public bool IsVisible { get { return _isVis; } set { _isVis = value; } }
public string ConvertTo { get { return _convertVal; } set { _convertVal = value; } }
public int ID { get { return _id; } set { _id = value; } }
#endregion
private void SetPropVals(string s)
{
switch (s)
{
case "accountexpires": _name = "Account Expires"; _isVis = false; _convertVal = "FromFileTime"; break;
... more handles each property conversion
}
}
}
public class ADEntity : IComparable<ADEntity>
{
#region Properties
public string Name { get { return _name; } set { _name = value; } }
public List<ADPropEntry> MyProperty { get { return _ade; } set { _ade = value; } }
public string EntityType { get { return _entT; } set { _entT = value; } }
public string ADName { get { return GetProperty("SAM Account Name"); } }
#endregion
}
This formed provided me a durable data container and then I used another class to query AD in whatever method makes sense. This was packaged in a DLL that the client application could use.
class ADAccess
{
#region Properties
public bool HasErrors { get { return (bool)(_errMsg.Length > 10); } }
public string ErrorMsg { get { return _errMsg; } }
public List<ADEntity> GroupEntries { get { return _lstGrps; } }
public List<ADEntity> UserEntries { get { return _lstUsrs; } }
public List<ADEntity> PrinterEntries { get { return _lstPrts; } }
public List<ADEntity> ComputerEntries { get { return _lstCmps; } }
#endregion
public List<ADEntity> GetADListByMSO(string groupType)
{
if (groupType == "")
{
// get them all return an empty list populating properties
}
else
{
// set the context and fetch return populated list
}
}
Used the same structure to report on SQL server permissions as well.
i found out the issue,
the problematic groups contained users from different domains,
once removed these users from the groups , everything went back to work.
thanks.

Xamarin.Forms jamesmontemagno settings plugin

I am building xamarin forms app, I am using jamesmontemagno settings plugin and I'm adding a property which list of a class. But the thing is I'm not able to add or insert to the list, every time I use method Add or Insert it jump into the get not the set and I can't understand why.
here is my code and thanks for the help in advance:
//adding an item to the list
private void order(Sales_Order_Items sale)
{
orderlist.Add(sale);
Settings.Usercartlist.Add(sale);
}
I can only set it by using this code
Settings.Usercarlist=orderlist;
the property in the settings file
public static List<Sales_Order_Items> Usercartlist
{
set
{
string listValue = JsonConvert.SerializeObject(value);
AppSettings.AddOrUpdateValue(myIntListKey, listValue);
}
get
{
string value = AppSettings.GetValueOrDefault(myIntListKey, string.Empty);
List<Sales_Order_Items> myList;
if (string.IsNullOrEmpty(value))
myList = new List<Sales_Order_Items>();
else
myList = JsonConvert.DeserializeObject<List<Sales_Order_Items>>(value);
return myList;
}
}
When adding an item, the actual object reference does not change. Only the value changes. This is not picked up by the getter or setter.
The only way to update after each item is to add the line you already have: Settings.Usercarlist=orderlist; after each operation.
I am using like the following way you can try that
Ex.
private static UserDetails _currentUser = null;
public static UserDetails CurrentUser
{
get
{
if (_currentUser == null)
{
string data = AppSettings.GetValueOrDefault(nameof(CurrentUser), string.Empty);
if (data != null)
_currentUser = JsonConvert.DeserializeObject<LoginResponse>(data);
}
return _currentUser;
}
set
{
string data = JsonConvert.SerializeObject(value);
AppSettings.AddOrUpdateValue(nameof(CurrentUser), data);
_currentUser = value;
}
}

Adding a bool for each property

I'm building a c# class that works with two different data sources. It will load a data source and take a configuration set from a function. Then I want to do several tasks on all properties within the object.
for example.
public String StreetAddress
{
get { return _streetAddress; }
set
{
if (value.Length <= 64)
_streetAddress = value;
else
_streetAddress = value.Substring(0, 1024).Trim();
}
}
public String City
{
get { return _city; }
set
{
if (value.Length <= 128)
_city = value;
else
_city = value.Substring(0, 128).Trim();
}
}
public String State
{
get { return _state; }
set
{
if (value.Length <= 128)
_state = value;
else
_state = value.Substring(0, 128).Trim();
}
}
So that holds the data from one side. I was hoping to be able to store and set a change flag on each property. So if we take State for example. If the person is moved from Texas to Illinois I want to set a bool within that property to note the change then be able to loop over all changes before saving the object to the DB. But I don't see any way to assign another state variable within that property. Is the best way to write another object on top of this to control it or is there another more creative way to store multiple strings within the one property?
If you'd like an OOP way of doing the thing, you can:
Define an interface and a class for holding your property, such as:
interface IPropertySlot
{
bool IsDirty { get; }
void ResetIsDirty();
object UntypedValue { get; }
}
class PropertySlot<T>:IPropertySlot
{
public T Value { get; private set; }
public bool SetValue(T value)
{
if (!Equals(_value, Value))
{
Value = value;
IsDirty = true;
return true;
}
return false;
}
public bool IsDirty { get; private set; }
public void ResetIsDirty()
{
IsDirty = false;
}
public object UntypedValue
{
get { return Value; }
}
}
Store your properties inside your class in a dictionary from String (for name of property) to IPropertySlot and get/set them through a pair of methods:
void SetProperty<T>(string name, T value)
{
IPropertySlot property;
if (!_properties.TryGetValue(name, out property))
{
property = new PropertySlot<T>();
_properties[name] = property;
}
((PropertySlot<T>)property) .SetValue(value);
}
T GetProperty<T>(string name)
{
IPropertySlot property;
if (!_properties.TryGetValue(name, out property))
{
property = new PropertySlot<T>();
_properties[name] = property;
}
return ((PropertySlot<T>)property).Value;
}
Finding the changed properties later is just a matter of going over the _properties.Values and finding which of them are IsDirty.
This approach also gives you a way to add more functionality to your properties in an OO manner (such as raising PropertyChanged/PropertyChanging events, mapping it to DB fields, etc.).
In such a situation I'd prefer an approach external to the Dto implementation.
Implement some unit that would take two instances of a class, and determine all the differences.
Map each property to compare:
static PropertyManager<Dto> manager = new PropertyManager<Dto>()
.Map(x => x.City)
.Map(x => x.StreetAddress);
Use two instances to compute difference:
var a = new Dto{ StreetAddress = "Foo", City = "Bar" };
var b = new Dto{ StreetAddress = "Foo", City = "Baz" };
var differences = manager.ComputeDifferences(a,b).ToList();
if( differences.Any() )
{
Console.WriteLine("Instances differ");
}
foreach (var diff in differences)
{
Console.WriteLine(diff);
}
This sample code prints out:
Instances differ
x.City
Here is a complete code example:
https://dotnetfiddle.net/4sNeoN

C# Generic List Property Set/Add behaviour

I want to store a generic list in the viewstate in an ASP.NET user control, so I have the following code:
protected List<MoodleCourse> MoodleCoursesCreated
{
get
{
if (ViewState["MoodleCoursesCreated"] == null)
{
return new List<MoodleCourse>();
}
else
{
return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
}
}
set
{
ViewState["MoodleCoursesCreated"] = value;
}
}
So to add an item to the list, I call MoodleCoursesCreated.Add(new MoodleCourse);
However it seems then I must do MoodleCourseCreated = MoodleCourseCreated to activate the setter so the list actually gets stored in the viewstate. I suspect there is a more elegant way to achieve this, does anyone have any suggestions? Cheers
Calling .Add() you never trigger the setter. I guess, you don't need setter at all. Instead, override Add() method or create your own AddMoodleCourse(MoodleCourse moodleCourse):
protected void AddMoodleCourse(MoodleCourse moodleCourse)
{
var courses = ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
if (courses == null)
{
courses = new List<MoodleCourse>();
ViewState["MoodleCoursesCreated"] = courses;
}
courses.Add(moodleCourse);
}
and now call MoodleCoursesCreated.AddMoodleCourse(new MoodleCourse);
protected List<MoodleCourse> MoodleCoursesCreated
{
get
{
if (ViewState["MoodleCoursesCreated"] == null)
{
ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
}
return (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
}
set
{
ViewState["MoodleCoursesCreated"] = value;
}
}
Don't use a property if you want it to have side-effects in the getter. Instead just use a method like this:
protected List<MoodleCourse> GetMoodleCourses()
{
List<MoodleCourse> list = (List<MoodleCourse>)ViewState["MoodleCoursesCreated"];
if (list == null)
{
list = new List<MoodleCourse>();
ViewState["MoodleCoursesCreated"] = list;
}
return list;
}
you should hold the same reference in the getter:
protected List<MoodleCourse> MoodleCoursesCreated
{
get
{
if (ViewState["MoodleCoursesCreated"] == null)
ViewState["MoodleCoursesCreated"] = new List<MoodleCourse>();
return ViewState["MoodleCoursesCreated"] as List<MoodleCourse>;
}
set
{
ViewState["MoodleCoursesCreated"] = value;
}
}

Categories