Categorizing class functions in groups - c#

Imagine a class as follows.. It's a class provided to me to work with.. I cannot change its source..
public class MyClass
{
object _Object { get; set; }
public void FuncA1() { _Object = new object(); }
public void FuncA2() { _Object = new List<object>(); }
public int FuncB1() { _Object = 0; return 0; }
public int FuncB2() { _Object = 123; return 123; }
public string FuncC1() { _Object = null; return null; }
public string FuncC2() { _Object = "Hello"; return "Hello"; }
}
Im trying to create a wrapper for this class, such that I can group its many functions into categories..
MyWrapper.Voids.FuncA1();
MyWrapper.Voids.FuncA2();
MyWrapper.Integers.FuncB1();
MyWrapper.Integers.FuncB2();
MyWrapper.Strings.FuncC1();
MyWrapper.Strings.FuncC2();
The only solution I can think of for this scenario is to design the wrapper like this:
public class MyWrapper
{
MyClass _Instance { get; set; }
public _Void Voids { get; private set; }
public _Integer Integers { get; private set; }
public _String Strings { get; private set; }
public class _Void
{
MyWrapper _Parent { get; set; }
public void FuncA1() { _Parent._Instance.FuncA1(); }
public int FuncA2() { return _Parent._Instance.FuncA2(); }
}
public class _Integer
{
...
}
public class _String
{
...
}
public MyWrapper()
{
_Instance = new MyClass();
Voids = new _Voids(this);
Integers = new _Integer(this);
Strings = new _String(this);
}
}
This solution works, but has a number of problems:
- The inner classes are forced to be public, which allows them to be instantiated by the user..
- I am forced to maintain a reference of the parent object in the child classes..
Is there a better way of doing this?
EDIT: The code posted initially was a bit confusing, in the sense that it was diverting attention away from the core issue and more into the issues of whether a function would cause exceptions or not if they all work on the same object..
NOTE: This is not actual code.. I hacked together this example to show what I'm trying to do.. CREATE A WRAPPER AROUND AN OBJECT (I cannot change the original object's code) AND GROUP FUNCTIONS INTO CATEGORIES..
FINAL EDIT: following suggestion by Juharr.. here's what ive done to accomplish what i wanted.. for the betterment of others..
public interface IVoid
{
void FuncA1();
void FuncA2();
}
public interface IInteger
{
int FuncB1();
int FuncB2();
}
public class MyWrapper
{
public MyClass Instance { get; private set; }
public IVoid Voids { get; private set; }
public IInteger Integers { get; private set; }
private abstract class MyBase
{
protected MyWrapper Parent { get; set; }
protected MyClass Instance { get { return Parent.Instance; } }
public MyBase(MyWrapper oParent) { Parent = oParent; }
}
private class MyVoid : MyBase, IVoid
{
public MyVoids (MyWrapper oParent) : base(oParent) { }
public void FuncA1() { Instance.FuncA1(); }
public void FuncA2() { Instance.FuncA2(); }
}
private class MyInteger : MyBase, IInteger
{
public MyInteger (MyWrapper oParent) : base(oParent) { }
public int FuncB1() { return Instance.FuncB1(); }
public int FuncB2() { return Instance.FuncB2(); }
}
public MyWrapper()
{
Instance = new MyClass();
Voids = new MyVoid(this);
Integers = new MyInteger(this);
}
}

You could write public interfaces instead. Then your inner classes don't have to be public. So something like this.
public interface IIntger
{
void Set(int iValue);
int Get();
}
public class MyWrapper
{
MyClass _Instance { get; set; }
public IInteger Integer { get; private set; }
private class _Integer : IInteger
{
MyWrapper _Parent { get; set; }
public void Set(int iValue) { _Parent._Instance.IntegerSet(iValue); }
public int Get() { return _Parent._Instance.IntegerGet(); }
}
public MyWrapper()
{
_Instance = new MyClass();
Integer = new _Integer(this);
}
}
EDIT:
To answer the second part of your question you will either need the reference to the parent class or a reference to the class you are wrapping. So you could have this instead.
public class MyWrapper
{
public IInteger Integer { get; private set; }
private class _Integer : IInteger
{
MyClass _Instance { get; set; }
public _Integer(MyClass myClass) { _Instance = myClass; }
public void Set(int iValue) { _Instance.IntegerSet(iValue); }
public int Get() { return _Instance.IntegerGet(); }
}
public MyWrapper(MyClass instance)
{
Integer = new _Integer(instance);
}
}

