0

I've got a program with connections to 4 databases. In three of these databases the entity objects are very similar. Now my question is quite simple but I can't wrap my head around how to proceed.

I have three databases let's call them 1 2 and 3 in those I got Several Tables a b and c

I'm asking since 1a and 2a and 3a is almost the same is there a way for me to do something like this. ?

Using(interfaceDB DB = new DB1())
{
   var getTabelA = (from a in DB.a select a);
}
Using(interface DB = new DB2())
{
   var getTabe2A = (from a in DB.a select a);
}
Using(interface DB = new DB3())
{
   var getTabe3A = (from a in DB.a select a);
}
foreach(interfaceDBTableA in getTabelA)
{
   //do something here
}
foreach(interfaceDBTableA in getTabe2A )
{
   //do something here
}
foreach(interfaceDBTableA in getTabe3A )
{
   //do something here
}

Basically my hope is that I could then just put the loop part in to it's own method and reuse it without the need to customize it to the individual table ?

Miika L.
  • 3,333
  • 1
  • 24
  • 35
Helbo
  • 473
  • 10
  • 32
  • Are the tables similar, or are they the same? In particular, is the data you need from them exactly the same in each database, or does it vary? If you need different columns from each database, or the data types and structure varies slightly, then you can't really use interfaces for this task. – Miika L. Apr 20 '12 at 09:41
  • the tables vary slightly however in this case I need the same columns on all of them – Helbo Apr 20 '12 at 10:15

1 Answers1

0

You can define an interface containing the members which your objects have in common, like this:

public interface ICommonStuff
{
    int CommonInt
    {
        get;
    }

    string CommonString
    {
        get;
    }
}

...then implement that interface in classes A, B and C:

public class A : ICommonStuff
{
    public int AInt { get; set; }

    public string AString { get; set; }

    public int CommonInt
    {
        get { return this.AInt; }
    }

    public string CommonString
    {
        get { return this.AString; }
    }
}

public class B : ICommonStuff
{
    public int BInt { get; set; }

    public string BString { get; set; }

    public int CommonInt
    {
        get { return this.BInt; }
    }

    public string CommonString
    {
        get { return this.BString; }
    }
}

... (same for C)

Then you should be able to select them like this:

using (interfaceDB DB = new DB1())
{
    var getTable1A = (from a in DB.a select a).Cast<ICommonStuff>();
}

using (interface DB = new DB2())
{
    var getTable2A = (from a in DB.a select a).Cast<ICommonStuff>();
}

using (interface DB = new DB3())
{
    var getTable3A = (from a in DB.a select a).Cast<ICommonStuff>();
}

var everythingLikeA = getTable1A.Concat(getTable2A).Concat(getTable3A);

foreach (ICommonStuff commonStuff in everythingLikeA)
{
    // do something here with commonStuff.CommonInt and commonStuff.CommonString
}
Steve Wilkes
  • 7,085
  • 3
  • 29
  • 32
  • this was my first idea but since the classes A,B and C are entities they are already implemented and I'm pretty sure that if I modify them then they will eventually overwrite them self therby deleting my modification – Helbo Apr 20 '12 at 10:51
  • "Already implemented" by what? Your concern over the overwriting indicates it's a T4 template; is it the Entity Framework? If the existing implementations are in partial classes you can write separate partials which implement the interface. – Steve Wilkes Apr 20 '12 at 11:06
  • yes it's the entity framework hmm I didn't know that might be usefull will look at it now – Helbo Apr 20 '12 at 11:22
  • I've been Trying to make this idea work but it seems to me that it's not a viable solution I keep running in to obstacles that makes it much to difficult to go down this road compared to having a lot of similar code. I just hate that I can't easily make 99% similar code reusable :( – Helbo Apr 23 '12 at 07:23
  • I'm gonna set you as the right answer because part of it is what I've been using just instead of using it on the database I've been using it on the individual tables. – Helbo Apr 23 '12 at 08:20
  • If you'd like to upload the code to something like [Bitbucket](https://bitbucket.org/) I'd be happy to take a look at it to see if I can help :) – Steve Wilkes Apr 23 '12 at 08:32