WCF库服务模式与实体框架 [英] WCF Repository Service pattern with entity framework

查看:128
本文介绍了WCF库服务模式与实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Silverlight中,asp.net等presentation层,所以一切都是通过WCF服务。
我在我的实现库层,服务层,WCF服务的

的疑虑数

我的事情要做当前


  1. 我的资料库,它不是每桌每次它聚合根创建

  2. 我有服务层的做一组涉及多个资源库动作

  3. WCF服务包在服务层和存储库层
  4. 方法
  5. 的实体是由EF自动生成

  6. 的实体传递和返回到服务层完全图

6.I有与所有库包括两个具体的类,和服务称为repositorycontainer和服务容器中,储存库的容器被传递到服务

我的仓库基地

 公共类RepositoryBase
    {
        公共DataBaseContext _Context;
        公共RepositoryContainer _RepositoryContainer;
        公共RepositoryBase(RepositoryContainer repositoryContainer)
        {
            _RepositoryContainer = repositoryContainer;
            _Context = repositoryContainer.Context;
        }
        公共RepositoryBase()
        {
            _RepositoryContainer =新RepositoryContainer();
            _Context = _RepositoryContainer.Context;
        }
    }

我的仓库容器

 公共类RepositoryContainer
    {
        公共RepositoryContainer()
        {
            上下文=新DataBaseContext();
        }
        公共RepositoryContainer(DataBaseContext上下文)
        {
            上下文=语境;
        }
 公共DataBaseContext上下文
    {
        得到;
        组;
    }
        公共SurveyRepository _SurveyRepository;
        公共SurveyRepository SurveyRepository
        {
            得到
            {
                返回_SurveyRepository? (_SurveyRepository =新SurveyRepository(本));
            }
        }
}

我的服务容器

 公共类的ServiceContainer
    {
        公众的ServiceContainer()
        {
            RepositoryContainer =新RepositoryContainer();
        }
        公众的ServiceContainer(RepositoryContainer容器)
        {
            RepositoryContainer =容器;
        }
        公共RepositoryContainer RepositoryContainer
        {
            得到;
            组;
        }
   公共SurveyService _SurveyService;
        公共SurveyService SurveyService
        {
            得到
            {
                返回_SurveyService? (_SurveyService =新SurveyService(本));
            }
        }
    }

要再做一次手术
我只是创建RepositoryContainer或的ServiceContainer

然后调用

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

我的疑惑是


  1. 是服务/程序存储库容器罚款?


  2. 我已经有服务层,这样我有WCF服务就是我所说的当前服务层servicewrapper什么?


  3. 我需要调用库方法本身例如:的getCategory()等,也是在服务层的所有方法,所以我需要包裹在WCF服务并重的方法和服务,是细


  4. 在哪里做缓存?因为我使用EF我觉得有什么办法使用与EF缓存提供商,



解决方案

  

是服务/程序存储库
  集装箱罚款?


RepositoryContainer 类包含一个SurveyRepository - 但不应该在SurveyRepository是RepositoryContainer的实例?同为的ServiceContainer 和SurveyService。它将使我更有意义,如果他们(虽然很难准确地评论而不较为熟悉的项目)。

您会再有:的ServiceContainer SurveyService =新的ServiceContainer(..);

当你拥有了它,我得到的IM pression说:SurveyService是具体的企业经营理念,但它包裹在一个更通用的类型(的ServiceContainer);同为SurveyRepository / RepositoryContainer。

这将打破 SRP ,的共同封闭原则并可能公共重用原则

我不知道还有什么其他认为,但我也有自己的类型后命名实例不是一个球迷(除最基本的senarios - 这这是不是):公共SurveyRepository SurveyRepository 的类型应该反映的类型是什么(或不会)这将是从它(如ServerContainer和ServeyService)。

特定实例quiote不同的名称

  

我已经有服务层,所以
  因为我有WCF服务我称之为
  当前服务层servicewrapper
  还是什么?



  

所以我需要改变我的服务的名称
  (BL)层的东西服务
  包装什么的,然后在WCF
  服务层我定义的方法
  资源库和服务,然后只是调用
  curresponding方法的服务,
  库


通常,任何可重复使用的BL应该是一个独立的包,而不是封闭的(想想硬codeD)在服务层或WCF服务,等等,那么你会创建服务端点那坐在顶部的BL。如果您有跨越的不同的包内的不同业务对象的商业交易,然后你需要把这些更高层次的业务流程更高的地方 - 我想这可能在服务层去,但是这不是繁琐的事情,你需要仔细考虑的地方一定的责任所在。

如果scover在同一封装内的不同业务对象的交易,然后编排更简单,可以用另一个BL型设计来处理这项工作,这将是该计划的一部分做 - 和不在服务层

关于命名 - 去白板和地图都出来,然后根据需要重新命名的一切。至少有一个有凝聚力的概述,你就可以把一切透明感。

BL包应该被命名为适合他们所做的事情 - 用商业术语。 WCF服务包裹这些应具有适合的名称,这可能包括参考信道的类型来使用(JSON,WebService的,等等)。因为你可以改变一个WCF服务通过配置使用信道(如果服务是设计正确),这可能不是一个好主意 -​​ 但假设它不会再额外清晰度可能会有所帮助。

这些文章可能会有所帮助:


  

我需要调用库方法
  本身如:的getCategory()等,也
  在服务层的所有方法,所以我
  需要包装两种方法和服务
  在WCF服务,它是罚款?


在包装纸的服务的服务听起来有点怀疑。只有外部呼叫者应该通过服务 - 假设服务旨在揭露BL给外部各方。内线主叫应该知道这是适当的方法调用(由于是内部的),presumably它是由服务公开的方法相同。


  

在哪里做缓存?如我一般
  使用EF我觉得有什么
  方式使用使用EF缓存提供商


我不知道你是否能在EF4缓存,但如果你可以把它不会让我感到吃惊。在哪里做缓存? - 这取决于那里的瓶子kneck是,你试图消除

在您RepositoryContainer中,_SurveyRepository领域是公共 - 它不应该是私有的?否则,为什么会有一个只读(得到)SurveyService财产?

 公共SurveyRepository _SurveyRepository;

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:

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;

这篇关于WCF库服务模式与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