Databuilder pattern issue - c#

I am playing with data builder pattern, and I am failing to understand some behaviours.
I wrote this simplified version of what I am trying to achieve below
public abstract class DataBuilderParent
{
private MyParent myParent;
protected void SetDataBuilder(MyParent myParent)
{
this.myParent = myParent;
}
public DataBuilderParent WithId(int id)
{
myParent.Id = id;
return this;
}
}
public class DataBuilderChild : DataBuilderParent
{
private readonly MyChild myChild = new MyChild();
public DataBuilderChild()
{
base.SetDataBuilder(myChild);
}
public DataBuilderChild WithDescription(string description)
{
myChild.Description = description;
return this;
}
private MyChild Build()
{
return myChild;
}
public static implicit operator MyChild(DataBuilderChild dataBuilder)
{
return dataBuilder.Build();
}
}
public class MyParent
{
public int Id { get; set; }
}
public class MyChild : MyParent
{
public string Description { get; set; }
}
Usage of the code above
internal class Program
{
private static void Main(string[] args)
{
MyChild child = new DataBuilderChild().WithDescription("");
}
}
Now it will create the child for me, also the intellisense shows up that I can do this new DataBuilderChild().WithId(1).WithDescription("");
however when I try to build it fails. I do not understand why I am not able to access this method ? it is public and its visible by the intellisense.
Can anybody explain how to make it work please?
Thank you
EDIT
The error message is: Error 1 Cannot implicitly convert type 'TestProgram.Program.DataBuilderParent' to 'TestProgram.Program.MyChild' C:\Apps\TestProgram\Program.cs 44 29 TestProgram

