Reputation: 22652
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]
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
Entity classes decoupled from LINQ to SQL provider for implementing the Repository pattern. How?
Use POCO LINQ to SQL Entities http://stephenwalther.com/archive/2008/07/22/asp-net-tip-23-use-poco-linq-to-sql-entities.aspx
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
How to: Create a Domain Service that uses POCO-defined Entities http://msdn.microsoft.com/en-us/library/gg602754(v=vs.91).aspx
Upvotes: 0
Views: 343
Reputation: 364259
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.
Upvotes: 1