Related

How to use a custom comparer to sort elements alphabetically in C#

I am new to C#. I'm trying to write a custom comparer which sorts a list of CityInfo object alphabetically by cityname.
public class CityInfoComparer
{
private readonly IComparer<string> _baseComparer;
public CityInfoComparer(IComparer<string> baseComparer)
{
_baseComparer = baseComparer;
}
public int Compare(CityInfo city1, CityInfo city2)
{
return _baseComparer.Compare(city1.CityName, city2.CityName);
}
}
public class CityList
{
public List<CityInfo> CityInfos { get; set; }
public void Sort(CityInfo city1, CityInfo city2)
{
CityInfos.Sort(new CityInfoComparer(StringComparer.CurrentCulture));
//CityInfos.Sort()
}
}
I am getting an error for this line:
CityInfos.Sort(new CityInfoComparer(StringComparer.CurrentCulture));
Cannot convert from CityInfo to Systems.Generic.Collections.Icomparer
You should inherit from IComparer, see below:
public class CityInfoComparer:IComparer<CityInfo>
{
private readonly IComparer<string> _baseComparer;
public CityInfoComparer(IComparer<string> baseComparer)
{
_baseComparer = baseComparer;
}
public int Compare(CityInfo city1, CityInfo city2)
{
return _baseComparer.Compare(city1.CityName, city2.CityName);
}
}
public class CityList
{
public List<CityInfo> CityInfos { get; set; }
public void Sort()
{
CityInfos.Sort( new CityInfoComparer(StringComparer.CurrentCulture));
}
}

Get the typed object with pattern factory

see my code :
public interface IStructureType
{
int Longueur { get; set; }
int Position { get; set; }
int CompleterCodeBy { get; set; }
}
public abstract class StructureTypeFactory
{
public abstract IStructureType GetStructureType(string type);
}
public class ConcreteStructureTypeFactory : StructureTypeFactory
{
public override IStructureType GetStructureType(string type)
{
switch(type)
{
case "StructureCodeMagasin":
return new StructureCodeMagasin();
case "StructureChrono":
return new StructureChrono();
case "StructureLotSimple":
return new StructureLotSimple();
default:
throw new ApplicationException("");
}
}
}
public class StructureCodeMagasin : IStructureType
{
public int Longueur { get ; set; }
public int Position { get; set; }
public int CompleterCodeBy { get { return 2; } set { CompleterCodeBy = value; } }
public void GetCodeMagasin()
{
//some code
}
}
I try to use Factory pattern, but how I can access to method GetCodeMagasin in this example :
public MainWindow()
{
InitializeComponent();
StructureTypeFactory st = new ConcreteStructureTypeFactory();
var structure = st.GetStructureType("StructureCodeMagasin");
int longueur = structure.CompleterCodeBy;
}
I can access properties but no method, I would like structure variable will typed StructureCodeMagasin.
Thanks for help

ObservableCollection with generic type does not work

I do not know if I understood something wrong. But Visual Studio says that adding an item does not allow the conversion from ExporterTaskWorker<ExporterTypeMusic> to ExporterTaskWorker<IExporterType>. But ExporterTypeMusic implements the IExporterType interface.
What am I doing wrong?
public interface IExporterType
{
bool BrandChannelAssigning();
}
public class ExporterTypeMusic : IExporterType
{
public bool BrandChannelAssigning()
{
throw new System.NotImplementedException();
}
}
public class ExporterTaskWorker<T> : INotifyPropertyChanged where T : IExporterType
{
public Config TheConfig { get; set; }
public object SomeProperty { get; set; }
...
...
public ExporterTaskWorker(Config _config) {
}
}
public class SomeClass
{
public ObservableCollection<ExporterTaskWorker<IExporterType>> ExporterInstanceCollection { get; set; } = new ObservableCollection<ExporterTaskWorker<IExporterType>>();
public void SomeMethod()
{
Config theConfig = new Config();
ExporterTaskWorker<ExporterTypeMusic> exporterTaskWorker = new ExporterTaskWorker<ExporterTypeMusic>(theConfig);
ExporterInstanceCollection.Add(exporterTaskWorker);
}
}

