2015-02-10 73 views
-1

我有两个类有一些相似的字段,一些不同,并且根据它在(插入/编辑)模式中使用两个不同的对象。如何组合两个对象,操作数据,然后将它们分开?

而不是使用两个不同的对象,如果语句检查表单模式,我想有一个结构与两个对象字段中的任何一个水合,所以我可以通过页面生命周期操纵一个对象。然后将结构分离回相应的对象以插入/更新数据库。类

实施例:

public partial class SomeClass 
{ 
    public Int32 B {get;set;} 
    public String C {get;set;} 
    public Boolean D {get;set;} 
} 

public class SomeOtherClass 
{ 
    public Int32 A {get;set;} 
    public Int32 B {get;set;} 
    public String C {get;set;} 
} 

更新用溶液实施例:

public interface IInsertable 
{ 
    string SharedName { get; set; } 
    string SharedID { get; set; } 
    string editedFieldValue { get; set; } 
    long GetSuperSecreteInfo(); 
} 

internal class InsertableImplementation : IInsertable 
{ 
    public string SharedName { get; set; } 
    public string SharedID { get; set; } 
    public string editedFieldValue { get; set; } 

    public long GetSuperSecreteInfo() 
    { 
     return -1; 
    } 
} 

public interface IUpdateable 
{ 
    string SharedName { get; set; } 
    string SharedID { get; set; } 
    string updatedFieldValue { get; set; } 
    Guid GenerateStevesMagicGuid(); 
} 

internal class UpdateableImplementation : IUpdateable 
{ 
    public string SharedName { get; set; } 
    public string SharedID { get; set; } 
    public string updatedFieldValue { get; set; } 

    public Guid GenerateStevesMagicGuid() 
    { 
     return new Guid(); 
    } 
} 

public static class WonderTwinFactory 
{ 
    public static WonderTwins GenerateWonderTwin(IUpdateable updateable, IInsertable insertable) 
    { 
     var wt = new WonderTwins(); 

     // who will win? 
     wt.SharedID = updateable.SharedID; 
     wt.SharedID = insertable.SharedID; 

     // you decide? 
     wt.SharedName = updateable.SharedName; 

     wt.editedFieldValue = "stuff"; 

     return wt; 
    } 
} 

public class WonderTwins : IInsertable, IUpdateable 
{ 
    public string SharedName { get; set; } 
    public string SharedID { get; set; } 
    public string editedFieldValue { get; set; } 

    public long GetSuperSecreteInfo() 
    { 
     return 1; 
    } 

    public string updatedFieldValue { get; set; } 

    public Guid GenerateStevesMagicGuid() 
    { 
     return new Guid(); 
    } 
} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     IUpdateable updateable = new UpdateableImplementation(); 
     IInsertable insertable = new InsertableImplementation(); 

     WonderTwins dualImplementatin = WonderTwinFactory.GenerateWonderTwin(updateable, insertable); 

     IUpdateable newUpdateable = dualImplementatin as IUpdateable; 
     IInsertable newInsertable = dualImplementatin as IInsertable; 


    } 
} 
+0

A只在SomeOtherClass和D只在SomeClass – kyle 2015-02-10 20:25:05

+0

对不起,我误读了。这些类需要合并,以便可以通过一个对象读取或设置A - D,然后将它们分离回各自的CRUD操作类。 – kyle 2015-02-10 20:36:38

回答

2

有两个类实现定义共同的每个操作,包括被共享的字段(假定一个接口该视图需要访问它们)以及实际执行它们表示的操作的方法(插入/编辑)。

+0

我最终不得不为两个类创建一个接口,一个额外的工厂类实现了接口和一个final类来包装一个对象以发送给浏览器。非常感谢,因为我之前没有写过界面,所以这绝对给了我一些额外的经验。 – kyle 2015-02-11 14:39:07

0

做这种事情的其他方法是使用C#动态对象并直接分配属性。这可能有助于避免任何新的类型或界面,并且可以随时根据需要随时直接使用新的动态对象。

var newObject = new { 
objectOfClass1 = x.prop1, 
objectOfClass2 = x.prop2 
} 
相关问题