Can I create an object in one function and constructing in another? - c#

So I got an assignment [college student] to create a program that runs a garage. I have a class for every car type [FuelMotorcycle, ElectricMotorcycle, FuelCar, ElectricCar, etc.], each car type has its own constructor and they all differ from one another.
One of the assignment requirements is to "place the code that creates car objects [new], and this code alone, in a class on the logical part of the program, this code part cannot turn to the user directly or indirectly" (translated).
So the way I see it, I have a class, let's say "EmptyCarCreator" , that will have methods such as:
public static FuelMotorcycle CreateNewFuelMotorcycle()
{
FuelMotorcycle EmptyFuelMotorcycle;
return EmptyFuelMotorcycle;
}
obviously this won't compile, and even if it did, I couldn't use the "FuelMotorcycle" class constructor after I get it returned.
I need the user to input the elements for the constructor.
So, is there any other way to do this? I feel like I am missing something very basic here.
Please excuse any English errors, hope my question was clear.

You would need something like this:
public static class EmptyCarCreator
{
public static T Create<T>() where T : class, new()
{
return new T();
}
}
Then you would use it like this:
FuelMotorcycle myVehicle = EmptyCarCreator.Create<FuelMotorcycle>();
This will create a new class through the parameterless constructor.
There are other options that might be able to handle parameters a little better like this:
public static class EmptyCarCreator
{
public static object Create(Type type)
{
return Activator.CreateInstance(type);
}
}
To use this you would have to cast this returned object.
FuelMotorcycle myVehicle = (FuelMotorcycle)EmptyCarCreator.Create(typeof(FuelMotorcycle));

Related

Best approach to instantiate object based on string

I'd like to discuss about the best approach (in C#) to instantiate an object based on an input string. Let me explain.
Let'say I have a base class:
public abstract class BaseCar
{
public asbtract int GetEngineID();
//Other stuff...
}
Then I have several implementations of this class, let's say:
public class SportCar : BaseCar
{
public override int GetEngine()
{
//Specific implementation
}
}
public class OtherCar: BaseCar
{
public override int GetEngine()
{
//Specific implementation
}
}
And so on...
What I'd like to do is to make a static CarFactory class which has a CreateCar method which accepts a string as a parameter and returns a BaseCar instance, depending on what string you give. The string would be a name of a child class.
For example, if I call CarFactory.CreateCar('SportCar') it should return a SportCar instance.
I know I could use a simple switch statement to check which car has been requested and create a new instance based on that but I don't like this approach for two reasons:
I plan to have a lot of child classes, hard-coding every case wouldn't be too easy to mantain
I plan to implement an inizialization procedure to also give some initial values to the objects I create (using Reflection), so mixing hard-coding and reflection doesn't seem to be a good idea for me.
What I was thinking about is to use the Assembly.CreateInstance from System.Reflection to create an instance of the specified class but since this is the first time I approach this problem, I don't know if there are better ways to do that. Is this a valid approach ?
Considering the input string will come from an XML file, is there a simplier method ? Maybe my issue is already handled in some .NET Assembly which I'm missing.
Here is what I came up with. A generic factory class that automatically registers all types that are a subclass of the given type, and allows you to instantiate them via their name. This is somewhat related to the approach shown in the Java SO question linked by #Achilles in the comments, only that there is no initialisation function associated with the type.
There is no need to maintain an enum/switch combination of all types. It should also be somewhat easily extendable to handle your proposed reflection based initialisation.
static class StringFactory<T> where T : class
{
static private Dictionary<string, Type> s_dKnownTypes = new Dictionary<string, Type>();
static StringFactory()
{
RegisterAll();
}
static private void RegisterAll()
{
var baseType = typeof(T);
foreach (var domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in domainAssembly.GetTypes()
.Where(t => t.IsSubclassOf(baseType)))
{
s_dKnownTypes.Add(type.Name, type);
}
}
}
static public T Create(string _sTypeName)
{
Type knownType;
if (s_dKnownTypes.TryGetValue(_sTypeName, out knownType))
{
return (T)Activator.CreateInstance(knownType);
}
throw new KeyNotFoundException();
}
}
Assuming the classes of your question exist, you would instantiate a specific car like this:
var car = StringFactory<BaseCar>.Create("SportsCar");
DoSomethingWith(car.EngineID());
Since your question was for a discussion about the best approaches, please consider this only one of them. I have not used this in a production environment, and it is entirely possible that it is the wrong approach to your specific situation. It works well enough to show the general principle, however, and should provide a starting point for further discussion.

