c#设计模式——为多个数据源设计的最佳方式

本文关键字:最佳 方式 设计模式 数据源 | 更新日期: 2023-09-27 18:05:59

我目前有一个ASP。. Net MVC 5应用程序,使用3个外部数据源(调用外部api,响应被反序列化,并映射到业务poco)。

该应用目前使用SimpleInjector将每个数据源的具体存储库注入业务逻辑层以供使用。

问题是,随着更多数据源的添加(可能是20-30),构造函数将会非常庞大,注入所有这些存储库似乎很麻烦。

是否有更好的模式/方法来使用所有的数据源而不是使用不同的存储库?

是facade还是其他模式更合适?

非常通用的例子:

    public class MyObject(){
         public IEnumerable<Cat> Cats { get; set; }
         public IEnumerable<Dog> Dogs { get; set; }
         public IEnumerable<Fish> Fish { get; set; }
    }
        public class BusinessLogic{
               private readonly ISourceARepository _sourceA;
               private readonly ISourceBRepository _sourceB;
               private readonly ISourceCRepository _sourceC;
            public BusinessLogic(ISourceARepository sourceA, ISourceBRepository sourceB, ISourceCRepository sourceC){
                    _sourceA = sourceA;
                    _sourceB = sourceB;
                    _sourceC = sourceC;
}
private Dog MapSourceARecordToDog(SourceARecord record){
    var result = new Dog();    
    if(record != null){
        result.Name = record.NameField;
        result.Age = record.Age;
    }     
    return result;
}
private Cat MapSourceBRecordToCat(SourceBRecord record){
    var result = new Cat();
    if(record != null){
         result.Name = record.NameField;
         result.Weight = record.WeightField;
    }
    return result;
}
private Fish MapSourceCRecordToFish(SourceCRecord record){
    var result = new Fish();
    if(record != null){
        result.ID = record.IDField;
        result.Name = record.NameField;
    }
    return result;
}
            public MyObject GetResults(){
                  var result = new MyObject();
                  result.Dogs = _sourceA.GetAll().Select(MapSourceARecordToDog).ToList();
                  result.Cats = _sourceB.GetAll().Select(MapSourceBRecordToCat).ToList();
                  result.Fish = _sourceC.GetAll().Select(MapSourceCRecordToFish).ToList();
                  return result;
            }
        }
        public class SourceARespository : ISourceARepository{
             public IEnumerable<SourceAResult> GetAll(){
                 return new List<SourceAResult>();
             }
         }
        public class SourceBRespository : ISourceBRepository{
             public IEnumerable<SourceBResult> GetAll(){
                 return new List<SourceBResult>();
             }
         }
        public class SourceCRespository : ISourceCRepository{
             public IEnumerable<SourceCResult> GetAll(){
                 return new List<SourceCResult>();
             }
         }

更新:这不是构造函数疯狂问题的重复,因为在这种情况下,类需要许多不同的数据源,但仍然只有一个职责。因此,它有自己的解释和答案。

c#设计模式——为多个数据源设计的最佳方式

您应该只向依赖的消费者中注入一个实体存储库。您还可以选择使用业务类中介来调整存储库。

:

根据问题和问题陈述中提供的信息,这里有一个可能的解决方案。像这样定义您的核心基础结构:

public abstract class Entity<TEntity, TDomainObject, TIRepository>
    where TEntity       : Entity<TEntity, TDomainObject, TIRepository>
    where TDomainObject : Entity<TEntity, TDomainObject, TIRepository>.BaseDomainObject, new()
    where TIRepository  : Entity<TEntity, TDomainObject, TIRepository>.IBaseRepository
{
    public class BaseDomainObject {}
    public interface IBaseRepository
    {
        IEnumerable<TDomainObject> GetAll();
        IEnumerable<T> GetAllMapped<T>(Func<TDomainObject, T> mapper);
    }
    public class BaseRepository : IBaseRepository
    {
        public IEnumerable<TDomainObject> GetAll()
        {
            return new List<TDomainObject>();
        }
        public IEnumerable<T> GetAllMapped<T>(Func<TDomainObject, T> mapper)
        {
            return this.GetAll().Select(mapper);
        }
    }
}