Is there a name for this creation pattern?

What should I be calling the "BFactory" below. Its not really a Factory since there is no selection of a concrete class happening, and its not necessarily creating an object each time. Its kind of a Pool but the users do not return the Bs they get to the pool after they are done with them. It could be called a Cache but performance is not the primary intention. The intention is that everyone who is using the same BFactory will get the same B when they pass the same A which starts to sound kind of like a singleton-ish.
public class A
{
public int MyProperty { get; set; }
}
public class B
{
public B(A wrapped)
{
Wrapped = wrapped;
}
public A Wrapped { get; set; }
}
public class BFactory
{
private Dictionary<A,B> _created = new Dictionary<A,B>();
public B GetB(A a)
{
if (_created.ContainsKey(a) == false)
{
_created[a] = new B(a);
}
return _created[a];
}
}
here is a slightly more real example:
The value from MyModel is shown in several locations in the app by binding a TextBlock to the ValueString property of MyViewModel. The user can select to present the value as a percent or a decimal and it should be updated in all locations if it is updated in one.
public class MyModel
{
public int Value { get; set; }
}
public class MyViewModel
{
private readonly MyModel _model;
public MyViewModel(MyModel model)
{
_model = model;
}
public string ValueString
{
get { return string.Format(FormatString, _model.Value); }
}
public string FormatString { get; set; }
}
public class MyViewModelFactory
{
private readonly Dictionary<MyModel, MyViewModel> _created = new Dictionary<MyModel, MyViewModel>();
public MyViewModel GetViewModel(MyModel model)
{
if (_created.ContainsKey(model) == false)
{
_created[model] = new MyViewModel(model);
}
return _created[model];
}
}

Base class isn't Deserialized

I am using Newtonsoft.Json .Net for 4.0 for this project
Parent class:
public class CLiveThing
{
private object lawk = new object();
public Action<double> hp_cur_changed;
public Action<double> hp_max_changed;
public double hp_max { get; private set; }
public double hp_cur { get; private set; }
public void change_hp_max(double val)
{
lock (lawk)
{
hp_max += val;
if (hp_max_changed != null)
hp_max_changed(hp_max);
}
}
public void change_hp_cur(double val)
{
lock (lawk)
{
hp_cur += val;
if (hp_cur_changed != null)
hp_cur_changed(hp_cur);
}
}
}
Child class:
public class CPlayer : CLiveThing
{
public int id { get; private set; }
public CPlayer(int id)
{
this.id = id;
}
/*
* Network
*/
public string Serialize()
{
return Newtonsoft.Json.JsonConvert.SerializeObject(this);
}
public static CPlayer Deserialize(string val)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<CPlayer>(val);
}
}
Server (uses Players.CPlayers to manage all players with a generic collection)
Players.CPlayers.Serialize()
Players.CPlayers.Serialize serializes all players in the server's memory, one per line
Like so:
public static string Serialize()
{
players_lock.AcquireReaderLock(Timeout.Infinite);
string str = "";
foreach (CPlayer player in players.Values)
{
str += player.Serialize();
str += Environment.NewLine;
}
players_lock.ReleaseReaderLock();
return str;
}
Client
I put a break line in the Players.CPlayers.Deserialize loop, which reverses what the server did.
foreach (string line in split)
{
if (line.Length > 0)
{
CPlayer player = CPlayer.Deserialize(line);
addOrReplace(player.id, player);
}
}
Here's an example of one line:
What goes in:
"{\"hp_cur_changed\":null,\"hp_max_changed\":null,\"id\":1,\"hp_max\":100.0,\"hp_cur\":100.0}"
What comes out of CPlayer.Deserialize():
It only Deserialized the ID and ignored the properties in the parent class. Which is weird because the server-side did Serialize it properly. Can anyone tell me how to fix this?
I was not able to find an official reference why it's working like this but there are at least two way to solve your problem:
Declare your base class property setters as public
public double hp_cur { get; set; }
public double hp_max { get; set; }
Or annotate them with the JsonProperty attribute:
[JsonProperty]
public double hp_max { get; private set; }
[JsonProperty]
public double hp_cur { get; private set; }

Categories

Resources