I just refactored my answer from the comments.
public class MyParent
{
public int Id { get; set; }
}
public class MyChild : MyParent
{
public string Description { get; set; }
}
Make the WithDescription method in the DataBuilderParent class as abstract.
public abstract class DataBuilderParent
{
private MyParent myParent;
protected void SetDataBuilder(MyParent myParent)
{
this.myParent = myParent;
}
public DataBuilderParent WithId(int id)
{
myParent.Id = id;
return this;
}
public abstract DataBuilderParent WithDescription(string description);
private MyChild BuildAsChild()
{
return myParent as MyChild;
}
private MyParent Build()
{
return myParent;
}
public static implicit operator MyChild(DataBuilderParent dataBuilder)
{
return dataBuilder.BuildAsChild();
}
public static implicit operator MyParent(DataBuilderParent dataBuilder)
{
return dataBuilder.Build();
}
}
Then the DataBuilderChild
public class DataBuilderChild : DataBuilderParent
{
private readonly MyChild myChild = new MyChild();
public DataBuilderChild()
{
base.SetDataBuilder(myChild);
}
public override DataBuilderParent WithDescription(string description)
{
myChild.Description = description;
return this;
}
private MyChild Build()
{
return myChild;
}
public static implicit operator MyChild(DataBuilderChild dataBuilder)
{
return dataBuilder.Build();
}
}
You would then build like this
class Program
{
static void Main(string[] args)
{
var childBuilder = new DataBuilderChild().WithId(1).WithDescription("Child");
MyParent parent = childBuilder;
MyChild child = childBuilder;
Console.WriteLine(#"Parent With Id {0}", parent.Id);
Console.WriteLine(#"Child With Id {0} and Desciprtion - {1}", child.Id, child.Description);
Console.ReadKey();
}
}

The return value of WithId is DataBuilderParent, which does not define the method WithDescription; this is only defined in the derived class DataBuilderChild. Apparently you aim at having some Named Constructor idiom on a class hierarchy, which cannot be implemented this way.
Furthermore, DataBuilderParent does not define a cast operator, neither to MyChild nor to MyParent, whereas DataBuilderChild defeines a cast operator to MyChild, as stated in the error message.

Related

Downcasting a List<AbstractClass> object to what the object actually is

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();

Generic class containing child of another generic type

The title may be a bit confusing, but I'm unsure how to phrase it differently.
I have a class of a generic type. I want the class to contain a child of the same class, but with another generic type. Something like this :
public class Test<Foo>
{
private readonly Foo _myFoo;
public Test<ChildFoo> Child { get; set; }
public Test(Foo foo)
{
_myFoo = foo;
}
}
public class Impl
{
public void FooTest()
{
var parent = new Test<string>("tester");
var child = new Test<int>(1234);
parent.Child = child;
}
}
But I can't have a Child with "ChildFoo" generic. Any other way of doing this?
Try it like this.
public class Test<T1, T2>
{
private readonly T1 _myFoo;
public T2 Child { get; set; }
public Test(T1 foo)
{
_myFoo = foo;
}
}
public class Impl
{
public void FooTest()
{
var parent = new Test<string, Test<int, object>>("tester");
var child = new Test<int, object>(1234);
parent.Child = child;
}
}
Since the first solution does not satisfy your needs I have one more idea that involves interface and let's you work with the child as if it was Test<,>.
public class Test<T1, T2> : ITest where T2 : ITest
{
private readonly T1 _myFoo;
public T2 Child { get; set; }
public void A()
{
}
public void B()
{
Child.A();
}
public Test(T1 foo)
{
_myFoo = foo;
}
}
public interface ITest
{
void A();
void B();
}
public class Impl
{
public void FooTest()
{
var parent = new Test<string, Test<int, ITest>>("tester");
var child = new Test<int, ITest>(1234);
parent.Child = child;
}
}
I would try something like this:
public class Test<T>
{
private readonly T _myFoo;
public Test(T foo)
{
_myFoo = foo;
}
}
public class ParentTest<T, TChild, TChildType> : Test<T> where TChild : Test<TChildType>
{
TChild Child { get; set; }
}
public class Impl
{
public void FooTest()
{
var parent = new ParentTest<string, Test<int>, int>("tester");
var child = new Test<int>(1234);
parent.Child = child;
}
}
This is correct way, with minimal modification of your code
public class Test<Foo,ChildFoo>
{
private readonly Foo _myFoo;
public Test<ChildFoo,ChildFoo> Child { get; set; }
public Test(Foo foo)
{
_myFoo = foo;
}
}
public class Impl
{
public void FooTest()
{
var parent = new Test<string,int>("tester");
var child = new Test<int,int>(1234);
parent.Child = child;
}
}

How can I pass value from one subclass to another subclass in a main class C#?

I create a class named MyMainClass
namespace MyTest
{
private void frmMain_Load(object sender, EventArgs e)
{
MyMainClass myVar = new MyMainClass();
myVar.sub1.sb1A = "value 1A";
myVar.sub1.sb1B = "value 1B";
MessageBox.Show(myVar.sub2.wantPassString);
//I want to print the value "I've got value value 1A"
}
public class MyMainClass
{
public subClass1 sub1 = new subClass1();
public subClass2 sub2 = new subClass2();
public class subClass1
{
public string sb1A{get;set;}
public string sb1B{get;set;}
}
public class subClass2
{
public string sb2A{get;set;}
public string sb2B{get;set;}
// a1a is a value that I want to get from subClass1
string a1a = subClass1.sb1A;
public string wantPassString {get{return "I've got value " + a1a;}}
}
}
}
How can I pass the value from subClass1.sb1A to the string a1a or wantPassString in subClass2 ? when I call in frmMain_Load
The normal approach would be to pass the class through the constructor. This works:
public class MyMainClass
{
public MyMainClass()
{
sub1 = new subClass1();
sub2 = new subClass2(sub1);
}
public subClass1 sub1;
public subClass2 sub2;
public class subClass1
{
public string sb1A{get;set;}
public string sb1B{get;set;}
}
public class subClass2
{
public subClass2(subClass1 sub1)
{
this.sub1 = sub1;
}
public string sb2A{get;set;}
public string sb2B{get;set;}
subClass1 sub1;
public string wantPassString {get{return "I've got value " + sub1.sb1A;}}
}
}
Create a third class to hold the data.
public MainClass
{
private class sharedClass
{
internal string sb1A { get; set; }
}
public class subClass1
{
private readonly sharedClass _shared;
internal subClass1(sharedClass shared)
{
this._shared = shared;
}
public string sb1A
{
get
{
return this._shared.sb1A;
}
set
{
this._shared.sb1A = value;
}
public string sb1B{get;set;}
}
public class subClass2
{
private readonly sharedClass _shared;
public subClass2(shared s)
{
_shared = s;
}
public string sb2A{get;set;}
public string sb2B{get;set;}
public string wantPassString {get{return "I've got value " + _shared.sb1;}}
}
private readonly sharedClass _shared = new sharedClass();
private readonly subClass1 _subClass1;
private readonly subClass2 _subClass2;
public MainClass()
{
this._subClass1 = new subClass1(this._shared);
this._subClass2 = new subClass2(this._shared);
}
}
You can add additional property and method and pass it as a parameter:
public string sb2C{ get; set; }
public string getData(string strParam)
{
return sb2C= strParam;
}
Then on your frmMain_Load:
MessageBox.Show(myVar.sub2.getData(myVar.sub1.sb1B));
//This would also populate sb2C that will hold the data from the other class

Access const with generics C#

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();
}

Override only Get accessor

I got an abstract class :
abstract class ClassBase
{
public abstract string Test { get; }
}
I want to derive it and by the way add a set accesor
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
set { _s = value; }
}
}
I can't do that because i may not override set
class ClassDerive2 : ClassBase
{
string _s;
public string Test
{
override get { return _s; }
set { _s = value; }
}
}
Syntax error
class ClassDerive3 : ClassBase
{
string _s;
public override string ClassBase.Test
{
get { return _s; }
}
public string Test
{
set { _s = value; }
}
}
Syntax error
Any Idea ???
thx
You cannot do exactly what you want to do but here is a workaround:
abstract class ClassBase
{
public abstract String Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
public override string Test
{
get { return _s; }
}
public void SetTest(String test)
{
this._s = test;
}
}
This will make Test only settable in ClassDerived via the public SetTest method. I know this is not as clean as using the property's setter but it is about as good as it's going to get.
If at first you have defined a read-only property in a type, you can't later change it to a read/write property in a derived class. That's simply how .NET works, and can't be changed.
If, on the other hand, you define an interface with a read-only property, you can later implement that interface in a class with a writable property.
If you'd like to share what you are trying to achieve, perhaps we can come up with a design that works and can compile :)
Another way:
abstract class ClassBase
{
public abstract string Test { get; }
}
class ClassDerive : ClassBase
{
string _s;
protected void setTest(string s)
{
_s = s;
}
public override string Test
{
get { return _s; }
}
}
class ClassDerive2 : ClassDerive
{
public new string Test
{
get { return base.Test; }
set { setTest(value); }
}
}
class Program
{
static void Main(string[] args)
{
var cd2 = new ClassDerive2();
cd2.Test = "asdf";
Console.WriteLine(cd2.Test);
}
}
My first thought was also to implement it as an interface. If this fits in with your design, the following code will work:
public interface TestInterface
{
string TestProperty { get; }
}
public class TestClass : TestInterface
{
public string TestProperty
{
get { return "test"; }
set { string t = value; }
}
}
No you cant, sorry. It is by design, so it's the law.

Categories