0

We need following domain class model to be generated from the table listed below. Is it possible to achieve it using Linq 2 SQL. If it is not possible, will Entity Framework help? Can you please explain how to do it?

Note: The code for domain classes are available in How to Implement Repository FindAll() Method?. Mapping examples is also available there.

Note: I am trying to avoid the mapping between Linq 2 SQL generated entities and domain classes.

EDIT: "Linq to SQL, as an Object Relational Mapping technology, supports only the Table per Class Hierarchy strategy. This means that all levels in the inheritance hierarchy are stored in the same table, and a discriminator column tells what class a record represents."

CREATE TABLE [dbo].[BankAccount](
[BankAccountID] [int] NOT NULL,
[AccountType] [nchar](10) NOT NULL,
[OpenedDate] [datetime] NULL,
[Status] [nchar](10) NULL,
[AccountOwnerID] [int] NULL,
 CONSTRAINT [PK_BankAccount] PRIMARY KEY CLUSTERED 
(
[BankAccountID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

enter image description here

The factory is as listed below

public class MySimpleBankAccountFactory : IBankAccountFactory
{
    public DomainEntitiesForBank.IBankAccount CreateAccount(string accountType, int bankAccountID, string status, System.Nullable<System.DateTime> openedDate, System.Nullable<int> accountOwnerID)
    {
        DomainEntitiesForBank.IBankAccount acc = null;

        if (System.String.Equals(accountType, "Fixed"))
        {
            acc = new DomainEntitiesForBank.FixedBankAccount();
            acc.BankAccountID = bankAccountID;
            acc.AccountStatus = status;
        }

        if (System.String.Equals(accountType, "Savings"))
        {
            acc = new DomainEntitiesForBank.SavingsBankAccount();
            acc.BankAccountID = bankAccountID;
            acc.AccountStatus = status;
        }

        return acc;
    }
}

READING

  1. Entity classes decoupled from LINQ to SQL provider for implementing the Repository pattern. How?

  2. Use POCO LINQ to SQL Entities http://stephenwalther.com/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx

  3. Using LINQ to SQL XML Mapping Files http://weblogs.asp.net/dwahlin/archive/2008/08/18/using-linq-to-sql-xml-mapping-files-step-by-step.aspx

  4. How to: Create a Domain Service that uses POCO-defined Entities http://msdn.microsoft.com/en-us/library/gg602754(v=vs.91).aspx


Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • 1
    Do you mean that you want to use POCO classes? Entity framework supports this. See http://stackoverflow.com/questions/2478081/entity-framework-4-poco-where-to-start. You can script your poco classes in a separate project, you just need a scripting-time reference to the physical edmx file. – Maarten Jul 02 '12 at 07:55
  • @Maarten In LINQ to SQL we can use an XML mapping file. Still that mapping seems to be possible only with one class (1 table - 1 class). But it does not support polymorphism. I need to say what all will be the derived classes for an interface through a mechanism (lets say, xml mapping). Also I should be map each record with one of the derived classes based on data. How to achieve it? – LCJ Jul 02 '12 at 08:39

1 Answers1

1

I'm not sure if Linq-to-Sql supports mapping to interfaces (Entity framework does not but NHibernate does). If you replace your IBankAccount with abstract class BankAccount you should be able to simply map TPH inheritance in all mentioned technologies. Here is example for Linq-to-Sql.

Once you have mapped inheritance you can query either a base class or specific class (by using OfType in Linq-to-Sql or Entity Framework). When querying a base class ORM will internally goes through all inherited classes as well.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for the TPH (Table Per Hierarchy) hint... Again thanks for providing such a crystal clear reference ["Linq to SQL Inheritance" by Guy Burstein]. I will mark it as answered soon. – LCJ Jul 02 '12 at 08:59