views:

350

answers:

4

I want to use my own Model classes in a repository pattern. I don't want to be dependent on the classes that LINQ to SQL generates. Is that viable? How do I handle the Where (and other selections when its a Func<MyModel, bool> but LINQ to SQL wants a Func<LinqToSqlModel, bool>?

I've devised this, but I might be starting to over engineer it...

interface IModelConverter<T1, T2>
{
  T2 Convert(T1 item);
  T1 Convert(T2 item);
}

Is this too much redirection?

All I want to do is have a repository of MyModel that is able to have any implementation in the back end whether LINQ to SQL, LINQ to Enities, etc. Does anyone have any resources?

+2  A: 

I think you're creating a lot of overhead here. All the classes that Linq to Sql generates are partial, so if you set up your namespaces right, you can just extend those classes with whatever information you want. And if there's anything you want to hide, set its access level to internal in the dbml.

I really wouldn't recommend rewriting the classes altogether.

David Hedlund
A: 

I don't really understand why you need to implement everything from scratch. But if you want to have more controls on the repository, you can simply use partial class to achieve that.

xandy
A: 

If you want your model to replace the linq to sql model, you can decorate your model with attributes, which allows you to basically define your own model, and tell linq to sql how to wire your model with the datacontext (which you have to build as well)

Example:

[Table(Name="SomeTable")]
public class SomeClass
{
    [Column(IsPrimaryKey=true, IsDbGenerated=true, AutoSync=AutoSync.OnInsert)]
    internal int MyID {get; set;}

    [Column]
    public String MyString {get; set;}
}

You can then create a DataContext and tell it how to handle your model like so:

var context = new DataContext(connectionString);
context.GetTable<SomeClass>();
Joseph
I don't want my model to be coupled that close to linq to sql.
Daniel A. White
A: 

I decided to switch to Fluent NHibernate.

Daniel A. White