使用实体框架,依赖注入,工作单元和存储库模式在运行时注入/管理更改连接字符串

埃德加佩特拉乌斯卡斯

情况

我正在使用标题中提到的技术来构建Web应用程序。该应用程序将类似于用于多个客户端的CMS系统。客户端必须使用其公司名称和登录凭据登录到该系统。使用提供的公司名称,我连接到一个数据库(静态DbContext,每次都使用相同的连接字符串),该数据库存储了所有客户端数据库信息,并搜索此客户端特定的数据库(每个客户端都有自己的设计完全相同)登录信息。一切正常。现在是棘手的部分。要继续登录过程,我需要以某种方式使用另一个DbContext注入或延迟加载该存储库,并使用从另一个数据库的结果中建立的连接字符串。

我有的

从一个现有数据库生成的2个数据库上下文,一个静态,一个动态(如果可能)。

然后上课。通用存储库类/接口

 public interface IRepository
{
    void Submit();
}

public interface IRepository<TEntity, TContext> : IRepository
    where TEntity : class
    where TContext : DbContext
{
     //crud stuff
}
 public abstract class GenericRepository<TEntity, TContext> : IRepository<TEntity, TContext>
    where TEntity : class
    where TContext : DbContext
{
    private TContext _dataContext;
    private IUnitOfWork _unitOfWork;
    private readonly IDbSet<TEntity> dbset;

    protected GenericRepository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
        _unitOfWork.Register(this);
    }
}

工作单元类/界面

 public interface IUnitOfWork
{
    void Register(IRepository repository);
    void Commit();
}
 public class UnitOfWork : IUnitOfWork 
{
    private readonly Dictionary<string, IRepository> _repositories;
    private HttpContextBase _httpContext;


    public UnitOfWork(HttpContextBase httpContext)        
    {            
        _httpContext = httpContext;
    }

    public void Register(IRepository repository)
    {
        _repositories.Add(repository.GetType().Name, repository);
    }        

    public void Commit()
    {
        _repositories.ToList().ForEach(x => x.Value.Submit());
    }
}

然后是一个特定于上下文/实体的仓库

 public class EmployeeRepository : GenericRepository<tbl_Medewerker, CustomerDbEntities>, IEmployeeRepository
{
    public EmployeeRepository(IUnitOfWork unitOfWork)
        : base(unitOfWork)
    {
    }
}

public interface IEmployeeRepository : IRepository<tbl_Medewerker, CustomerDbEntities>
{

}

然后实现存储库的服务

 public interface IEmployeeLoginService
{
    tbl_Medewerker GetEmployeeByLogin(string username, string password);
    tbl_Medewerker GetEmployeeByID(Guid id);
}

public class EmployeeLoginService : IEmployeeLoginService
{
    private readonly IEmployeeRepository _employeeRepository;

    public EmployeeLoginService(IEmployeeRepository employeeRepository)
    {
        _employeeRepository = employeeRepository;
    }

    public tbl_Medewerker GetEmployeeByLogin(string username, string password)
    {
        return _employeeRepository.Get(e => e.MedewerkerNaam.ToLower() == username.ToLower() && e.Password == password);
    }

    public tbl_Medewerker GetEmployeeByID(Guid id)
    {
        return _employeeRepository.GetById(id);
    }
}

最后,实现该服务并在登录操作中使用它的控制器

 public class AccountController : BaseController
{
    IConnectionService _connectionService;
    IEmployeeLoginService _employeeService;

    public AccountController(IConnectionService connectionService, IEmployeeLoginService employeeService)
    {
        _connectionService = connectionService;
        _employeeService = employeeService;
    }



    [AllowAnonymous, HttpPost]
    public ActionResult Login(LoginModel login)
    {
        if ((Settings)Session["Settings"] == null)
        {
            Settings settings = new Settings();

            settings.company = _connectionService.GetCompanyName(login.CompanyName);
            if (settings.company != null)
            {
                settings.licence = _connectionService.GetLicenceByCompanyID(settings.company.Company_id);
                if (settings.licence != null)
                {
                    settings.connectionStringOrName = string.Format(@"Data Source={0};Initial Catalog={1};User ID={2};Password={3};Application Name=EntityFrameworkMUE", settings.licence.WS_DatabaseServer, settings.licence.WS_DatabaseName, settings.licence.WS_DatabaseUID, settings.licence.WS_DatabasePWD);                        
                    Session["Settings"] = settings;

                    settings.user = _employeeService.GetEmployeeByLogin(login.UserName, login.Password);
                    if (settings.user != null)
                    {
                        FormsAuthentication.SetAuthCookie(string.Format("{0},{1}", settings.company.Company_id.ToString(), settings.user.Medewerker_ID.ToString()) , login.RememberMe);
                        return RedirectToAction("index", "home");                            
                    }
                }
            }
        }
        else
        {
            return RedirectToAction("index", "home");
        }
        return View();
    }


}

当然还有autofac引导程序

private static void SetAutoFacContainer()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerHttpRequest();           
        builder.RegisterAssemblyTypes(typeof(UserRepository).Assembly)
            .Where(t => t.Name.EndsWith("Repository"))
            .AsImplementedInterfaces().InstancePerHttpRequest();
        builder.RegisterAssemblyTypes(typeof(ConnectionService).Assembly)
            .Where(t => t.Name.EndsWith("Service"))
            .AsImplementedInterfaces().InstancePerHttpRequest();

        builder.Register(c => new HttpContextWrapper(HttpContext.Current)).As<HttpContextBase>().InstancePerLifetimeScope();
        builder.RegisterModule(new AutofacWebTypesModule());

        builder.Register(att => new AuthorizeFilter(att.Resolve<IConnectionService>(), att.Resolve<IEmployeeLoginService>())).AsAuthorizationFilterFor<Controller>().InstancePerHttpRequest();
        builder.RegisterFilterProvider();

        IContainer container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

我的想法是如何在从存储数据的一个静态数据库中检索数据后,使用连接字符串设置一个会话变量,并在工作单元中注入会话,然后以某种方式在其中使用它,但是我无法解决问题它。

问题

我是否正在朝着正确的方向努力以实现这一目标,甚至可能?如果没有,您将采取什么步骤来实现这一目标

我知道它读得很长,希望你们能对我有所帮助,对同时使用这些技术来说还是很新的。在此先感谢,我非常感谢!

用户名

您在正确的轨道上,我已经习惯了

var mtc = new MultitenantContainer(container.Resolve<ITenantIdentificationStrategy>(), container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(mtc));

识别策略将基于已登录的用户。使用默认值(未登录时)。

public class CompanyNameIdentificationStrategy : ITenantIdentificationStrategy
    {

        public bool TryIdentifyTenant(out object tenantId)
        {
             var context = HttpContext.Current;
             if(context != null) 
             {
                var myUser = context.User as MyUserObject;
                if(myUser != null) 
                {
                    tenantId = myUser.CompanyName;
                    return true;
                }
             }
             return false; 
        }
}

然后,您将添加到自动资料设置:

 var s = c.Resolve<ITenantIdentificationStrategy>();
                    object id;
                    if (s.TryIdentifyTenant(out id) && id != null)
                    {
                        return id;
                    }
                    return "default"; 
                }).Keyed<string>("CompanyName");



builder.Register<Settings>(c =>
                {
                    var companyName = c.ResolveKeyed<string>("companyName");
                    if (companyName == "default")
                    {
                        return new DefaultSettings();
                    }
                    var settings = new Settings(); 
                    return settings;

                }).InstancePerLifetimeScope();

您可以解析这些代码块中的内容。我可能会设置一个键控默认设置,然后在用户登录时将设置切换为他们的设置,其余的应用程序也应正常工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用存储库模式,工作单元和统一性的实体框架

来自分类Dev

使用具有工作单元的Entity框架和通用存储库模式连接多个表

来自分类Dev

Castle和NLog在运行时更改连接字符串

来自分类Dev

DDD:存储库,工作单元,ORM和依赖项注入

