0

i need to write a library (dll) in c# to handle correctly multiple instances of DataContext.

This library must be written for a global using (web, console, etc..).

On a my old DB library, i create a type with private costructor, and a static property, for simplify my projects

An example:

public class DbClass
{
    private DbClass() { }

    private static DbClass instance;
    public static DbClass Instance
    {
        get
        {
            if (DbClass.instance == null)
                DbClass.instance = new DbClass();
            return DbClass.instance;
        }
    }
}

An example of use:

public void Update()
{
    DbClass.Instance.ExecuteSql("UPDATE Users SET IsUpdated = 'True'");
}

This class stored only a string (connectionString) into its static instance: no problems in web context, no problems in console context.

But i need to reuse this concept with linq-to-sql, by extending the DataContext type.

Any ideas?

EDIT:

By the first comment of Grant Thomas, i think my request is not explained good, and maybe my bad english not help..

I need to write a .Net Library (dll) to manage DB Data by a static DataContext Property:

this property must detect the current Context (web, windows MTA Thread, windows STA Thread)

and create/reuse instances of DataContext without throwing ThreadExceptions or others conflicts.

A sort of DataContextFactory...

Any comments/ideas was appreciated

4

2 回答 2

1

Well, If I gave you the best answer you might as well choose me...

Would not go there if I were you. Read http://www.west-wind.com/weblog/posts/2008/Feb/05/Linq-to-SQL-DataContext-Lifetime-Management

于 2013-04-06T19:13:13.207 回答
0

You can extend your DataContext easily enough by creating another file under the same namespace:

namespace MyDataContextNamespace {
  public partial class MyDataContext { 

  }
}

Then you can expose your singleton from there.

于 2013-03-30T22:51:19.470 回答