拜读websharp时,发现的几处问题(三)
6.在DEMO项目Ioffice中也有一些小问题,对于Ioffice.Interfaces项目中,实现对Schdule实体的操作接口ISchduleSystem 用此接口来实现对Schdule实现的操作,咋一看非常不错,但仔细想想还是有些小问题。问题在于,如果按照上面的实现 方式,我们需要对所有的实体都写一个相当于上面的接口来实现对每个实体的操作,如果能再次对实体进行抽象,改成一个通用的实体操作接口,是不是更好一些呢?
代码如下:
1
using System;
2
using Ioffice.Interfaces.EntityDefinition;
3
using Websharp.ORM.Base;
4
5
namespace Ioffice.Interfaces
6

{
7
/**//// <summary>
8
/// IObjectSystem 的摘要说明。
9
/// </summary>
10
public interface IObjectSystem : IDisposable
11
{
12
bool AddObject(EntityData entity);
13
void AddObject(PersistenceCapable pc);
14
bool UpdateObject(EntityData entity);
15
void DelObject(EntityData entity);
16
PersistenceCapable FindObject(string SGuid,System.Type entityType);
17
18
}
19
using System;2
using Ioffice.Interfaces.EntityDefinition;3
using Websharp.ORM.Base;4

5
namespace Ioffice.Interfaces6


{7

/**//// <summary>8
/// IObjectSystem 的摘要说明。9
/// </summary>10
public interface IObjectSystem : IDisposable11

{12
bool AddObject(EntityData entity);13
void AddObject(PersistenceCapable pc);14
bool UpdateObject(EntityData entity);15
void DelObject(EntityData entity);16
PersistenceCapable FindObject(string SGuid,System.Type entityType);17

18
}19

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Xml;
using System.Xml.Serialization;
using Ioffice.Interfaces;
using Ioffice.Interfaces.EntityDefinition;
using Websharp.Data;
using Websharp.ORM.Base;
using Websharp.ORM.Service;
namespace Ioffice.Interfaces

{
/**//// <summary>
/// ObjectDALSystem 的摘要说明。
/// </summary>
public class ObjectDALSystem : IObjectSystem 
{
public ObjectDALSystem()
{
}
public bool AddObject(EntityData entity)
{
if(!CheckSchdule(entity))
return false;
PersistenceManager pm =
PersistenceManagerFactory.Instance().CreatePersistenceManager();
Transaction trans = pm.CurrentTransaction;
trans.Begin();
try
{
pm.PersistNewObject(entity);
trans.Commit();
return true;
}
catch(System.Exception ex)
{
string str = ex.Message.ToString();
trans.Rollback();
return false;
}
finally
{
pm.Close();
}
}
public void AddObject(PersistenceCapable pc)
{
AddObject(pc.EntityData);
}
private bool CheckSchdule(EntityData entity)
{
return true;
}


Dispose#region Dispose
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(true);
}
protected virtual void Dispose(bool disposing)
{
if (! disposing)
return; // we're being collected, so let the GC take care of this object
}
#endregion
}
}
1
using System;
2
using Websharp.Data;
3
using Websharp.ORM.Base;
4
5
namespace Ioffice.Interfaces
6

{
7
/**//// <summary>
8
/// OjbectDALSystemFactory
9
/// </summary>
10
public class ObjectDALSystemFactory
11
{
12
private ObjectDALSystemFactory()
{}
13
14
private static ObjectDALSystemFactory m_ObjectDALSystemFactory = new ObjectDALSystemFactory();
15
16
public static ObjectDALSystemFactory Instance()
17
{
18
return m_ObjectDALSystemFactory;
19
}
20
public IObjectSystem CreateIObjectSystem()
21
{
22
return p_CreateIObjectSystem();
23
}
24
25
private IObjectSystem p_CreateIObjectSystem()
26
{
27
return new ObjectDALSystem();
28
}
29
}
30
}
31
使用方法如下:
using System;2
using Websharp.Data;3
using Websharp.ORM.Base;4

5
namespace Ioffice.Interfaces6


{7

/**//// <summary>8
/// OjbectDALSystemFactory9
/// </summary>10
public class ObjectDALSystemFactory11

{12

private ObjectDALSystemFactory()
{}13

14
private static ObjectDALSystemFactory m_ObjectDALSystemFactory = new ObjectDALSystemFactory();15

16
public static ObjectDALSystemFactory Instance()17

{18
return m_ObjectDALSystemFactory;19
}20
public IObjectSystem CreateIObjectSystem()21

{22
return p_CreateIObjectSystem();23
}24

25
private IObjectSystem p_CreateIObjectSystem()26

{27
return new ObjectDALSystem();28
}29
}30
}31

1
T1 t1 = EntityManager.CreateObject(typeof(T1)) as T1;
2
t1.A = "3333";
3
t1.B = null;
4
t1.C= 199;
5
t1.D = System.DateTime.Now;
6
t1.F = 2;
7
8
IObjectSystem IOS = ObjectDALSystemFactory.Instance().CreateIObjectSystem();
9
10
IOS.AddObject(t1);
T1 t1 = EntityManager.CreateObject(typeof(T1)) as T1;2
t1.A = "3333";3
t1.B = null;4
t1.C= 199;5
t1.D = System.DateTime.Now;6
t1.F = 2;7

8
IObjectSystem IOS = ObjectDALSystemFactory.Instance().CreateIObjectSystem();9

10
IOS.AddObject(t1);