来自分类Dev

实体框架-在运行时更改连接字符串(需要解释)

来自分类Dev

与实体框架一起使用而没有存储库模式的依赖注入

来自分类Dev

实体框架6,存储库模式和工作单元

来自分类Dev

如何构造函数注入仅在运行时已知的字符串?(温莎城堡)

来自分类Dev

在运行时使用连接字符串

来自分类Dev

在运行时更改注入的对象

来自分类Dev

使用Moq进行单元测试的通用工作单元和存储库模式框架

来自分类Dev

在Windows窗体中使用带有工作单元和存储库模式的简单注入器

来自分类Dev

使用存储库和工作单元模式进行多个DB事务的C#实体框架

来自分类Dev

Azure Web服务-在运行时更改数据库连接字符串

来自分类Dev

使用Moq进行单元测试的工作单元和通用存储库模式框架

来自分类Dev

使用Google Guice在运行时注入依赖项

来自分类Dev

实体框架核心,工作单元和存储库模式

来自分类Dev

使用连接字符串和精简程序时如何在运行时更改初始目录

来自分类Dev

使用依赖项注入在运行时确定实现

来自分类Dev

如何使用注入到存储库中的IServiceProvider来实现工作单元模式?

来自分类Dev

Net Core:使用Xunit对工作单元模式和DBContext执行依赖注入

来自分类Dev

使用存储库模式,工作单元和统一性的实体框架

来自分类Dev

Castle和NLog在运行时更改连接字符串

来自分类Dev

DDD:存储库,工作单元,ORM和依赖项注入

来自分类Dev

实体框架在运行时设置连接字符串

来自分类Dev

如何使用简单的注入器在运行时更改依赖项

来自分类Dev

存储库模式工作单元依赖注入注入

来自分类Dev

使用Google Guice在运行时注入依赖项

来自分类Dev

Dapper 在运行时更改连接字符串

Related 相关文章

  1. 1

    使用存储库模式,工作单元和统一性的实体框架

  2. 2

    使用具有工作单元的Entity框架和通用存储库模式连接多个表

  3. 3

    Castle和NLog在运行时更改连接字符串

  4. 4

    DDD:存储库,工作单元,ORM和依赖项注入

  5. 5

    实体框架-在运行时更改连接字符串(需要解释)

  6. 6

    与实体框架一起使用而没有存储库模式的依赖注入

  7. 7

    实体框架6,存储库模式和工作单元

  8. 8

    如何构造函数注入仅在运行时已知的字符串?(温莎城堡)

  9. 9

    在运行时使用连接字符串

  10. 10

    在运行时更改注入的对象

  11. 11

    使用Moq进行单元测试的通用工作单元和存储库模式框架

  12. 12

    在Windows窗体中使用带有工作单元和存储库模式的简单注入器

  13. 13

    使用存储库和工作单元模式进行多个DB事务的C#实体框架

  14. 14

    Azure Web服务-在运行时更改数据库连接字符串

  15. 15

    使用Moq进行单元测试的工作单元和通用存储库模式框架

  16. 16

    使用Google Guice在运行时注入依赖项

  17. 17

    实体框架核心,工作单元和存储库模式

  18. 18

    使用连接字符串和精简程序时如何在运行时更改初始目录

  19. 19

    使用依赖项注入在运行时确定实现

  20. 20

    如何使用注入到存储库中的IServiceProvider来实现工作单元模式?

  21. 21

    Net Core:使用Xunit对工作单元模式和DBContext执行依赖注入

  22. 22

    使用存储库模式,工作单元和统一性的实体框架

  23. 23

    Castle和NLog在运行时更改连接字符串

  24. 24

    DDD:存储库,工作单元,ORM和依赖项注入

  25. 25

    实体框架在运行时设置连接字符串

  26. 26

    如何使用简单的注入器在运行时更改依赖项

  27. 27

    存储库模式工作单元依赖注入注入

  28. 28

    使用Google Guice在运行时注入依赖项

  29. 29

    Dapper 在运行时更改连接字符串

热门标签

归档