How to convert class with generic type to class with object as generic type in C#

Hello I'm using Visual Studio 2005 (because I need compact framework support) and my problem is with generics.
I have created an abstract class called AbstractDAO which is my base
From that I am creating other classes like DocumentDAO,HeaderDAO etc which represent different tables on my database
What I wish to do is retrieve a certain number of the above mentioned DAO classes, but as an AbstractDAO (the abstract class has a number of concrete implementations that I wish to use)
What I tried is
AbstractDAO<object> dao = new DocumentDAO();
AbstractDAO<object> dao = (AbstractDAO<object>)new DocumentDAO();
AbstractDAO<T> dao = new DocumentDAO();
I need the above because I have created a function that transfers data from one table to another similar table in a different database, so it would (if it worked) go something like this
AbstractDAO<object> dao_local = new DocumentDAO(local_database);
AbstractDAO<object> dao_remote = new DocumentDAO(remote_database);
do_transfer(dao_local,dao_remote)
void do_transfer(AbstractDAO<object> from, AbstractDAO<object> to) {
List<object> items = from.get_all();
to.insert(items);
}
Is there any way to do the above?
That will only work if your class hierachy is like this:
class DocumentDAO : AbstractDAO<object> {
//...
}
By your comment it seems like you have a type hierarchy like this:
class DocumentDAO : AbstractDAO<SomeType> {
public override SomeType Foo() {
//...
return someValue;
}
//...
}
class AbstractDAO<T> {
public abstract T Foo();
//...
}
You probably want to refactor AbstractDAO to implement a non generic interface like IAbstractDAO:
class IAbstractDAO {
object Foo();
//...
}
class AbstractDAO<T> {
public object Foo() {
return Foo();
}
public abstract T Foo();
//...
}
Any implementation of AbstractDAO<T> is compiled to a separate object type where T is replaced with the type. See "Is generics runtime or compile time polymorphism?" for more information on how this happens. In short, don't let the <T> fool you.
This means that you can't assign DocumentDAO to AbstractDAO<object> any more than you can assign String to it. Also a generic type is not the same as inheriting, which seems to be what you are trying to achieve.
In either case there are two standard solutions, as already mentioned.
The first is to operate on interfaces. You create an interface for the common properties and have AbstractDAO<T> or any other inherit from this. Then most of the time you simply operate on interfaces. Exactly how you organize it is up to you.
The second is to perform a shallow copy of the object. This means copying values and references from one object to the other. For this you usually use an object mapper like AutoMapper. This tutorial should get you started..
You can try to use Automapper to transfer your objects like this:
void do_transfer(AbstractDAO<FirstType> from, AbstractDAO<SecondType> to)
{
Mapper.Initialize(cfg=>cfg.CreateMap<FirstType, SecondType>);
List<FirstType> fromItems = from.get_all();
List<SecondType> itemsToInsert =
Mapper.Map<List<FirstType>, List<SecondType>>(fromItems);
to.insert(itemsToInsert);
}
By default automapper will map fields with same names. You can create configurations for complex type mapping.
So I finally found the answer to what I was trying to do, instead of assigning abstractDAO to something I created a factory that would retrieve the required AbstractDAO according to what type the generic was, and used this function
private bool transfer<T>(){
AbstractDAO<T> local = DAOFactory.get<T>(local_database);
AbstractDAO<T> remote = DAOFactory.get<T>(remote_database);
List<T> items = local.get_all();
foreach (T item in items) {
remote.insert(item);
}
}
That way I can call this function like this:
transfer< Document >();
transfer< Header >();
etc. and do a full transfer
edit: just for completeness' shake this is the factory I created
public static AbstractDAO<T> get<T>(Database database) {
Type t = typeof(T);
if (t == typeof(Document)) {
return new DocumentDAO(database) as AbstractDAO<T>;
} else if (t == typeof(Header)) {
return new HeaderDAO(database) as AbstractDAO<T>;
} etc.
}

