Guest User

Untitled

a guest
Mar 5th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.85 KB | None | 0 0
  1. namespace Domain.Entities
  2. {
  3. public class BaseEntity
  4. {
  5. public virtual Guid ID { get; set; }
  6.  
  7. public virtual DateTime DateCreated { get; set; }
  8.  
  9. public virtual DateTime? DeletedDate { get; set; }
  10.  
  11. public override bool Equals (object obj)
  12. {
  13. if (obj == null)
  14. return false;
  15. var t = obj as BaseEntity;
  16. if (t == null)
  17. return false;
  18. if (ID == t.ID)
  19. return true;
  20. return false;
  21. }
  22.  
  23. public override int GetHashCode ()
  24. {
  25. int hash = GetType ().GetHashCode ();
  26. hash = (hash * 397) ^ ID.GetHashCode ();
  27. return hash;
  28. }
  29. }
  30. }
  31.  
  32. namespace Domain.Entities
  33. {
  34. public class Company : BaseEntity
  35. {
  36. public virtual string Name { get; set; }
  37.  
  38. public virtual IEnumerable<User> Users { get; set; }
  39.  
  40. public virtual IEnumerable<Branch> Branches { get; set; }
  41.  
  42. public virtual IEnumerable<Department> Departments { get; set; }
  43.  
  44. public class CompanyCollection : List<Company>
  45. {
  46.  
  47. }
  48. }
  49. }
  50.  
  51. namespace Domain.Interfaces
  52. {
  53. public interface IReadRepository<TEntity> where TEntity : class
  54. {
  55. IQueryable<TEntity> All ();
  56.  
  57. TEntity FindBy (Expression<Func<TEntity, bool>> expression);
  58.  
  59. TEntity FindBy (object id);
  60.  
  61. IQueryable<TEntity> FilterBy (Expression<Func<TEntity, bool>> expression);
  62. }
  63. }
  64.  
  65. namespace Domain.Interfaces
  66. {
  67. public interface IWriteRepository<TEntity> where TEntity : class
  68. {
  69. bool Add (TEntity entity);
  70.  
  71. bool Add (IEnumerable<TEntity> entities);
  72.  
  73. bool Update (TEntity entity);
  74.  
  75. bool Update (IEnumerable<TEntity> entities);
  76.  
  77. bool Delete (TEntity entity);
  78.  
  79. bool Delete (IEnumerable<TEntity> entities);
  80. }
  81. }
  82.  
  83. namespace Domain.Interfaces
  84. {
  85. public interface IReadWriteRepository<TEntity> :
  86. IReadRepository<TEntity>, IWriteRepository<TEntity> where TEntity : class
  87. {
  88.  
  89. }
  90. }
  91.  
  92. namespace Domain.Interfaces
  93. {
  94. public interface IUnitOfWork : IDisposable
  95. {
  96. void Commit ();
  97.  
  98. void Rollback ();
  99. }
  100. }
  101.  
  102. namespace Infrastructure.Interfaces
  103. {
  104. public interface IConfigService
  105. {
  106. string Connection{ get; }
  107. }
  108. }
  109.  
  110. namespace Infrastructure.Interfaces
  111. {
  112. public interface ILoggingService
  113. {
  114. bool IsDebugEnabled { get; }
  115.  
  116. bool IsErrorEnabled { get; }
  117.  
  118. bool IsFatalEnabled { get; }
  119.  
  120. bool IsInfoEnabled { get; }
  121.  
  122. bool IsTraceEnabled { get; }
  123.  
  124. bool IsWarnEnabled { get; }
  125.  
  126. void Debug (Exception exception);
  127.  
  128. void Debug (string format, params object[] args);
  129.  
  130. void Debug (Exception exception, string format, params object[] args);
  131.  
  132. void Error (Exception exception);
  133.  
  134. void Error (string format, params object[] args);
  135.  
  136. void Error (Exception exception, string format, params object[] args);
  137.  
  138. void Fatal (Exception exception);
  139.  
  140. void Fatal (string format, params object[] args);
  141.  
  142. void Fatal (Exception exception, string format, params object[] args);
  143.  
  144. void Info (Exception exception);
  145.  
  146. void Info (string format, params object[] args);
  147.  
  148. void Info (Exception exception, string format, params object[] args);
  149.  
  150. void Trace (Exception exception);
  151.  
  152. void Trace (string format, params object[] args);
  153.  
  154. void Trace (Exception exception, string format, params object[] args);
  155.  
  156. void Warn (Exception exception);
  157.  
  158. void Warn (string format, params object[] args);
  159.  
  160. void Warn (Exception exception, string format, params object[] args);
  161. }
  162. }
  163.  
  164. namespace Infrastructure.Data.Helpers
  165. {
  166. public class NHibernateHelper
  167. {
  168. private ISessionFactory _sessionFactory;
  169. private readonly string _connectionString;
  170.  
  171. public NHibernateHelper (string connectionString)
  172. {
  173. if (string.IsNullOrEmpty (connectionString))
  174. throw new HibernateConfigException ("ConnectionString in Web.config is not set.");
  175.  
  176. _connectionString = connectionString;
  177. }
  178.  
  179. public ISessionFactory SessionFactory {
  180. get {
  181. return _sessionFactory ?? (_sessionFactory = InitializeSessionFactory ());
  182. }
  183. }
  184.  
  185. private ISessionFactory InitializeSessionFactory ()
  186. {
  187. return Fluently.Configure () . . . . . . .;
  188. }
  189. }
  190.  
  191. }
  192.  
  193. namespace Infrastructure.Data.Helpers
  194. {
  195. public class UnitOfWork : IUnitOfWork
  196. {
  197. private readonly ISessionFactory _sessionFactory;
  198. private readonly ITransaction _transaction;
  199.  
  200. public ISession Session { get; private set; }
  201.  
  202. public UnitOfWork (ISessionFactory sessionFactory)
  203. {
  204. _sessionFactory = sessionFactory;
  205. Session = _sessionFactory.OpenSession ();
  206. Session.FlushMode = FlushMode.Auto;
  207. _transaction = Session.BeginTransaction (IsolationLevel.ReadCommitted);
  208. }
  209.  
  210. public void Commit ()
  211. {
  212. if (!_transaction.IsActive) {
  213. throw new InvalidOperationException ("Oops! We don't have an active transaction");
  214. }
  215. _transaction.Commit ();
  216. }
  217.  
  218. public void Rollback ()
  219. {
  220. if (_transaction.IsActive) {
  221. _transaction.Rollback ();
  222. }
  223. }
  224.  
  225. public void Dispose ()
  226. {
  227. if (Session.IsOpen) {
  228. Session.Close ();
  229. Session = null;
  230. }
  231. }
  232. }
  233. }
  234.  
  235. namespace Infrastructure.Data.Mapping
  236. {
  237. public class BaseMapping : ClassMap<BaseEntity>
  238. {
  239. public BaseMapping ()
  240. {
  241. UseUnionSubclassForInheritanceMapping ();
  242. Id (x => x.ID);
  243. Map (x => x.DateCreated);
  244. Map (x => x.DeletedDate);
  245. }
  246. }
  247. }
  248.  
  249. namespace Infrastructure.Data.Mapping
  250. {
  251. public class CompanyMapping : SubclassMap<Company>
  252. {
  253. public CompanyMapping ()
  254. {
  255. Abstract ();
  256. Map (x => x.Name);
  257.  
  258. HasManyToMany<User> (x => x.Users).Table ("CompanyUser").Inverse ();
  259.  
  260. HasMany<Branch> (x => x.Branches).Inverse ().Cascade.All ();
  261. HasMany<Department> (x => x.Departments).Inverse ().Cascade.All ();
  262.  
  263. Table ("Company");
  264. }
  265. }
  266. }
  267.  
  268. namespace Infrastructure.Data.Repositories
  269. {
  270. public class Repository<TEntity> : IReadWriteRepository<TEntity>
  271. where TEntity : class
  272. {
  273. private readonly ISession _session;
  274.  
  275. public Repository (ISession session)
  276. {
  277. _session = session;
  278. }
  279.  
  280. #region IWriteRepository
  281.  
  282. public bool Add (TEntity entity)
  283. {
  284. _session.Save (entity);
  285. return true;
  286. }
  287.  
  288. public bool Add (System.Collections.Generic.IEnumerable<TEntity> entities)
  289. {
  290. foreach (TEntity entity in entities) {
  291. _session.Save (entity);
  292. }
  293. return true;
  294. }
  295.  
  296. public bool Update (TEntity entity)
  297. {
  298. _session.Update (entity);
  299. return true;
  300. }
  301.  
  302. public bool Update (System.Collections.Generic.IEnumerable<TEntity> entities)
  303. {
  304. foreach (TEntity entity in entities) {
  305. _session.Update (entity);
  306. }
  307. return true;
  308. }
  309.  
  310. public bool Delete (TEntity entity)
  311. {
  312. _session.Delete (entity);
  313. return true;
  314. }
  315.  
  316. public bool Delete (System.Collections.Generic.IEnumerable<TEntity> entities)
  317. {
  318. foreach (TEntity entity in entities) {
  319. _session.Delete (entity);
  320. }
  321. return true;
  322. }
  323.  
  324. #endregion
  325.  
  326. #region IReadRepository
  327.  
  328. public System.Linq.IQueryable<TEntity> All ()
  329. {
  330. return _session.Query<TEntity> ();
  331. }
  332.  
  333. public TEntity FindBy (System.Linq.Expressions.Expression<System.Func<TEntity, bool>> expression)
  334. {
  335. return FilterBy (expression).SingleOrDefault ();
  336. }
  337.  
  338. public TEntity FindBy (object id)
  339. {
  340. return _session.Get<TEntity> (id);
  341. }
  342.  
  343. public System.Linq.IQueryable<TEntity> FilterBy (System.Linq.Expressions.Expression<System.Func<TEntity, bool>> expression)
  344. {
  345. return All ().Where (expression).AsQueryable ();
  346. }
  347.  
  348. #endregion
  349. }
  350. }
  351.  
  352. namespace Infrastructure.Data.Services
  353. {
  354. public class ConfigService : IConfigService
  355. {
  356. #region IConfigService implementation
  357.  
  358. public string Connection {
  359. get {
  360. string strConnectionString = null;
  361. var connectionSettings = ConfigurationManager.ConnectionStrings ["Connection"];
  362.  
  363. if (connectionSettings != null) {
  364. strConnectionString = connectionSettings.ConnectionString;
  365. }
  366.  
  367. return strConnectionString;
  368. }
  369. }
  370.  
  371. #endregion
  372. }
  373. }
  374.  
  375. namespace Infrastructure.DependecyResolution
  376. {
  377. public class ConfigPackage : IPackage
  378. {
  379. #region IPackage implementation
  380.  
  381. public void RegisterServices (SimpleInjector.Container container)
  382. {
  383. container.Register<IConfigService,ConfigService> ();
  384. }
  385.  
  386. #endregion
  387. }
  388. }
  389.  
  390. namespace Infrastructure.DependecyResolution
  391. {
  392. public class RepositoryPackage : IPackage
  393. {
  394. #region IPackage implementation
  395.  
  396. public void RegisterServices (SimpleInjector.Container container)
  397. {
  398.  
  399. container.RegisterPerWebRequest<ISessionFactory> (() => {
  400.  
  401. var configPackage = container.GetInstance<IConfigService> ();
  402. NHibernateHelper objNHibernate = new NHibernateHelper (configPackage.Connection);
  403. return objNHibernate.SessionFactory;
  404. });
  405.  
  406. container.RegisterPerWebRequest<IUnitOfWork, UnitOfWork> ();
  407.  
  408. container.RegisterPerWebRequest<ISession> (() => {
  409.  
  410. UnitOfWork unitOfWork = (UnitOfWork)container.GetInstance<IUnitOfWork> ();
  411. return unitOfWork.Session;
  412.  
  413. });
  414.  
  415. container.RegisterOpenGeneric (typeof(IReadWriteRepository<>), typeof(Repository<>));
  416. }
  417.  
  418. #endregion
  419. }
  420. }
  421.  
  422. namespace Infrastructure.DependecyResolution
  423. {
  424. public class LoggingPackage : IPackage
  425. {
  426. #region IPackage implementation
  427.  
  428. public void RegisterServices (SimpleInjector.Container container)
  429. {
  430. ILoggingService logger = GetLoggingService ();
  431.  
  432. container.Register<ILoggingService> (() => logger);
  433. }
  434.  
  435. #endregion
  436.  
  437. private ILoggingService GetLoggingService ()
  438. {
  439. ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition ("utc_date", typeof(UtcDateRenderer));
  440. ConfigurationItemFactory.Default.LayoutRenderers.RegisterDefinition ("web_variables", typeof(WebVariablesRenderer));
  441.  
  442. ILoggingService logger = (ILoggingService)LogManager.GetLogger ("NLogLogger", typeof(LoggingService));
  443.  
  444. return logger;
  445. }
  446. }
  447. }
  448.  
  449. namespace Services.Interfaces
  450. {
  451. public interface ICompanyService
  452. {
  453. IQueryable<Company> GetCompanies ();
  454.  
  455. Company GetCompany (Guid guidId);
  456.  
  457. void CreateNewCompany (Company company);
  458.  
  459. void UpdateExistingCompany (Company company);
  460.  
  461. void DeleteCompany (Company company);
  462. }
  463. }
  464.  
  465. namespace Web.UI.Services
  466. {
  467. public class CompanyService : ICompanyService
  468. {
  469. private IUnitOfWork _unitOfWork;
  470. private IReadWriteRepository<Company> _companyRepository;
  471.  
  472. public CompanyService (IUnitOfWork unitOfWork, IReadWriteRepository<Company> companyRepository)
  473. {
  474. _unitOfWork = unitOfWork;
  475. _companyRepository = companyRepository;
  476. }
  477.  
  478. #region ICompanyService implementation
  479.  
  480. public IQueryable<Company> GetCompanies ()
  481. {
  482. return _companyRepository.All ().Where (x => !x.DeletedDate.HasValue);
  483. }
  484.  
  485. public Company GetCompany (Guid guidId)
  486. {
  487. return _companyRepository.FindBy (guidId);
  488. }
  489.  
  490. public void CreateNewCompany (Company company)
  491. {
  492. if (_companyRepository.Add (company))
  493. _unitOfWork.Commit ();
  494. else
  495. _unitOfWork.Rollback ();
  496. }
  497.  
  498. public void UpdateExistingCompany (Company company)
  499. {
  500. if (_companyRepository.Update (company))
  501. _unitOfWork.Commit ();
  502. else
  503. _unitOfWork.Rollback ();
  504. }
  505.  
  506. public void DeleteCompany (Company company)
  507. {
  508. if (_companyRepository.Update (company))
  509. _unitOfWork.Commit ();
  510. else
  511. _unitOfWork.Rollback ();
  512. }
  513.  
  514. #endregion
  515. }
  516. }
Add Comment
Please, Sign In to add comment