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.
}
}
}
Related
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
I want to have class which will have an indexer and number of fields like in the following example:
public abstract class ARecord
{
public abstract double this[int index] { get; }
public abstract int NumberOfFields { get; }
}
public class Record : ARecord
{
public double Field1{ get; private set; }
public double Field2{ get; private set; }
public override int NumberOfFields { get { return 2; } }
public Record(double[] records)
{
if (records.Count() != NumberOfFields) // PROBLEM IS HERE. WHEN CALLING THIS FROM DERIVED CLASS NumberOfFields=3!
throw new ArgumentException();
this.Field1= records[0];
this.Field2 = records[1];
}
public override double this[int index]
{
get { throw new NotImplementedException(); }
}
}
public class ChildRecord : Record
{
public double Field3 { get; private set; }
public override int NumberOfFields { get { return 3; } }
public ChildRecord(double[] records)
: base(new double[] { records[0], records[1] })
{
if (records.Count() != NumberOfFields)
throw new ArgumentException();
this.Field3 = records[2];
}
public override double this[int index]
{
get { throw new NotImplementedException(); }
}
}
public static class TestRecord
{
public static void CreateRecord()
{
var record = new ChildRecord(new double[]{1.0,1.5,2.5}); // Not working
}
}
This example crashes because of polymorphic call NumberOfFields from ChildRecord inside constructor of Record.
As far as I know, I can use new insted of override to solve this problem, but in that case I cannot declare NumberOfFields as abstract in base class (which I needed).
What is the proper way to solve this problem? Is something wrong with the design?
The way you've formulated this, it cannot possibly work as intended. Assume it did (through some sort of some magic) work the way you think it should, and you were able to create your ChildRecord:
var record = new ChildRecord(new double[] { 1.0, 1.5, 2.5 });
What would you expect the value of record.NumberOfFields to be? Two or three? This particular object cannot be a ChildRecord with NumberOfFields == 3 and at the same time be a Record with NumberOfFields == 2. You get the result of the NumberOfFields implementation of the instantiated class, regardless of whether you type record as ARecord, Record or ChildRecord.
To put it another way: It makes no sense to expose ARecord.NumberOfFields to the outside, as there is no one correct answer -- it isn't a meaningful concept.
If you abstain from exposing it, you can do your validation something like this (with indexers and properties omitted):
public abstract class ARecord
{
public abstract double this[int index] { get; }
}
public class Record : ARecord
{
private const int NumberOfFields = 2;
public Record(double[] records)
{
if (records.Count() != NumberOfFields)
throw new ArgumentException();
this.Field1 = records[0];
this.Field2 = records[1];
}
}
public class ChildRecord : Record
{
private const int NumberOfFields = 3;
public ChildRecord(double[] records)
: base(new double[] { records[0], records[1] })
{
if (records.Count() != NumberOfFields)
throw new ArgumentException();
this.Field3 = records[2];
}
}
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) { }
I have a class that contains the following:
HashSet<CookieSetItem> _set = new HashSet<CookieSetItem>();
public IEnumerable<CookieSetItem> Set
{
get { return _set; }
}
public void Add(int id)
{
id.ThrowDefault("id");
var item = new CookieSetItem(id);
if (_set.Add(item))
{
// this only happens for the first call
base.Add();
}
}
When I call the add method multiple times, say with ID's 1,2,3 etc, only the first item is added.
Obviously I'm confused as a new CookieSetItem is being created each time with a unique element (the ID), so why is it not being added?.
For completeness, here's the cookie set class:
public sealed class CookieSetItem
{
readonly DateTime _added;
readonly int _id;
public DateTime Added
{
get { return _added; }
}
public int ID
{
get { return _id; }
}
public CookieSetItem(int id)
: this(id, DateTime.Now)
{
}
public CookieSetItem(int id, DateTime added)
{
id.ThrowDefault("id");
added.ThrowDefault("added");
_id = id;
_added = added;
}
}
Got to the bottom of it - more than one error, which clouded the overall view.
Firstly I updated my class with IEquatable, which fixed the adding problem. Secondly, I found that the end result which was to update a cookie with a string version of the hashset also failed due to the fact that it was not encrypted. Here's the amended class that fixed the original problem.
public sealed class DatedSet : IEquatable<DatedSet>
{
readonly DateTime _added;
readonly int _id;
public DateTime Added
{
get { return _added; }
}
public int ID
{
get { return _id; }
}
public DatedSet(int id)
: this(id, DateTime.Now)
{
}
public DatedSet(int id, DateTime added)
{
id.ThrowDefault("id");
added.ThrowDefault("added");
_id = id;
_added = added;
}
public bool Equals(DatedSet other)
{
if (other == null) return false;
return this.ID == other.ID;
}
public override bool Equals(Object obj)
{
if (obj == null) return false;
var ds = obj as DatedSet;
return ds == null ? false : Equals(ds);
}
public override int GetHashCode()
{
return ID.GetHashCode();
}
}
Thanks for the advice.
I'm wondering about what's the way to go, if I need to publicate data-interfaces but want to use them internal with extended calculated properties. To make it clearer:
// The public interface
public interface IData
{
int Property { get; }
}
// The internal interface
internal interface IExtendedData : IData
{
int ExtendedProperty { get; }
}
// The assumed implementation of someone using my interface
public class Data : IData
{
public Data(int a)
{
Property = a;
}
public int Property
{
get;
private set;
}
public override string ToString()
{
return Property.ToString();
}
}
// My implementation
internal class ExtendedData : IExtendedData
{
public ExtendedData(int a)
{
Property = a;
}
public int Property
{
get;
private set;
}
public int ExtendedProperty
{
get
{
return 2 * Property;
}
}
public override string ToString()
{
return Property.ToString() + ExtendedProperty.ToString();
}
}
// publicated by me, for the person who uses my dll
public static class Calculations
{
public static int DoSomeCalculation(IData data, int parameter)
{
// This probably don't work, but maybe shows what I want to do
IExtendedData tempData = (ExtendedData)data;
return tempData.ExtendedProperty * parameter;
}
}
I'm realy frustrated, cause I feel like missing some basical programing skills.
You could solve this problem by implementing ExtendedData as a Wrapper for a class implementing IData
internal class ExtendedData : IExtendedData
{
private IData data;
public ExtendedData(IData data)
{
this.data = data;
}
public int Property
{
get { return data.Property; }
private set { data.Property = value; }
}
public int ExtendedProperty
{
get
{
return 2 * Property;
}
}
}
and use this in DoSomeCalculation like
IExtendedData tempData = new ExtendedData(data);
ExtendedData could inherit from Data:
class ExtendedData : Data
{...}
And for creation of a Data object you add a factory like so:
public class DataFactory
{
public IData CreateData()
{
return new ExtendedData();
}
}
User have to create all its Data objects by this factory. You can ensure it by making Data's constructor internal.
In your DLL you can then cast to ExtendedData.