简体   繁体   English

定义界面

[英]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: 在我的Lite模型中,我只想暴露速度:

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 谢谢

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 创建一个具有主对象也具有的所有属性的Feed对象
  • 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; } 在feed的属性上添加注释,指定谁有权访问此属性,如[FeedSecurity("piet", "klaas")] string MyProperty { get;set; } [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. 使用一些反射和表达式树自动从业务对象填充您的Feed对象,并检查用户是否有权访问该属性,否则忽略它。

You could may be look at using multiple interfaces 你可以看看使用多个接口

public interface ICarBasic
{
    double Speed { get; set; }
}

public interface ICar : ICarBasic
{
    FuelType FuelType { get; set; } 
}

Or Write all your Basic object as base classes. 或者将所有Basic对象写为基类。 Inherit from them to make the full classes using a new assembly. 继承它们以使用新程序集创建完整的类。 Giving the developers out side the project only the assembly with the base classes in might solv your issue. 将开发人员放在项目的一边,只有具有基类的程序集可以解决您的问题。

Assembly 1 (For the other Devs) 组装1(对于其他开发者)

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

    }         
}    

Assembly 2 (Fully Featured) 大会2(全功能)

using ModelLite

namespace Model          
{          
    public class Car : ModelLite.Car         
    {          
        private FuelType m_fuelType;          

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

Only give assembly 1 to the other developers. 只将程序集1提供给其他开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM