vendredi 6 janvier 2017

is it a good practice to pass an EF DbContext as delegate in services to attach all changes on it

Having the following context:

public class DemoContext : DbContext
{
    private readonly string _connectionStringName = "DemoContext";
    public DemoContext() : base("name=DemoContext")
    {
        //_connectionStringName = "DemoContext";
        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;
        //Database.SetInitializer(new MigrateDatabaseToLatestVersion<DemoContext, Migrations.Configuration>(_connectionStringName));
        Database.SetInitializer(new NullDatabaseInitializer<DemoContext>());
    }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Company> Companies { get; set; }
}

public class Employee
{
    [Key]
    public int EmployeeId { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class Company
{
    [Key]
    public int CompanyId { get; set; }
    public string CompanyName { get; set; }
}

And this services:

public class EmployeeSvc
{
    private readonly DemoContext _context;

    public EmployeeSvc(Func<DemoContext> context)
    {
        _context = context();
    }

    public void Add(EmployeeAgg employee)
    {
        _context.Employees.Attach(new Employee()
        {
            Name = employee.Name,
            DateOfBirth = employee.DateOfBirth
        });
    }

    public void UpdateAll()
    {
        var employees = _context.Employees.ToList();
        foreach (var employee in employees)
        {
            employee.Name = $"{Guid.NewGuid()}";
        }
    }
}

public class CompanySvc
{
    private readonly DemoContext _context;

    public CompanySvc(Func<DemoContext> context)
    {
        _context = context();
    }
    public void Add(CompanyAgg company)
    {
        _context.Companies.Attach(new Company()
        {
            CompanyName = company.CompanyName
        });
    }

    public void UpdateAll()
    {
        var empresas = _context.Companies.ToList();
        foreach (var empresa in empresas)
        {
            empresa.CompanyName = $"{Guid.NewGuid()}";
        }
    }
}

public class EmployeeAgg
{
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }
}

public class CompanyAgg
{
    public string CompanyName { get; set; }
}

Anf the following client code:

static void Main(string[] args)
    {
        var context = new DemoContext();
        // ReSharper disable AccessToDisposedClosure
        var employeeSvc = new EmployeeSvc(() => context);
        var companySvc = new CompanySvc(() => context);
        // ReSharper restore AccessToDisposedClosure
        Console.WriteLine("Adding entities to context inside services");
        employeeSvc.Add(new EmployeeAgg()
        {
            DateOfBirth = DateTime.Now.AddYears(-10),
            Name = $"Employee name"
        });
        companySvc.Add(new CompanyAgg()
        {
            CompanyName = $"Company name"
        });

        employeeSvc.UpdateAll();
        companySvc.UpdateAll();
        context.SaveChanges();            
        context.Dispose();
        Console.ReadLine();
    }

Is it a good practice passing DbContext to application services as delegate in order to have a reference to a single instance and track all changes on it?

Aucun commentaire:

Enregistrer un commentaire