C# how to avoid typecasting to subclass?

Let's say I have a base class called Shape. And then some sub classes such as circle and square.
Let's then create a method in another class called GetShape:
public Shape GetShape()
{
return new Circle();
}
Alright, so the idea is, that I can pass in a shapeType and then get a strongly typed Shape subclass returned. The above example is a massive simplification of real code, but I think it gets the point across.
So how when I call this method it would look like
var shapeCreator = new ShapeCreator();
Circle myCircle = shapeCreator.GetShape();
Only problem is it won't even run, since it requires a cast.
This would actually work:
Circle myCircle = (Circle) shapeCreator.GetShape();
I'm not wild about that cast, how can I avoid it and still accomplish a way to have a method return a baseclass so that I can return any compatible subclass.
You can use generics for this, even without reflection. This sample uses the parameterless constructor filter on T (sample altered from Adil):
public T GetShape<T>() where T : Shape, new()
{
return new T();
}
There is no way to solve your problem as presented. However, you say
so the idea is, that I can pass in a shapeType and then get a strongly
typed Shape subclass returned
Do you mean something like this:
var shape = shapecreator.GetShape(typeof(Circle));
or
var shape = shapecreator.GetShape<Circle>();
If so, and the type is statically known at compile time, just do
var circle = shapecreator.GetCircle();
If the type is not statically known, but can only be determined at runtime, you will need logic to decide what method to call, e.g. using if or switch statements. That being said, you need logic like that for a cast as well, so this isn't a real disadvantage.
Another option may be to use an abstract factory design pattern, where you have a ShapeFactoryBase class with a virtual Shape Create() method, and a derived class CircleFactory that inherits from it, and overrides the Create() method. You would still need the cast though, and you would still need logic to decide what type to cast to.
You can use generic methods to pass the type you want with the method call. In the method you can use Activator.Createinstance to create the object of type being passed.
GetShape Definition
public T GetShape<T>() where T : Shape
{
return (T)Activator.CreateInstance(typeof(T));
}
Calling of GetShape
Circle c = GetShape<Circle>();
Rectangle r = GetShape<Rectangle>();
Edit You can do it without reflection using new Constraint
Apply the new constraint to a type parameter when your generic class
creates new instances of the type, as shown in the following example:
public T GetShape<T>() where T : Shape, new()
{
return new T();
}
Instead of using a function, you could simply nest the shape classes in a GetShape static class and create the new shape from that static class as needed.
public static class GetShape {
public class Circle() { .. }
public class Square() { .. }
public class Triangle() { .. }
...
}
var NewShape = new GetShape.Circle();
However, I do recommend the generics exploit if you don't wish to do this.
If you want to do something Circle-specific you must cast to this particular type.
You could use dynamic objects but in such case you lose type safty checks on compilation stage.

How to pass object attributes as function parameters?

