vendredi 28 décembre 2018

Problem with business and data access layer design

I am creating a library to interact with third party api.This library will be wrapper around that third party library and i want to expose my wrapper methods to client(webapi,winform,console,mvc etc..).

Below are the methods that i want to expose to my clients to perform operations using third party apis but i dont want to give them direct access.They will always use my wrapper api to perform operations.

public interface IMyLibraryWrapperApi
    {
             //methods expost to clients
         int AddRegion(RegionRequest request);
    }

    public class MyLibraryWrapperApi : IMyLibraryWrapperApi
    {   
        private readonly IThirdPartyUnitOfWork _thirdPartyUnitOfWork;
        private readonly IRegionServices _regionService;
        public MyLibraryWrapperApi(string domain, string username,string password)
        {
            this._thirdPartyUnitOfWork = new ThirdPartyUnitOfWork(domain, username,password);
            this._regionService = new RegionServices(_thirdPartyUnitOfWork);
        }
        public int AddRegion(RegionRequest request)
        {
           return _regionService.CreateRegion(RegionRequest request);
        }
    }

Service/Business Layer :

public class RegionService : IRegionService
    {
        private readonly IThirdPartyUnitOfWork _thirdPartyUnitOfWork;
        public RegionService(IThirdPartyUnitOfWork thirdPartyUnitOfWork)
        {
            this._thirdPartyUnitOfWork = thirdPartyUnitOfWork;
        }
        public void CreateRegion(RegionRequest request)
        {
            _thirdPartyUnitOfWork.Insert(request.Name,request.Direction);
        }
    }

DataAccess Layer :

public interface IThirdPartyUnitOfWork 
    {
        int Insert(string name,string direction);
        T GetById(int id);
    }

    public class ThirdPartyUnitOfWork : IThirdPartyUnitOfWork, IDisposable
    {
        private readonly ServiceContext _serviceContext;
        public ThirdPartyUnitOfWork(string domain, string username, string password)
        {
            _serviceContext = new ServiceContext(domain);
            _serviceContext.Credentials = new ThirdPartyCredentials(username, password);
        }
        //Insert method implementation
        //GetById method implementation
    }

Now I want that IMyLibraryWrapperApi should always interact with Service Layer and Service layer will interact with data access layer but here as you can see that IThirdPartyUnitOfWork is being exposed in IMyLibraryWrapperApi and even any client can call IThirdPartyUnitOfWork which i dont want.

But with current design I am not getting how to design this layer properly so that they do not leak in to other layers.

Can anybody please help me with some suggestions to improve this design

Aucun commentaire:

Enregistrer un commentaire