I have a class Item:
public class Item
{
public int ID { get; private set; }
public int Value { get; private set; }
public Item CachedReference
{
get
{
return Server.Instance.Data.Items[this.ID];
}
}
public Item(int id)
{
this.ID = id;
this.Value = this.CachedReference.Value;
}
}
I have a class Equip that derives from Item:
public sealed class Equip : Item
{
public new Equip CachedReference
{
get
{
return Server.Instance.Data.Equips[this.ID];
}
}
}
public Equip(int id) : base(id) { }
When I'm calling:
Equip equip = new Equip(id);
The constructor of Item base will use the CachedReference property of Item, and not the new CachedReference property of Equip. How can I make it so if I initialize an object of type Equip, the base Item class will use the CachedReference value of Equip and not of Item?
Generally speaking I'd say that this is a bad design. Your class (Item) is handling 2 things at once (storing value and caching). Let's say for example later you want to change the cache mechanism (to Redis or sth), it would be very difficult.
Bottom line is you should have a separate class/interface to handle caching.
This is a (very) simplified version
public class Item
{
public int Id { get; }
public Item(int id)
{
Id = id;
}
}
public class Equip : Item
{
public Equip(int id) : base(id)
{
}
}
public class CacheManager
{
public TItem GetItem<TItem>(int id) where TItem : Item
{
if (typeof(TItem) == typeof(Equip))
{
return Server.Instance.Data.Equips[id];
}
return Server.Instance.Data.Items[id];
}
}
Usually, the ideal way to do that automatically is for CachedReference to be a virtual method (or: indirectly invoke a virtual method), that Equip overrides.
However! In this case, you're talking about a constructor; you should not call a virtual method during a constructor. Instead, one other way is to pass the desired value down via the constructor (it could be a different protected constructor for this purpose).
For example:
private static GetCachedReference(int id)
=> Server.Instance.Data.Items[id];
public Item(int id) : (id, GetCachedReference(id).Value) {}
protected Item(int id, int value)
{
this.ID = id;
this.Value = value;
}
private static GetCachedReference(int id)
=> Server.Instance.Data.Equips[this.ID];
public Item CachedReference => GetCachedReference(ID);
public new Equip CachedReference => GetCachedReference(ID);
public Equip(int id) : base(id, GetCachedReference(id).Value) { }
Related
I have a ParentClass. Two classes are inherit from it, FirstChildClass and SecondChildClass. A class MultipleValueTypes contains a Dictionary and a method that adds values to it. My intention is to be able to pass values of different classes, which inherit from the same abstract class to the value parameter of the Dictionary. Therefore, I initialize the dictionary with the value List<ParentClass> so that I would be able to add objects made with the child classes to the Dictionary. I can do this, but I cannot access them, therefore in the abstract class I create a way to tell them apart, a virtual method that both the children classes override to return their own class type.
I test the values they return against the enum itself and based on whether the condition is fulfilled, the object would be casted as what it is instead of a List<ParentClass>. Is this the wrong approach? Is this impossible?
I think it should work, because in my thinking the FirstObject and SecondObject are still objects of their respective classes, so casting should work and I should be able to access the overridden method.
What doesn't work: I cannot access the method that returns what type of class it is, because it only gets methods from the List<ParentClass>.
What I've tried so far: searching for a way to access the method, but I did not find any.
What I still need help with: everything mentioned above.
public abstract class ParentClass
{
public string Name { get; set; }
public ParentClass(string Name)
{
this.Name = Name;
}
public enum ChildClasses
{
NoChildClass = 0,
FirstChildClass = 1,
SecondChildClass = 2
}
public virtual ChildClasses TypeOfClass()
{
return ChildClasses.NoChildClass;
}
}
public class FirstChildClass : ParentClass
{
private string _randomvalue;
public string RandomValue { get => _randomvalue; set => _randomvalue = value; }
public FirstChildClass(string Name) : base(Name)
{
}
public void ReturnMessage()
{
Console.WriteLine("This is the FirstChildClass");
}
public override ChildClasses TypeOfClass()
{
return ChildClasses.FirstChildClass;
}
}
public class SecondChildClass : ParentClass
{
private string _randomvalue;
public string RandomValue { get => _randomvalue; set => _randomvalue = value; }
public SecondChildClass(string Name) : base(Name)
{
}
public void ReturnMessage()
{
Console.WriteLine("This is the SecondChildClass");
}
public override ChildClasses TypeOfClass()
{
return ChildClasses.SecondChildClass;
}
}
class MultipleValueTypes
{
public Dictionary<string, List<ParentClass>> ADictionary = new Dictionary<string, List<ParentClass>>();
public void AddObject(string Name, ParentClass variable)
{
if (!ADictionary.ContainsKey(Name))
{
ADictionary.Add(Name, new List<ParentClass>());
}
ADictionary[Name].Add(variable);
}
}
class Program
{
static void Main(string[] args)
{
FirstChildClass FirstObject = new FirstChildClass("FirstObject");
SecondChildClass SecondObject = new SecondChildClass("SecondObject");
MultipleValueTypes TestDictionary = new MultipleValueTypes();
TestDictionary.AddObject("FirstObject", FirstObject);
TestDictionary.AddObject("SecondObject", SecondObject);
if(TestDictionary.ADictionary["FirstObject"].TypeOfClass() == ParentClass.ChildClasses.FirstChildClass) ///List<ParentClass>' does not contain a definition for 'TypeOfClass' and no accessible extension method 'TypeOfClass' accepting a first argument of type 'List<ParentClass>' could be found (are you missing a using directive or an assembly reference?)
{
TestDictionary.ADictionary["FirstObject"] = (FirstChildClass)TestDictionary.ADictionary["FirstObject"]; ///Cannot convert type 'System.Collections.Generic.List<Dictionary.ParentClass>' to 'Dictionary.FirstChildClass
}
}
}
You forgot to use indexer of the list value of the key of the dictionary here:
==> TestDictionary.ADictionary["FirstObject"][0]
Here is your code now refactored too:
class Program
{
static void Main(string[] args)
{
var FirstObject = new FirstChildClass("FirstObject");
var SecondObject = new SecondChildClass("SecondObject");
FirstObject.ReturnMessage();
SecondObject.ReturnMessage();
MultipleValueTypes TestDictionary = new MultipleValueTypes();
TestDictionary.AddObject("FirstObject", FirstObject);
TestDictionary.AddObject("SecondObject", SecondObject);
if ( TestDictionary.ADictionary["FirstObject"][0].TypeOfClass()
== ParentClass.ChildClasses.FirstChildClass )
{
TestDictionary.ADictionary["FirstObject"][0]
= (FirstChildClass)TestDictionary.ADictionary["FirstObject"][0];
}
Console.ReadKey();
}
}
public abstract class ParentClass
{
public string Name { get; set; }
public string RandomValue { get; set; }
public ParentClass(string Name)
{
this.Name = Name;
}
public virtual void ReturnMessage()
{
Console.WriteLine($"This is the {this.GetType().Name} instance");
}
public virtual ChildClasses TypeOfClass()
{
return ChildClasses.NoChildClass;
}
public enum ChildClasses
{
NoChildClass = 0,
FirstChildClass = 1,
SecondChildClass = 2
}
}
public class FirstChildClass : ParentClass
{
public FirstChildClass(string Name)
: base(Name)
{
}
public override ChildClasses TypeOfClass()
{
return ChildClasses.FirstChildClass;
}
}
public class SecondChildClass : ParentClass
{
public SecondChildClass(string Name)
: base(Name)
{
}
public override ChildClasses TypeOfClass()
{
return ChildClasses.SecondChildClass;
}
}
class MultipleValueTypes
{
public readonly Dictionary<string, List<ParentClass>> ADictionary
= new Dictionary<string, List<ParentClass>>();
public void AddObject(string Name, ParentClass variable)
{
if ( !ADictionary.ContainsKey(Name) )
{
ADictionary.Add(Name, new List<ParentClass>());
}
ADictionary[Name].Add(variable);
}
}
If the intention is to cast the whole list from List<ParentClass> to List<FirstChildClass> and List<SecondChildClass>, then Linq is your friend, just use the Cast function:
List<FirstChildClass> firstChildClasses = TestDictionary.ADictionary["FirstObject"]
.Cast<FirstChildClass>().ToList();
List<SecondChildClass> secondChildClasses = TestDictionary.ADictionary["SecondObject"]
.Cast<SecondChildClass>().ToList();
I have the following code:
public interface BaseInterface
{
int ID { get; }
}
public interface SpecialInterface1 : BaseInterface
{
int price { get; }
}
public interface SpecialInterface1 : BaseInterface
{
int xyz { get; }
}
public class Implementation1 : SpecialInterface
{
int price { get; }
int ID { get; internal set; }
}
public class Implementation2 : SpecialInterface
{
int xyz { get; }
int ID { get; internal set; }
}
Now in a Management class I want to add the objects that implement BaseInterface into a List.
I know that I can use as or is to cast the interface to an implementation, but in my project, I have about 10 special interfaces with an implementation each so I would have to write a really big if statements.
public void Add(BaseInterface u, int id)
{
if (u is Implementation1)
{
((Implementation1)u).ID = id;
Units.Add(u);
}
if (u is Implementation2)
{
((Implementation2)u).ID = id;
Units.Add(u);
}
}
My goal is that the id is not changeable outside the implementation and I would provide only the interfaces outside my dll so none can change the id.
A solution would be to add an extra interface. This eliminates the internal setter in your implementation.
internal interface IChangeID
{
void SetID(int id);
}
public interface IBaseInterface
{
int ID { get; }
}
public class Implementation : IBaseInterface,
IChangeID
{
public void SetID(int id) { ID = id; }
public int ID { get; private set; }
}
Only the real implementations should implement IChangeID. Returning IBaseInterface or ISpecialInterface will hide the setter, because those interfaces do not inherit from IChangeID.
This would change your add into:
public void Add(BaseInterface u, int id)
{
((IChangeID)u).SetID(id);
Units.Add(u);
}
If you do want to return the concrete types, not interfaces. You could implement the given interface explicit. This will hide the set method even from the concrete implementation.
public class Implementation : IBaseInterface,
IChangeID
{
void IChangeID.SetID(int id) { ID = id; }
public int ID { get; private set; }
}
var obj = new Implementation();
obj.SetID() // This WILL NOT Compile
If you don't want to modify the interfaces and implementations, you could use C# 7's pattern matching to access the implementation type without casting. It requires 3 lines per implementation type but avoids modifying the classes:
public void Add(BaseInterface u, int id)
{
switch(u)
{
case Implementation1 u1:
u1.ID = id;
break;
case Implementation2 u1:
u1.ID = id;
break;
default :
throw new ArgumentException("Unexpected implementation!");
}
Units.Add(u);
}
The obvious disadvantage is that the code will have to be modified if a new implementation is added.
Another option is to use dynamic, losing type safety. This will fail at runtime if some implementation doesn't have a setter (eg because it was replaced by constructor initialization)
public void Add(BaseInterface u, int id)
{
dynamic x =u;
x.ID=id;
Units.Add(x);
}
While I like this answer the best,
I recommend making the ID a required parameter of all the implementation's constructors, and then to use a factory pattern to generate any instance you require. This makes any instance without the ID set throw an exception at compile time rather than runtime reducing the probability of exceptions.
Here is a simple example that gets you what you want without an additional interface. Should you choose you can combine my answer with #Iqon's answer.
public interface IInterface
{
int ID { get; }
}
internal class InternalImplementation: IInterface {
public InternalImplementation(int ID) { this.ID = ID; }
public int ID { get; set; }
}
public class MyImplementationFactoryService {
public IInterface Create() {
int id = 1 // Or however you get your ID, possibly from a DB query?
return new InternalImplementation(id);
}
public IInterface Create(type|enum createtype) {
// return type based on typeof or enum
}
}
In case you want to use reflection to set property, code below may help
public interface IBaseInterface
{
int ID { get; }
}
public class Impl1 : IBaseInterface
{
public int ID { get; internal set; }
public int Price {get; set;}
}
public class Impl2 : IBaseInterface
{
public int ID { get { return 0;} }
public int Subscription {get; set;}
}
public class Program
{
public static void Main(string[] args)
{
IBaseInterface obj1 = new Impl1();
SetProperty(obj1, "ID", 100);
Console.WriteLine("Object1 Id is {0}", obj1.ID);
IBaseInterface obj2 = new Impl2();
SetProperty(obj2, "ID", 500);
Console.WriteLine("Object2 Id is {0}", obj2.ID);
}
private static void SetProperty(IBaseInterface obj, string propertyName, object id){
if(obj.GetType().GetProperty(propertyName).CanWrite) {
obj.GetType().GetProperty(propertyName).SetValue(obj, id);
Console.WriteLine("CanWrite property '{0}' : {1}" , propertyName, obj.GetType().GetProperty(propertyName).CanWrite);
}
}
}
Output
CanWrite property 'ID' : True
Object1 Id is 100
Object2 Id is 0
First time poster...
New to C# and Generics and I have been experimenting by creating a simple series of Object Tables for read-only data entries.
On my Generic Insert routine I increment a static Id variable to ensure it is always unique. To try and prevent it being modified I set it to protected but the Generic class which then throws a compile error stating that Id can't be accessed.
I am struggling to find out why exactly as I thought "where T : DBEntity" would allow this.
Thanks in advance:
public class DBEntity
{
public int Id { get; protected set; }
}
public class Table<T> where T : DBEntity
{
static int _id = 0;
private readonly List<T> _set = new List<T>();
public IEnumerable<T> Set() { return _set; }
public void Insert(T item)
{
_id++;
item.Id = _id; //when set to protected it is inaccessible
_set.Add(item);
}
}
You're protecting the ID, so you can't set it. It's honestly as simple as that.
Also doing a generic of Table, and tying the generic to a concrete class buys you nothing. Consider an interface instead.
You could fix your issue as the following:
public interface IDatabaseItem
{
int? Id { get; }
SetID(int value);
}
public class DBEntity : IDatabaseItem
{
public int? Id { get; private set; }
public void SetID(int value)
{
if (Id == null)
{
Id = value;
}
else
{
throw new Exception("Cannot set assigned Id; can only set Id when it is not assgined.");
}
}
}
public class Table<T> where T : IDatabaseItem
{
static int _id = 0;
private readonly List<T> _set = new List<T>();
public IEnumerable<T> Set() { return _set; }
public void Insert(T item)
{
if (item.Id == null)
{
_id++;
item.SetID(_id);
_set.Add(item);
}
else
{
//Handle this case. Something else set the ID, yet you're trying to insert it. This would, with your code, imply a bug.
}
}
}
I have the following base class:
public class Base
{
public string LogicalName { get; set; }
public int NumberOfChars { get; set; }
public Base()
{
}
public Base(string logicalName, int numberOfChars)
{
LogicalName = logicalName;
NumberOfChars = numberOfChars;
}
}
and the following derived classes:
public class Derived1 : Base
{
public const string EntityLogicalName = "Name1";
public const int EntityNumberOfChars = 30;
public Derived1() : base(EntityLogicalName, EntityNumberOfChars)
{
}
}
public class Derived2 : Base
{
public const string EntityLogicalName = "Name2";
public const int EntityNumberOfChars = 50;
public Derived2()
: base(EntityLogicalName, EntityNumberOfChars)
{
}
}
and I also have this function that is provided by a service:
public IEnumerable<T> GetEntities<T>(string entityName, int numberOfChars) where T : Base
{
//Some code to get the entities
}
My problem is how can I call this function generically? I want to call it with something that looks like this:
public void TestEntities<T>() where T : Base
{
var entities = GetEntities<T>(T.EntityLogicalName, T.EntityNumberOfChars);
//some other code to test the entities
}
This of course doesn't work because at this point T is not known. How can I accomplish something similar to this? EntityLogicalName and EntityNumberOfChars are characteristics that all Base derived classes have and they never change for each derived class. Can I get them from the Base class without instantiating objects or some other way that I am not seeing?
Replace constants with getter abstract properties
public abstract class Base
{
public abstract string LogicalName { get; }
public abstract int NumberOfChars { get; }
public Base()
{
}
}
public class Derived1 : Base
{
public string LogicalName { get { return "Name1"; } }
public int NumberOfChars { get { return 30; } }
public Derived1() : base()
{
}
}
Also, you will be able to put some logic into overriden getter, e.g. :
...
public string LogicalName { get { return this.EntityMap.Name; } }
...
UPDATE: The fact that you do not want to instantiate object from class but want to be able to get that string in a strongly typed manner can be handled in one more way. It is totally separate from answer above ( Since you can't override static props in c#). Consider the following code. We are adding one more class here, but LocatorInner can be a member of BaseClass. We are using this approach a lot in several existing apps.:
public class Locator
{
public static class LocatorInner<T> where T : BaseClass
{
public static string Name { get; set; }
}
public static string GetName<T>() where T : BaseClass
{
return LocatorInner<T>.Name;
}
public static void SetName<T>(string name) where T : BaseClass
{
LocatorInner<T>.Name = name;
}
}
public class BaseClass
{
}
public class DerivedClass: BaseClass
{
static DerivedClass()
{
Locator.LocatorInner<DerivedClass>.Name = "me";
}
}
public class TestClass<T> where T : BaseClass
{
public void Method()
{
var name = Locator.GetName<T>();
}
}
IMHO, I believe using constants here is a bad design decision.
You can either solve the issue using #vittore approach, but for me it sounds like you should use meta-programming with attributes if you're looking to get data from the T generic argument
For example, what about:
public class LogicalNameAttribute : Attribute
{
public LogicalNameAttribute(string name)
{
Name = name;
}
public string Name { get; private set; }
}
public class NumberOfCharsAttribute : Attribute
{
public NumberOfCharsAttribute (int number)
{
Number = number;
}
public string Number { get; private set; }
}
[LogicalName("Name1"), NumberOfChars(30)]
public class Derived1 : Base
{
public Derived1() : base()
{
}
}
Now your service method can extract attribute metadata as follows:
public void TestEntities<T>() where T : Base
{
LogicalNameAttribute logicalNameAttr = typeof(T).GetCustomAttribute<LogicalNameAttribute>();
NumberOfCharsAttribute numberOfCharsAttr = typeof(T).GetCustomAttribute<NumberOfCharsAttribute >();
Contract.Assert(logicalNameAttr != null);
Contract.Assert(numberOfCharsAttr != null);
string logicalName = logicalNameAttr.Name;
int numberOfChars = numberOfCharsAttr.Number;
// Other stuff
}
There's a performance penalty because you need to use reflection to get attributes applied to T, but you gain the flexibility of not forcing derived classes to provide this static info.
As #vittore mentioned, move the properties to base,pass the hard coded values from derived and in creation use just defautl(T)
public IEnumerable<T> GetEntities<T>(string entityName, int numberOfChars) where T : Base
{
yield return default(T); //Is its always class use new constraint and return new T();
}
I am exploring this and see if this one make sense. For instance I have 2 abstract objects called: Customer and Tender. The relationship is that one Customer can have many Tenders.
How can I achieve the following on the TestClient app:
customer.InTender[0].ID = ???
What method to handle to handle this? Do I need to pass CustomerID into Customer constructor to achieve this or ... ?
If I want to get all tenders for that particular customer should I do this:
customer.InTender.Get()
How do I differentiate between All Tender VS All Customer Tender (point 3). I guess it will be like this. One with ID of Customer and the other one without?
inTender.Get()
public abstract class Customer
{
protected Int64 id;
protected string name;
protected ArrayList tender;
public abstract ArrayList Tender
{
get;
set;
}
public abstract Int64 ID
{
get;
set;
}
public abstract string Name
{
get;
set;
}
public abstract bool Update();
public abstract bool Add();
public abstract bool Delete();
}
public class CorporateCustomer : Customer
{
public CorporateCustomer ()
{}
public override ArrayList Tender
{
get
{
return tender
}
set
{
tender = value;
}
}
public override Int64 ID
{
get
{
return id;
}
set
{
id = value;
}
}
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public override bool Update()
{
return true;
}
public override bool Add()
{
return true;
}
public override bool Delete()
{
return true;
}
}
public abstract class Tender
{
protected Int64 id;
protected string name;
public abstract bool Update();
public abstract bool Add();
public abstract bool Delete();
}
public class InTender : Tender
{
public InTender ()
{}
public override Int64 ID
{
get
{
return id;
}
set
{
id = value;
}
}
public override string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public override bool Update()
{
return true;
}
public override bool Add()
{
return true;
}
public override bool Delete()
{
return true;
}
}
1) Don't use ArrayList, it was depreciated as of .net 2.0. You should use List, IList, or Dictionary.
Also, Customer sure seems like concrete type. Are you going to have multiple Customer classes that all inherit from it? If not, drop the Abstract. Same goes for your other classes.
2) Look up Repository objects and LazyLoading. Davy Bryon has a good series on building your own DAL. http://davybrion.com/blog/2009/08/build-your-own-data-access-layer-lazy-loading/
But either the customer should have all of the Tenders right away, or you should have a service that gets them for you. I'm not in favor of having Entities know about their persistence.
Anyway, the general approach is to have a separate Repository class that has the methods needed to get the data you need.
public class CustomerRepository
{
public List<Customer> GetAllCustomers() { .... }
public List<Tenders> GetTendersForCustomer(Customer customer) { .... }
}
I think a standard Tender class and a standard Customer class with a property List < Tender > should suffice. I don't see why you need the abstract classes and the inheritance.
class Tender {}
class Customer {
List < Tender > tenders; // would be null if customer has no tenders
....
}