[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

How do I define an abstract function that accepts an unknown object type as a parameter?

Hi Experts,

     Here's what I am trying to do:

Basically, something like this:

abstract class BusinessObjectServiceBase
{
          abstract void Save(SomeBusinessObjectType instanceOfProduct);
}

class Product_Service : BusinessObjectServiceBase
{
           public override Save(Product instanceOfProduct)
           {
           }
}

class Product
{
}

//Usage:
buton_Click()
{
         Product instanceOfProduct = new Product;
         Product_Service service = new Product_Service();
         service.Save(instanceOfProduct);

}

So my question is how do I have to change the above BusinessObjectServiceBase class to be able to use it with several different business objects so I want to be able to do this:

Product instanceOfProduct = new Product;
Product_Service service = new Product_Service();
service.Save(instanceOfProduct);

Customer instanceOfCustomer = new Customer;
Customer_Service service = new Customer_Service();
service.Save(instanceOfCustomer);

and I want Product_Service and Customer_Service to inherit from the same base class (BusinessObjectServiceBase)

So the main issue is that the different child classes are going to pass in different Types into the Save function of the Service class, so how do I define Save in the abstract Parent?  I could use an object type I guess, but can't this issue be solved with Generic types <T> somehow?  and if yes, how?  What would the parent and child look like?

Thanks!
0
axnst2
Asked:
axnst2
1 Solution
 
Craig WagnerSoftware ArchitectCommented:
It would probably be best if Customer and Product both inherited from a common base class or implemented the same interface. Then your abstract method could take an object of that common base type (or interface) and operate on it.
0
 
Miguel OzSoftware EngineerCommented:
If you only need to save the objects, you can use this code:
public static void SerializeToXml<T>(T obj, string fileName) 
{ 
    using (var fileStream = new FileStream(fileName, FileMode.Create)) 
    {  
        var ser = new XmlSerializer(typeof(T));  
        ser.Serialize(fileStream, obj); 
    } 
} 

Open in new window

For further info, check my answer:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26231877.html
The sample is saving the object as xml file, but you can save a binary if required.
0
 
käµfm³d 👽Commented:
I agree with CraigWagner's assessment  = )
0
 
käµfm³d 👽Commented:
Actually, after reviewing your requirement a bit further, I believe what you are asking for would be along these lines:

namespace _27866044
{
    class Program
    {
        static void Main(string[] args)
        {
            Product instanceOfProduct = new Product();
            Product_Service pService = new Product_Service();
            pService.Save(instanceOfProduct);

            Customer instanceOfCustomer = new Customer();
            Customer_Service cService = new Customer_Service();
            cService.Save(instanceOfCustomer);
        }
    }

    public class Product
    {

    }

    public class Customer
    {

    }

    public abstract class BusinessObjectServiceBase<T>
    {
        public abstract void Save(T instanceOfProduct);
    }

    public class Product_Service : BusinessObjectServiceBase<Product>
    {
        public override void Save(Product instanceOfProduct)
        {
            // do something to save the instance
        }
    }

    public class Customer_Service : BusinessObjectServiceBase<Customer>
    {
        public override void Save(Customer instanceOfProduct)
        {
            // do something to save the instance
        }
    }
}

Open in new window


...where the BusinessObjectServiceBase is generic, and which uses the generic type to define the Save method's signature. The derived classes will fill out their signatures based on the type passed for the generic placeholder.
0
 
axnst2Author Commented:
Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now