Using a DataContext in Each Function

I have a class that handles all database operations. I read that it is better to use multiple instances of a DataContext for different read / write / update / delete operations, as opposed to a single instance of a DataContext that has lived for a long time.

This means that every function that reads / writes / updates / deletes in the database must do this:

    public int GetSomeID(string name)
    {
        using (XXDataContext context = new XXDataContext(connStr))
        {
            ...
        }
    }

    public int GetAnotherID(string name)
    {
        using (XXDataContext context = new XXDataContext(connStr))
        {
            ...
        }
    }

    public void WriteSomething(string text)
    {
        using (XXDataContext context = new XXDataContext(connStr))
        {
            ...
        }
    }

against this use () only in the constructor and the presence of context as a private member variable available for each function call.

Keeping the functionality of creating a new DataContext each time the function is called, can this be moved using () to another place, so not every single function should have this line in it?

+4
4

, using:

private static void WithContext(Action<XXDataContext> action)
{
    using(XXDataContext context = new XXDataContext(connStr))
        action(context);
}    
private static T WithContext<T>(Func<XXDataContext, T> function)
{
    using(XXDataContext context = new XXDataContext(connStr))
        return function(context);
}

:

public int GetSomeID(string name)
{
    WithContext(context => 
        {
            //TODO use context
        });
}

.

+5

, :

. Unit Of Work .

, , :

using(var dal = new MyDalUOW()) {

   dal.Delete(s1);
   dal.Update(s2);
   dal.Get(s3);

   dal.Commit()
}

Dal, IDisposable Commit

public class BaseDal: IDisposable {
   private MyDbContext _context;

   public BaseDal() { _context = new MyDbContext; }

   public void Commit() { _context.SaveChanges(); }
}

_context .

, , , DAL, DAL.

0

desktop/windows, DataContext, ( db, EF linq2sql), datacontext . : .

WEB- (ASP NET), , datacontext ( ).

.

0

, :

using (var fileStream = new FileStream(@"C:\temp\test.txt", FileMode.Open))
{
    var bytes = new byte[fileStream.Length];
    fileStream.Read(bytes, 0, (int)fileStream.Length);

    var text = Encoding.Default.GetString(bytes);

    Console.WriteLine(text);
}

, IDisposable; DataContext. , , using; :

Read(fileStream =>
{
    var bytes = new byte[fileStream.Length];
    fileStream.Read(bytes, 0, (int)fileStream.Length);

    var text = Encoding.Default.GetString(bytes);

    Console.WriteLine(text);
}, @"C:\temp\test2.txt");

static void Read(Action<FileStream> action, string path)
{
    using (var fileStream = new FileStream(path, FileMode.Open))
    {
        action(fileStream);
    }
}

Here you enter the code you want to execute, ensuring that the statement usingis removed from the method. In addition, you can guarantee that the instruction usingwill be used. The output of this program will be as expected:

Hello, World!
Hello, World; from an injected function!
Press any key to continue . . .
0
source

Source: https://habr.com/ru/post/1538595/


All Articles