locked
Designing n-tier architectecture RRS feed

  • Question

  • Hi All,

    I am designing N-tier architecture for my Project. I have taken the reference of following links:

    http://www.dotnetjohn.com/articles.aspx?articleid=260
    http://aspalliance.com/1215_Implementing_a_Generic_Data_Transfer_Object_in_C.all

    I am confused about how client can populate the data?

    because client has the object of interface and doesn't know instance of which class is being created, then how would set the properties of that class. he is having access to those properties which are declared in the interface and not in actual class. I want to keep the structure same but don't know how to populate the data from the client side.

    please help me on this.

    Regards,
    Prabi
    Thursday, March 26, 2009 5:49 AM

Answers

  • You can define an IKeyable and an ICustomer interface as below

    Public Interface IKeyable
        Property key() As Integer
    End Interface
    
    Public Interface ICustomer
        Property Description() As String
        Property Code() As String
    End Interface
    
    

    And then implement both these on your class;

    Public Class Customer
        Implements ICustomer, IKeyable
    
    ' Implement interface
    
    

    Or you can derive ICustomer from IKeyable;

    Public Interface IKeyable
        Property Key() As Integer
    End Interface
    
    Public Interface ICustomer : Inherits IKeyable
        Property Description() As String
        Property Code() As String
    End Interface
    
    Public Class Customer
            Implements ICustomer
    
    ' Implement interface
    
    

    Now the client can access the Key property through the IKeyable interface and the specific properties through the ICustomer interface.  You can return a business class as an IKeyable and the client just has to be able to cast it to the correct specific interface (ICustomer, ISupplier) etc.

    Hope this helps


    Pl mark as answer or helpful if you found this useful
    • Edited by G Moore Tuesday, April 7, 2009 8:40 AM removed white space
    • Marked as answer by Prabodh Sawant Tuesday, April 7, 2009 12:46 PM
    Tuesday, April 7, 2009 8:32 AM

