what is wrong with this using generic implicit operator?
-
16-04-2021 - |
Pergunta
if I use implicit operator in non generic builder class every thing is ok:
public class ReligionBuilder
{
private Religion _religion;
public ReligionBuilder()
{
_religion = new Religion(){//some codes}
}
public ReligionBuilder AddToRepository()
{
Repository<Religion>.Add(_religion);
return this;
}
public Religion Build()
{
return _religion;
}
public static implicit operator Religion(ReligionBuilder _builder)
{
return _builder.Build();
}
}
I can use it :
Religion religion=new ReligionBuilder().AddToRepository();
but if this operator is in generic class something is wrong:
public abstract class DataTestBuilderBase<T> : IDataTestBuilder<T>
{
protected T TestData { get; set; }
public virtual T Build()
{
return TestData;
}
public abstract IDataTestBuilder<T> AddToRepository();
public abstract IDataTestBuilder<T> WithDefault();
public static implicit operator T(DataTestBuilderBase<T> builder)
{
return builder.Build();
}
}
public class PersonDataTestBuilder : DataTestBuilderBase<Person>
{
private Person _person;
public PersonDataTestBuilder()
{
//some codes
}
public override IDataTestBuilder<Person> AddToRepository()
{
//some codes
return this;
}
}
usage:
PersonDataTestBuilder _testBuilder = new PersonDataTestBuilder();
Person person = _testBuilder.AddToRepository();
the error is :cannot convert IDataTestBuilder to Person
what is the problem?
Solução
AddToRepository
returns just IDataTestBuilder<Person>
(in terms of the compile-time return type) - and there's no implicit conversion from that to Person
. If you change the return type of the abstract method to DataTestBuilderBase<T>
then it should work - although frankly I wouldn't want to use that implicit conversion anyway. I'm generally pretty cautious about providing implicit conversions - they can often make the code less clear, as I believe they do here.
Should you really be providing AddToRepository
on a builder anyway? It feels like an inappropriate action for a builder - I would expect:
Person person = new PersonBuilder { /* properties */ }
.Build()
.AddToRepository();
EDIT: Just to show what I mean about changing the return type of AddToRepository
, here's a short but complete program to demonstrate. It works fine.
using System;
public abstract class BuilderBase<T>
{
public abstract T Build();
public abstract BuilderBase<T> AddToRepository();
public static implicit operator T(BuilderBase<T> builder)
{
return builder.Build();
}
}
public class TestBuilder : BuilderBase<string>
{
public override string Build()
{
return "Built by Build()";
}
public override BuilderBase<string> AddToRepository()
{
return this;
}
}
class Program
{
static void Main(string[] args)
{
string x = new TestBuilder().AddToRepository();
Console.WriteLine(x);
}
}