3

I need presentation layer in silverlight, asp.net etc , so everything is through wcf services. I have number of doubts in my implementation of repository layer, service layer, wcf services

things i currently do

  1. I have repository , its not per table its created per aggregate root
  2. I have service layer its for doing a group of actions involving multiple repository
  3. WCF service wraps the methods in service layer and repository layer
  4. The entities are auto generated by EF
  5. The entities are passed and returned to service layer as complete graph

6.I have two concrete class with all repository , and service called repositorycontainer and service container , Repository container is passed to the service

My repository base

public class RepositoryBase
    {
        public DataBaseContext _Context;
        public RepositoryContainer _RepositoryContainer;
        public RepositoryBase(RepositoryContainer repositoryContainer)
        {
            _RepositoryContainer = repositoryContainer;
            _Context = repositoryContainer.Context;
        }
        public RepositoryBase()
        {
            _RepositoryContainer = new RepositoryContainer();
            _Context = _RepositoryContainer.Context;
        }


    }

My repository container

public class RepositoryContainer
    {
        public RepositoryContainer()
        {
            Context = new DataBaseContext();
        }
        public RepositoryContainer(DataBaseContext context)
        {
            Context = context;
        }
 public DataBaseContext Context
    {
        get;
        set;
    }


        public SurveyRepository _SurveyRepository;
        public SurveyRepository SurveyRepository
        {
            get
            {
                return _SurveyRepository ?? (_SurveyRepository = new SurveyRepository(this));
            }           
        }


}

My service container

 public class ServiceContainer
    {
        public ServiceContainer()
        {
            RepositoryContainer = new RepositoryContainer();
        }
        public ServiceContainer(RepositoryContainer container)
        {
            RepositoryContainer = container;
        }


        public RepositoryContainer RepositoryContainer
        {
            get;
            set;
        }
   public SurveyService _SurveyService;
        public SurveyService SurveyService 
        {
            get
            {
                return _SurveyService?? (_SurveyService= new SurveyService(this));
            }           
        }


    }

To do an operation I just create RepositoryContainer or ServiceContainer

then calls

RepositoryContainer.Repository.Method()
ServiceContainer.Service.Method()

My doubts are

  1. Is that service / respository container fine ?

  2. I already have the service layer, so as i have wcf service what i call the current service layer servicewrapper or something ?

  3. I need to call repository methods itself eg: GetCategory() etc , also all methods in service layer, So i need to wrap both methods and service in wcf service, is it fine ?

  4. Where to do the caching ? as i am using EF i think there is something way to use a cache provider with EF ,

Priyan R
  • 1,042
  • 1
  • 15
  • 24
  • - Clients can access individual WCF methods directly, or via the service layer (which "aggregates" one or more WCF methods into a larger / more complex "method" - is that right? - Caching: Do you have performance issues? If so - where? - What's the difference between a ServiceContainer and a RepositoryContainer? – Adrian K Jul 25 '10 at 02:08
  • Repositories do like GetCategories, Insert Delete etc, Operations / BL is done in service, for eg: I have user repository , Survey repository, When i update survey , i update a field in user(Logic) , So its are handled in surveyservice, where it calls user & survey repository methods , simply my repository doesn't conatin logic, all logic wrapped into service layer , Instead of creating each repository instances , it all wrapped into repository container, so i just need to create repository container (Creating an instance of it is not costly) , – Priyan R Jul 25 '10 at 04:43
  • Same way for services, When creating service container repository container is passed , its then passed to each service instances, so in each service there is no need to create each repository instances, Note: somhow i am not a fan of IoC. The problem is ,I like current things i do without WCF, means all residing in a single server , i need to seperate apllication server (BL) , If its all together i just reference the core dlls , create instance of repository container, service container call the methods, now i need WCF – Priyan R Jul 25 '10 at 04:43
  • i need to call repsotiory and service through WCF , So i need to change name of my service (BL) layer to something service wrapper or something , then in wcf service layer i define methods in repository and service then just calls curresponding methods in service, repository, i am confused with things i do, – Priyan R Jul 25 '10 at 04:43

1 Answers1

3

Is that service / respository container fine ?

The RepositoryContainer class contains a "SurveyRepository" - but shouldn't the SurveyRepository be an instance of a RepositoryContainer? Same for ServiceContainer and "SurveyService". It would make more sense to me if they were (although it's hard to comment accurately without being more familiar with the project).

You'd then have: ServiceContainer SurveyService = new ServiceContainer(..);

As you have it, I get the impression that "SurveyService" is a specific business concept but it's wrapped up in a more generic type (ServiceContainer); same for SurveyRepository / RepositoryContainer.

This will break SRP, Common Closure Principle and probably Common Reuse Principle.

I'm not sure what other think, but I'm also not a fan of naming instances after their types (except in the most basic of senarios - which this isn't): public SurveyRepository SurveyRepository The name of the type should reflect what the type is (or does) which will be quiote different from a specific instance of it (like ServerContainer and ServeyService).

I already have the service layer, so as i have wcf service what i call the current service layer servicewrapper or something ?

and

So i need to change name of my service (BL) layer to something service wrapper or something , then in wcf service layer i define methods in repository and service then just calls curresponding methods in service, repository

Generally any reusable BL should be in a standalone package and not enclosed (think "hard-coded") in a service layer or WCF service, etc. You'd then create service end-points that sat on top of the BL. If you have business transactions that span different business objects within different packages then you'll need to put that higher level orchestration somewhere higher - I guess this could go in the service layer, but this isn't a trival thing to do, you'll need to carefully consider where certain responsibilities lie.

If the transaction scover different business objects within the same package then the orchestration is much simpler and can be done with another BL type designed to handle that job, which will be part of that package - and not in the service layer.

Regarding the naming - go to a whiteboard and map everything out, and then rename everything as required. At least with a single cohesive overview you'll be able to make clear sense of everything.

BL packages should be named as appropriate to what they do - in business terms. WCF services that wrap these should have a name that fits and this could include reference to the type of channel being used (JSON, WebService, etc). Because you can change the channel a WCF service uses by config (if the service is design correctly) this might not be a good idea - but assuming it doesn't then the extra clarity might be helpful.

These articles might be of help:

I need to call repository methods itself eg: GetCategory() etc , also all methods in service layer, So i need to wrap both methods and service in wcf service, is it fine ?

Wrapping a service in a service sounds a bit suspect. Only external callers should go through the services - assuming the services are designed to expose the BL to external parties. Internal callers should know which is the appropriate method to call (by virtue of being internal), presumably it's the same method that is exposed by the service.

Where to do the caching ? as i am using EF i think there is something way to use a cache provider with EF

I don't know if you can cache in EF4 but it wouldn't surprise me if you can. Where to do caching? - it depends on where the bottle kneck is that you're trying to eliminate.

In your RepositoryContainer, the _SurveyRepository field is public - shouldn't it be private? Otherwise why have a read-only (get) SurveyService property?

public SurveyRepository _SurveyRepository;
Community
  • 1
  • 1
Adrian K
  • 9,880
  • 3
  • 33
  • 59