views:

86

answers:

2

I got this issue:

Product class in a SQLBackend that implements IProduct interface.

I also got a Product in SAPBackend (ERP, accounting software) that implements the same IProduct.

Both backend is in a different project.

I want to be able to use pass a Product between this two project so I need the same common interface.

I was thinking placing IProduct in a common interface project but if had many common cases would that lead quite many interfaces in that project. And if just lead to that I expose those interfaces to quite many projects

I wonder if there is a better case so SAPBackend and SQLBackend stand by them self and still share a common interface?

namespace Interfaces
{
    public interface IProduct
    {
        string name {set; get;}
    }

}
namespace Sqlbackend
{
    public class Product : IProduct
    {
        public string name { set; get; }
     }
}
namespace ERPbackend
{
    public class Product : IProduct
    {
        public string name { set; get; }
    }
}
+2  A: 

Types in .NET are scoped by their assembly. Unless the interface is in the same assembly, it is not the same type and won't be treated as such (cast will fail, etc).

So you will need a reference between projects here; most commonly, to a third dll that just has the common types and interfaces in it.

The exception to this is if you are doing contract-based SOA between the two layers (for example via WCF). But I don't think that this is what you intended.

Marc Gravell
+1  A: 

The IProduct in the SQL back-end has a different meaning from the one in the ERP back-end, so I would not let them share the same interface.

Implement the two back-ends separately, then pass data between them using a pre-defined format and using a class that understands the two. That class can read the ERP IProduct and write to the SQL IProduct.

namespace ERPBackend
{   interface IProduct
    {
     string Code { get; }
     string Description { get; }
    }

    class ProductForm
    {
     public IProduct CreateProductFromInput() 
     {
      ...

      return product;
     }
    }
}

namespace SQLBackend
{
    interface IProduct
    {
     string Id { get; }
     string Description { get; }
    }

    class ProductDB
    {
     public void SaveProduct(IProduct product)
     {
      ...
     }
    }
}

namespace MyApplication
{
    class ProductController
    {
     private ProductForm form;
     private ProductDB db;

     public ProductController(ProductForm form, ProductDB db)
     {
      this.form = form;
      this.db = db;
     }
     public void AddProduct()
     {  
      ERPBackend.IProduct product1 = form.CreateProductFromInput();
      SQLBackend.IProduct product2 = 
       new SQLBackend.MyProduct(product1.Code, product1.Description);
      db.SaveProduct(product2);
     }
    }
}
jeyoung
Code nicely formatted: http://pastebin.com/f4bfe4a8c
jeyoung