开发者

WCF Repository Service pattern with entity framework

开发者 https://www.devze.com 2023-01-08 16:46 出处:网络
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

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 ,


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:

  • http://blogs.oracle.com/christomkins/2009/07/my_thoughts_on_service_naming.html
  • http://ea.typepad.com/enterprise_abstraction/2006/08/service_naming_.html
  • What naming convention do you use for the service layer in a Spring MVC application?

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号