Thursday, May 5, 2011

c# (wcf) architecture file and directory structure (and instantiation)

Hello and thanks for any assistance.

I have a wcf service that I'm trying to properly modularize.

I'm interested in finding out if there is a better way or implementing the file and directory structure along with instanciatation, is there a more appropriate way of abstraction that I may be missing?

Is this the best approach? especially if performance and the ability to handle thousands of simultanious request?

Currently I have this following structure:

-Root\Service.cs

public class Service : IService
{
    public void CreateCustomer(Customer customer)
    {
        CustomerService customerService = new CustomerService();
        customerService.Create(customer);
    }

public void UpdateCustomer(Customer customer)
{
     CustomerService customerService = new CustomerService(); 
     customerService.Update(customer); 
}

}

-Root\Customer\CustomerService.cs

pulbic class CustomerService
{
    public void Create(Customer customer)
    {
        //DO SOMETHING
     }

public void Update(Customer customer)
{
    //DO SOMETHING
}

public void Delete(int customerId)
{
    //DO SOMETHING
}

public Customer Retrieve(int customerId)
{
    //DO SOMETHING
}

}

Note: I do not include the Customer Object or the DataAccess libraries in this example as I am only concerned about the service.

If you could either let me know what you think, if you know a better way, or a resource that could help out.

Thanks Kindly. Steven

From stackoverflow
  • I used web services software factory and liked its structure.

    The structure in their sample code was something like this:

    BYA.Mfg.SCM.Svc.WCF
      Source
        Business Logic
          BYA.Mfg.SCM.Svc.WCF.BusinessEntities
          BYA.Mfg.SCM.Svc.WCF.BusinessLogic
        Resource Access
          BYA.Mfg.SCM.Svc.WCF.DataAccess
        Service Interface
          BYA.Mfg.SCM.Svc.WCF.DataContracts
          BYA.Mfg.SCM.Svc.WCF.FaultContracts
          BYA.Mfg.SCM.Svc.WCF.MessageContracts
          BYA.Mfg.SCM.Svc.WCF.ServiceContracts
          BYA.Mfg.SCM.Svc.WCF.ServiceImplementation
      Tests
    
  • This may be unanswer for a number of reasons, but I'd guess the broadness of the question would have a lot to do with it. Also, its a little challenging to analyze designs with little context. There are also a lot of questions here, making an answer complicated.

    First, you mentioned several classes that are not in your example code. I'll assume that Customer refers to CustomerService and likewise for the other mentioned classes and examples?

    You asked if there was a better way to instatiate. You may want to look up Design Patterns like the "FlyWeight" pattern and the "Factory" pattern to help you in this. I mention "FlyWeight" because you talked about performance.

    The Design Patterns book will help you. Also, Refactoring by Martin Fowler (although written in Java) will help a great deal.

    stevenrosscampbell : fooMonster. Thanks for you help, you were correct, I was providing to much information in the example. I have editted the question, if you get another chance to look it over i'd appreciate it. Thanks, Steven
  • I don't know that this is the best (or even recommended) directory structure, but it's what I've settled on for now.

    .\MyProject
    |----\bin
    |----\MyProject                                 (main application)
    |----\MyProject.Core                            (shared libraries)
    |----\MyProject.Server                          (WCF-hosting Windows service)
    |----\MyProject.Services                        (the WCF services)
    |---------\MyProject.Services.Service1          (WCF Service1)
    |---------\MyProject.Services.Service1.Support  (WCF Service1-specific libraries)
    |---------\MyProject.Services.Service2          (WCF Service2)
    |---------\MyProject.Services.Service2.Support  (WCF Service2-specific libraries)
    

    Based on this directory structure and what you've shown so far, the Service class would go in a MyProject.Services.Service folder under the MyProject.Services directory. The CustomerService class would go in a MyProject.Services.Service.Support folder under the MyProject.Services directory.

    I've not worked with databases enough to understand the trade-offs between opening multiple simultaneous connections or simply reusing the same connection over and over. My guess is that the way you are doing it is the preferred solution.

    And given that you defer your processing to the database each time your WCF service is called (by creating a new CustomerService object), you might benefit from making your WCF service a singleton. In general, WCF singletons are frowned upon for scalability reasons because the assumption is that they share state between various service calls and thus have to be synchronized. However, as shown, your WCF service does not maintain any state directly. It simply accesses the database to create, update, delete, or fetch the Customer. As long as the database access using the appropriate locking scheme, you can avoid the overhead associated with per-call instantiations of your WCF service. To make your service a singleton, use the following attribute on your service:

    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    public class Service : IService
    {
    }
    

0 comments:

Post a Comment