Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Class Factories

I have a class called Foo that has a function that looks like the following

List<Bar> LoadData();

Both Foo and Bar are in a library that I want to reuse in other projects. Now I am working on a new project and I want to subclass Bar. Let's call it NewBar.

What is a simple and flexible way to get Foo.LoadData to return a list of NewBar? I think that a factory is needed or perhaps just a delegate function. Can anyone provide an example?

Thanks, Andy

like image 826
Andy Avatar asked Mar 14 '11 22:03

Andy


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


4 Answers

Maybe even simplier, depends on what you need, but generic constraints may suffice if Bar is constructed with parameterless constructor:

public List<TBar> LoadData<TBar>()
    where TBar : Bar, new()
{
    // return a list populated with `new TBar()`;
}

Usage:

var newBars = Foo.LoadData<NewBar>();
like image 135
Snowbear Avatar answered Oct 23 '22 04:10

Snowbear


The easiest way would be to have an interface implemented by Bar and NewBar, and have your function return List<IBar> instead.

It's that or return object and cast galore.

like image 26
kprobst Avatar answered Oct 23 '22 03:10

kprobst


There are a few problems here. Since you are using List<Bar> this would not work if you return List<Baz> since List<> does not do covariance.

My suggestion is to redesign it to return IEnumerable<Bar> and make it virtual so that in the new project create a SubFoo and you can return IEnumerable<Baz>.


UPDATE

OK, according to the new information you provided (you use it for populating list from XML), I would create a virtual protected CreateBar() which creates a new bar object and is called by LoadData() to create new Bar in the loop. In the SubFoo I override and return Baz instead. In the SubFoo I will call base.LoadData() and populate the Baz list and then add the logic to populate new properties of Baz which are not in Bar.

Or perhaps

just use XML Serilization and I get all of that for free!

like image 23
Aliostad Avatar answered Oct 23 '22 03:10

Aliostad


Make LoadData() virtual. Then override it in a derived factory.

If the derived factory method would still return a list of Bar, but you could specify in the method implementation that all of the Bar instances are actually NewBar. This could be altered by using a generic method like in Snowbear's example.

like image 1
smartcaveman Avatar answered Oct 23 '22 03:10

smartcaveman