Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defining interface

I have a datamodel used by multiple application that I now needs to be used by other developers outside the team. The model should only be made partialy available to the developers.

I wonder how I best approach this: my current approach is to create a new project that just copies the orginal model and only include the requested properties.

for example

namespace Model
{
    public class Car
    {
        private double m_speed;
        private FuelType m_fuelType;

        public double Speed
        {
            get { return m_speed; }
            set { m_speed = value; }
        }       


        public FuelType FuelType
        {
            get { return m_fuelType; }
            set { m_fuelType = value; }
        }
    }
}

In my Lite Model I only want to expose the speed:

using Model;

namespace ModelLite
{
    public class Car
    {
        private Model.Car car = new Model.Car();

        public double Speed
        {
            get { return this.car.Speed; }
            set { this.car.Speed = value; }
        }

    }
}

Since the model is big this involves in a lot of duplication. Maybe there is a better alternative?

Thanks

like image 287
Tarscher Avatar asked Jan 18 '10 10:01

Tarscher


People also ask

How do you define an interface?

An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.

What is defining interface in Java?

An interface in the Java programming language is an abstract type that is used to describe a behavior that classes must implement. They are similar to protocols.

What is interface define with example?

1. The definition of interface is a surface that forms a common boundary between two things or a point of interaction between two components or systems. An example of interface is someone using the controls on a washing machine to tell the machine how to function. noun.

Why do we define interface?

You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: Capturing similarities among unrelated classes without artificially forcing a class relationship.


2 Answers

Take a look at the work by Martin Fowler on Application Facades and the Facade Pattern

like image 158
Stephan Eggermont Avatar answered Oct 02 '22 15:10

Stephan Eggermont


There is no solution to this problem. If different devs are only allowed to have partial access to fields, you will need to create different feeds for different devs.


Although your model just seems wrong to me, you might however accomplish this by:

  • Creating one feed object that has all the properties the main object also has
  • Create some attribute like:

 class FeedSecurityAttribute : Attribute  
 {   
       public FeedSecurityAttribute(params string[] rights) {}  
 }

  • Add annotations on the properties of the feed specifying who has access to this property like [FeedSecurity("piet", "klaas")] string MyProperty { get;set; }
  • Fill your feed object from a business object automatically using some reflection and expression trees, and check whether the user has access to the property, otherwise ignore it.
like image 34
Jan Jongboom Avatar answered Oct 02 '22 17:10

Jan Jongboom