简体   繁体   English

C#OOPs体系结构问题

[英]C# OOPs architecture problem

I am facing a class resolution issue while trying to make my architecture flexible. 我在尝试使我的体系结构灵活时遇到类解析问题。 To make it simple, consider the following example: 为简单起见,请考虑以下示例:

I have four tiers: UI, BL, Common and DAL 我有四个层次:UI,BL,Common和DAL

I have a base class in my BL layer as defined below: 我的BL层中有一个基类,如下所示:

public class User
    {
        private UserDTO _d;
        public User()
        {
            _d = new UserDTO();
        }
        public User(UserDTO d)
        {
            _d = new UserDTO(d);
        }
        public UserDTO D
        {
            get { return _d; }
            set { _d = value; }
        }

        //static method (I cannot make it non-static due to some reasons)
        public static User GetUser()
        {
            User user = new User(Provider.DAL.GetUser());
            return user;            
        }
    }

The DTO is defined as: DTO定义为:

public class UserDTO
    {
        public int ID;
        public UserDTO()
        {
        }

        public UserDTO(UserDTO source)
        {
            ID = source.ID;
        }
    }

My DAL is defined as (it returns a DTO not a business object): 我的DAL定义为(它返回DTO而不是业务对象):

 public static UserDTO GetUser()
        {
            UserDTO dto = new UserDTO();
            dto.ID = 99;
            return dto;
        }

Now, I want to "extend" my code so that I can have one more field in my User table: Name. 现在,我想“扩展”我的代码,以便我的User表中可以再有一个字段:Name。 So I create a derived DTO class as: 因此,我将派生的DTO类创建为:

 public class MyUserDTO : UserDTO
    {
    public string Name;
    public MyUserDTO()
    {
    }

    public MyUserDTO(MyUserDTO source)
    {
        Name = source.Name; //new field
        base.ID = source.ID;
    }
}

Then, I create a derived User class as: 然后,我将派生的User类创建为:

 public class MyUser : User
    {
        public MyUser()
        {
            this.D = new MyUserDTO(); 
        }
    }

And I create my own custom DAL provider with this method: 然后使用此方法创建自己的自定义DAL提供程序:

 public static UserDTO GetUser()
        {
            UserDTO dto = new MyUserDTO();
            dto.ID = 99;
            ((MyUserDTO)dto).Name = "New Provider Name";
            return dto;
        }

Now when I access this MyUserDTO object in my BL, it loses resolution: 现在,当我在BL中访问此MyUserDTO对象时,它将失去分辨率:

User.GetUser(DAL.Provider.GetUser()) User.GetUser(DAL.Provider.GetUser())

and in the UI, I dont get the properties in the MyUserDTO. 在用户界面中,我没有在MyUserDTO中获得属性。

Is there a method which can help me get those properties in the UI layer, even after I call the static User.GetUser() method (which will in turn call my custom provider returning a MyUserDTO object)? 有没有一种方法可以帮助我在UI层中获得这些属性,即使我调用了静态User.GetUser()方法之后(该方法又将调用我的自定义提供程序返回MyUserDTO对象)?

Thanks, 谢谢,

You don't inherit from a class to add in new data, only inherit if your new class is going to extend the behaviour of your old class. 您不会从类继承来添加新数据,仅当新类将扩展旧类的行为时才继承。

Give your UserDTO a Name-Value collection to hold its data and populate that. 给您的UserDTO一个Name-Value集合来保存其数据并填充它。 Then you won't have an issue. 这样您就不会有问题了。

You're not getting the derived behaviour because the static method in the user class can't be overriden by the MyUser class. 您没有得到派生的行为,因为MyUser类无法覆盖用户类中的静态方法。

You should remove the static method (I know you've indicated you can't), but really you should. 您应该删除静态方法(我知道您已经表明不能这样做),但实际上应该删除。

Try to extract the creation of your users in another object (factory, repository,...) 尝试将用户的创建内容提取到另一个对象(工厂,存储库等)中

As others have mentioned, the problem is with the static method. 正如其他人提到的那样,问题在于静态方法。

I would also suggest using generics or dependency injection to clean up your parallel object hierarchy. 我还建议使用泛型或依赖项注入来清理并行对象层次结构。 You don't want that duplication. 您不希望重复。

Instead of having a set of almost-identical 'User' and 'MyUser' classes, that have the same hierarchy, just do something like User and plug in the UserDTO type that you want. 不必拥有一组具有相同层次结构的几乎相同的'User'和'MyUser'类,而是执行类似User的操作并插入所需的UserDTO类型。

If you refactor and remove the object hierarchy duplication, it may make it easier to see a good solution to get around the problem with the static GetUser(). 如果重构并删除对象层次结构重复项,则可以更轻松地找到一个好的解决方案,以解决静态GetUser()的问题。

From what I can see it looks like User.GetUser() will return a User object, not a MyUserDTO object, which is why you don't get those extra fields. 从我所看到的看起来,User.GetUser()将返回一个User对象,而不是MyUserDTO对象,这就是为什么您没有得到这些额外字段的原因。 The DAL layer needs to abide by the contract it was written against. DAL层必须遵守它所针对的合同。 There are a handful of ways you could go about solving this, although some are more "right" than others. 您可以通过几种方法解决此问题,尽管有些方法比其他方法更“正确”。

You could cast the object you get out of User.GetUser to the appropriate object which would then give you access to the casted object's fields (assuming the object can be cast to that type). 您可以将您从User.GetUser中获得的对象强制转换为适当的对象,然后该对象将允许您访问已转换对象的字段(假设对象可以强制转换为该类型)。 I don't really like this solution from a design perspective, but it would work: 从设计的角度来看,我不是很喜欢这种解决方案,但是它可以工作:

MyDTOUser myDtoUser = User.GetUser() as MyDTOUser;

Other people have posted some other examples of ways around this so I won't repeat those here. 其他人还发布了一些其他解决方法的示例,因此在此不再赘述。

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

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