B
Bryan Kyle
Hi All,
I'm fairly new to C# and Generics and I'm wondering if anyone has some
suggestions for me.
I'm trying to implement a simple DAO framework using generics to keep
my code as clean as I can, however I'm getting an error with what seems
to me to be correct code. The error I'm getting is:
Error 1 Cannot implicitly convert type 'Sample.PersonDao' to
'Sample.Dao<Sample.Model>' F:\Documents and Settings\Bryan\My
Documents\Visual Studio
2005\Projects\EventManager\EventManager\Sample.cs 22 24 EventManager
Below is a snippet of code that will reproduce compilation error I
receive.
namespace Sample
{
// Base class for all Model objects.
abstract class Model { }
// Base class for all Data Access Objects.
abstract class Dao<TModel> where TModel : Model { }
// Person model.
class Person : Model { }
// Data Access Object for a Person model.
class PersonDao : Dao<Person> { }
// Factory that returns the DAO for a model.
class DaoFactory
{
public Dao<Model> GetDao(Model model)
{
if (model is Person)
{
// Compilation error on the next line.
return new PersonDao();
}
return null;
}
}
}
Am I doing something completely wrong here? Is there something I
should be doing? Is there an alternate approach that might work
better?
Thanks in advance,
Bryan Kyle
I'm fairly new to C# and Generics and I'm wondering if anyone has some
suggestions for me.
I'm trying to implement a simple DAO framework using generics to keep
my code as clean as I can, however I'm getting an error with what seems
to me to be correct code. The error I'm getting is:
Error 1 Cannot implicitly convert type 'Sample.PersonDao' to
'Sample.Dao<Sample.Model>' F:\Documents and Settings\Bryan\My
Documents\Visual Studio
2005\Projects\EventManager\EventManager\Sample.cs 22 24 EventManager
Below is a snippet of code that will reproduce compilation error I
receive.
namespace Sample
{
// Base class for all Model objects.
abstract class Model { }
// Base class for all Data Access Objects.
abstract class Dao<TModel> where TModel : Model { }
// Person model.
class Person : Model { }
// Data Access Object for a Person model.
class PersonDao : Dao<Person> { }
// Factory that returns the DAO for a model.
class DaoFactory
{
public Dao<Model> GetDao(Model model)
{
if (model is Person)
{
// Compilation error on the next line.
return new PersonDao();
}
return null;
}
}
}
Am I doing something completely wrong here? Is there something I
should be doing? Is there an alternate approach that might work
better?
Thanks in advance,
Bryan Kyle