I am designing a 3 layer framework
I would like to know if It's possible to pass attribiutes of an object to a function without declaring them explicitly ?
For example If I want to pass Id,Name to personnelBL.ValidateInsert(...)
I don't want the ValidateInsert function interface look like this : ValidateInsert(Id,Name)
The reason for that is that I want to write a base abstract class to contain a ValidateInsert(...)
abstract function so I will Inherit from that class in my BL Layer classes and If the ValidateInsert input parameters could be declared in a way that I could pass an object attribiutes in a general form It would really be nice .
Note: Someone might say that I can pass an object to the function using generics but I really don't want to pass an object ! I want to pass any object's attribiutes so I can Inherit that abstract base class in any entityBL classes .
I really could not explain what I want better ! Sorry for that and thanks for understanding me .
not sure that I fully understand what you want , but I think the below can help
You can use reflection.You can avoid the performance issues, is you create method per class on the fly and compile it (can use compile expression tree). and add your own attribute that you put only on relevant attributes.
Create an Interface, It can return dictionary of column name and their values. your abstract class will implement this interface.
hope this answer your question
I am not sure if i understand your question correctly, but are you looking for something similar to this-
public class Base<T, TFiled>
{
public void ValidateInsert(TFiled filed)
{
}
}
public class Derived : Base<Derived, long>
{
public long Id { get; set; }
}
public class AnotherDerived : Base<Derived, string>
{
public string IdSring { get; set; }
}
public class MyObject
{
private Derived d = new Derived();
private AnotherDerived anotherIsntance = new AnotherDerived();
public MyObject()
{
d.ValidateInsert(10);
anotherIsntance.ValidateInsert("some string");
}
}
Well, not really.
But you can get very close to!
You can use the Expression API. It's awesome. The code I'll post here is just pseudocode but you'll get the idea. I'll not worry about syntax but I'll try the hardest I can.
public static bool ValidateInsert(params Expression<Func<object,object>>[] properties)
{
//Here you'll do some code to get every property. You can do a foreach loop.
//I think you will need to use reflection to get the property values
} //Change Func<Object,Object> accordingly. This represents a function that takes an object and returns another object.
This is how you can achieve the syntax, but I'm not sure about functionality.
You'll need an "instance" object where you'll get the properties values from.
So, you could call it like this:
ValidadeInsert(x => x.Id, x => x.Name, x => x.Whatever)
Here you can see how to get the Getter method of a property. I think you can get the PropertyInfo from the lambda expression, but I'm not sure. You'll have to do some research and adapt it to your code, if you decide to follow this way.
Sorry about my english, but I think you understood what I meant.

Get a List of Generic Types Applied to Any Class

I have a particular need which I cannot seem to figure out. I've done some research into this, but cannot find a feasible solution.
I have a base class:
public abstract class BaseProvider<T> {}
this class in turn is inherited by two different provider classes:
public sealed class MonkeyProvider<T>
: BaseProvider<MonkeyProvider<T>>
where T
: IAnimalProvider
Within in the interface IAnimalProvider I expose a single property that all implementations must derive off. Now, MonkeyProvider and maybe DonkeyProvider or something similar needs to know what the assigned value for the property the root instance is:
public class JoburgZoo
: IAnimalProvider
{
#region IAnimalProvider members
public string Id{ get; set; }
#endregion
}
// somewhere in a console application
public static void Main(string[] args)
{
JoburgZoo zoo = new JoburgZoo();
zoo.Id = "Mammals";
**// edit: an instance of the provider will be created**
MonkeyProvider<JoburgZoo> mp = new MonkeyProvider<JoburgZoo>();
mp.CheckMonkeys(zoo); // where CheckMonkeys(JoburgZoo) is a method in the provider
}
Now, here is my actual question:
I have the need to expose an internal property through BaseProvider which every instance that implement it, has access to. This property needs to return the value of "Id" at any given point, but I cannot seem to be able get the value through reflection (which I know is the solution to this).
From my various fruitless efforts:
Type type = typeof(T); // this returns BaseProvider<MonkeyProvider<T>>
var generic = type.GetGenericTypeDefinition(); // brings back BaseProvider<T>
I can't create a new instance of T as it will clear all values currently assigned to the object. I can't iterate the property info collection, as that will only return the properties of BaseProvider.
Thanks for any help on this.
Eric
// Edit.
Added an additional call in the console main code above. the instance of MonkeyProvider<T> templates JoburgZoo, so in ProviderBase<T> it will look something like:
ProviderBase<MonkeyProvider<JoburgZoo>>
I want to know what the properties of JoburgZoo is, from within the BaseProvider<T> without the need to identify the object withing MonkeyProvider<T>.
Hope this makes sense.
With following class definition,
class BaseProvider<T>
{
//...
}
following code returns System.Int32 :
Type type = typeof(BaseProvider<Int32>);
foreach (var arg in type.GetGenericArguments())
{
MessageBox.Show(arg.FullName);
}

Categories

Resources