All replies

  • Hi,

    If you want to create a concrete instance of your interface dynamically then you can use Unity Application Block to inject the instances. Refer to http://msdn.microsoft.com/en-us/library/cc468366.aspx for help on Unity.

    If you know statically which concrete classes to instantiate then you can do something like:
    ISampleInterface int = new ConcreteClassImplementation();

    ConcreteClassImplementation is the class that implements the ISampleInterface interface. The object int will give you all the properties and methods implemented in the concrete class.

    Hope this helps,

    --Pavan
    Thursday, March 26, 2009 11:19 AM
  • You can also use a product called StructureMap.  It is similar to the Unity product in that you can have interfaces and it will fetch an instance of your object for you based on the definition of that interface. 

    IObject obj = ObjectFactory.GetInstance<IObject>();

    I like this because I can control what StructureMap is returning behind the scenes either programatically or via a configuration file.  This makes for easy testing if that is your preference but is also nice in that I can define many different ways to do the same thing (think persisting to a db, a file system, etc.) and control it without actually having to change any of my code.  As long as everything conforms to the base interface!
    Friday, March 27, 2009 9:34 PM
  • Hi Pavan,

    I am having class clsCustomer which implements ICustomer interface.

    now structure of ICustomer is

    Interface ICustomer
    
      Property Key as Integer
    
    End Iterface

    Structure of clsCustomer is

    Public Class clsCustomer
        Implements ICustomer
    
        Private Code As String
        Private Desc As String
        Private ky As Integer
    
        Public Property Key() As Integer Implements ICountry.Key
            Get
                Return ky
            End Get
            Set(ByVal value As Integer)
                ky = value
            End Set
        End Property
    
        Public Property CustomerCode() As String
            Get
                Return Code
            End Get
            Set(ByVal value As String)
                Code = value
            End Set
        End Property
    
        Public Property CustomerDesc() As String
            Get
                Return Desc
            End Get
            Set(ByVal value As String)
                Desc = value
            End Set
        End Property
    
    End Class

    After this when I say,

    Dim iObj as ICustomer
    iobj = new clsCustomer

    then, iobj shows me only one property i.e. Key and now those properties from clsCustomer class.

    Why it is so?

    Regards,
    Prabi


    Monday, March 30, 2009 6:30 AM
  • Hi Andrew,

    Can u provide some example or link which describes about StructureMap?

    Thanks & Regards,
    Prabi
    Monday, March 30, 2009 6:32 AM
  • StructureMap and Repository layer: http://blog.andrewsiemer.com/archive/2008/02/05/linq-to-sql---implementing-the-repository-pattern.aspx
    Dynamically swapping objects: http://blog.andrewsiemer.com/archive/2008/03/03/structuremap-101---dynamically-swapping-objects.aspx
    Structure Map container idependant of ObjectFactory: http://blog.andrewsiemer.com/archive/2008/09/15/structuremap-users---great-article-using-the-structuremap-container-independently.aspx
    Andrew Siemer www.andrewsiemer.com blog.andrewsiemer.com www.socialnetworkingin.net
    Monday, March 30, 2009 9:11 PM
  • That's a simple one, this isn't anyting to do with inversion of control, or dependency injection, this is simply OO.

    You are accessing the class through the interface (as you should do)  The interface has only a Key property defined against it.

    If you want to see the other properties through the interface, you must define them on the interface, and I would suggest that you would want to do that, as every customer is going to have a description and code as well as a key.

    If you use the Key property in a few classes, you might want to define an IKeyable interface, and define the Key property against tht, then derive your ICustomer class from IKeyable interface.

    My suggestion would be to look into what Inversion of Control (IoC) and Dependency Injection mean first, before jumping into Unit or anything similar, if you don't understand fully what they are meant for you will find it difficult.

    In some cases something such as Unity isn't really necessary, you need to understand what you are trying to do before you choose the technology, so that you can make the right choice.

    Good luck!

    Martin.
    MCSD, MCTS. Please mark my post as helpful if you find the information good!
    Sunday, April 5, 2009 10:04 AM
  • Hi Martin,

    Thanks for your reply.

    The reason why I am creating instance of Customer class through the interface is that I don't want the client to know instance of which class he is creating. At the moment I have only one class that is Customer. Similarly I can have many business classes like Order, Invoice, Supplier etc. and they all can have Key property and will have their own properties specific to them as well.

    At the client side, client will create the instance of class through the Interface since he doesn't have the knowledge of Business classes. He knows only Interface. But my problem is I don't get the properties related to business class through the interface. Is it possible to do this by only sharing the interface with client? or is it must for the client to have the knowledge of business classes?

    If you could explain with some example that will help a lot.

    Thanks in advance.

    Regards,
    Prabi
    Tuesday, April 7, 2009 5:22 AM
  • http://codebetter.com/blogs/eric.wise/archive/2005/02/14/50919.aspx provides some basic information on how/why to use interfaces
    Tuesday, April 7, 2009 6:24 AM
  • You can define an IKeyable and an ICustomer interface as below

    Public Interface IKeyable
        Property key() As Integer
    End Interface
    
    Public Interface ICustomer
        Property Description() As String
        Property Code() As String
    End Interface
    
    

    And then implement both these on your class;

    Public Class Customer
        Implements ICustomer, IKeyable
    
    ' Implement interface
    
    

    Or you can derive ICustomer from IKeyable;

    Public Interface IKeyable
        Property Key() As Integer
    End Interface
    
    Public Interface ICustomer : Inherits IKeyable
        Property Description() As String
        Property Code() As String
    End Interface
    
    Public Class Customer
            Implements ICustomer
    
    ' Implement interface
    
    

    Now the client can access the Key property through the IKeyable interface and the specific properties through the ICustomer interface.  You can return a business class as an IKeyable and the client just has to be able to cast it to the correct specific interface (ICustomer, ISupplier) etc.

    Hope this helps


    Pl mark as answer or helpful if you found this useful
    • Edited by G Moore Tuesday, April 7, 2009 8:40 AM removed white space
    • Marked as answer by Prabodh Sawant Tuesday, April 7, 2009 12:46 PM
    Tuesday, April 7, 2009 8:32 AM