像这样定义你的源实体:

public class SourceA : Entity<SourceA, SourceA.DomainObject, SourceA.IRepository>
{
    public class DomainObject : BaseDomainObject
    {
        public  string  Name;
        public  int     Age;
    }
    public interface IRepository : IBaseRepository {}
    public class Repository : BaseRepository, IRepository {}
}
public class SourceB : Entity<SourceB, SourceB.DomainObject, SourceB.IRepository>
{
    public class DomainObject : BaseDomainObject
    {
        public  string  Name;
        public  decimal Weight;
    }
    public interface IRepository : IBaseRepository {}
    public class Repository : BaseRepository, IRepository {}
}
public class SourceC : Entity<SourceC, SourceC.DomainObject, SourceC.IRepository>
{
    public class DomainObject : BaseDomainObject
    {
        public  Guid    Id;
        public  string  Name;
    }
    public interface IRepository : IBaseRepository {}
    public class Repository : BaseRepository, IRepository {}
}

然后像这样定义一个ISourceRepositoryContext接口并在这里添加每个源存储库接口:

public interface ISourceRepositoryContext
{
    SourceA.IRepository SourceARepository { get; }
    SourceB.IRepository SourceBRepository { get; }
    SourceC.IRepository SourceCRepository { get; }
}

然后定义接口的默认实现:

public class DefaultSourceRepositoryContext : ISourceRepositoryContext
{
    public SourceA.IRepository SourceARepository => new SourceA.Repository();
    public SourceB.IRepository SourceBRepository => new SourceB.Repository();
    public SourceC.IRepository SourceCRepository => new SourceC.Repository();
}

定义结果传输对象:

public class Dog
{
    public  string  Name;
    public  int     Age;
}
public class Cat
{
    public  string  Name;
    public  decimal Weight;
}
public class Fish
{
    public  Guid    Id;
    public  string  Name;
}
public class MyObject
{
     public IEnumerable<Cat>  Cats { get; set; }
     public IEnumerable<Dog>  Dogs { get; set; }
     public IEnumerable<Fish> Fish { get; set; }
}

然后在您的BusinessLogic类中使用ISourceRepositoryContext:

public class BusinessLogic
{
    protected ISourceRepositoryContext repositories;
    public BusinessLogic(ISourceRepositoryContext repositories)
    {
        this.repositories = repositories;
    }
    public MyObject GetResults(string param1)
    {
        return new MyObject()
        {
            Dogs    = this.repositories.SourceARepository.GetAllMapped
            (domainObject=>new Dog
            {
                Age     = domainObject.Age,
                Name    = domainObject.Name
            }),
            Cats    = this.repositories.SourceBRepository.GetAllMapped
            (domainObject=>new Cat
            {
                Name    = domainObject.Name,
                Weight  = domainObject.Weight
            }),
            Fish    = this.repositories.SourceCRepository.GetAllMapped
            (domainObject=>new Fish
            {
                Id      = domainObject.Id,
                Name    = domainObject.Name
            }),
        };
    }
}

我已经确认上面的代码可以在c# 6.0下编译。

我建议将IRepository更改为实体中的IBusiness,并将数据访问关注点分离到只有IBusiness实现者通过其构造函数接收的IDataAccess接口中。然后将ISourceRepositoryContext更改为ISourceEntities,并将该接口中的IRepository属性更改为IBusiness属性。

BusinessLogic类是我真正关心的部分。你确定这门课不会给你带来太多麻烦吗?这应该是UoW课程吗?

关于基于类似技术的更完整的解决方案,请查看我对另一个问题的回答:.NET管理层关系